MetaMask / MetaMask/metamask-sdk

Unity: Add and Switch Chains

Open
#880 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
TypeScript
Stars
338
Forks
260
PR merge metrics
No merged PRs in 30d

Description

# Feature Request
Implement chain adding and switching to the Core Unity SDK, instead of in the samples.

## Why it is needed
Adding and switching chains is a standard requirement for web3 applications that require the user to use a specific chain.

## Possible implementation
Add a function to MetaMaskUnity that enables switching to a new chain, if switching fails, add the chain then switch to it.

### Code sample
This solution is partial and doesn't allow for all the options to be customized when adding new chains such as token decimals and token name.
```C#
//Send a request to metamask to add and change to the chain
private async void AddAndSwitchMetamaskChain(string chainId, string chainName, string tokenSymbol, string[] rpcUrls)
{
//Send a request to metamask to add and change to the chain
MetaMaskSwitchChain chainSwitchDetails = new MetaMaskSwitchChain
{
ChainId = chainId
};
MetaMaskEthereumRequest request = new MetaMaskEthereumRequest
{
Method = "wallet_switchEthereumChain",
Parameters = new MetaMaskSwitchChain[] {
chainSwitchDetails
}
};
try
{
await MetaMaskUnity.Instance.Wallet.Request(request);
}
catch(Exception e)
{

//We have to add the chain...
Debug.LogWarning(e.Message);
AddMetamaskChain(chainId, chainName, tokenSymbol, rpcUrls);
}
}
//Add the chain to MetaMask
private async void AddMetamaskChain(string chainId, string chainName, string tokenSymbol, string[] rpcUrls)
{
MetaMaskAddChain addChainDetails = new MetaMaskAddChain
{
ChainId = chainId,
ChainName = chainName,
RpcUrls = rpcUrls,
NativeCurrency = new MetamaskNativeCurrency() { Decimals=18, Name= tokenSymbol, Symbol=tokenSymbol }
};
MetaMaskEthereumRequest request = new MetaMaskEthereumRequest
{
Method = "wallet_addEthereumChain",
Parameters = new MetaMaskAddChain[] {
addChainDetails
}
};
try
{
await MetaMaskUnity.Instance.Wallet.Request(request);
}
catch (Exception e)
{

Debug.LogWarning(e.Message);
//Failed to add the chain
}
}

//Classes for Metamask chain switching
public class MetaMaskSwitchChain
{
[JsonProperty("chainId")]
public string ChainId { get; set; }
}
public class MetaMaskAddChain
{
[JsonProperty("chainId")]
public string ChainId { get; set; }
[JsonProperty("chainName")]
public string ChainName { get; set; }
[JsonProperty("rpcUrls")]
public string[] RpcUrls { get; set; }
[JsonProperty("nativeCurrency")]
public MetamaskNativeCurrency NativeCurrency { get; set; }

}
public class MetamaskNativeCurrency
{
[JsonProperty("decimals")]
public int Decimals { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("symbol")]
public string Symbol { get; set; }
}
```

Contributor guide

Open the contributing guide

Research direction

Start at MetaMaskUnity and its Wallet.Request path in the Core Unity SDK, then compare the sample chain-switching code with the existing request models. Done means the SDK exposes reusable chain switching with add-then-switch fallback and supports the chain and native-currency options described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, unity
Domain
blockchain, game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.