You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
//Send a request to metamask to add and change to the chainprivateasyncvoidAddAndSwitchMetamaskChain(stringchainId,stringchainName,stringtokenSymbol,string[]rpcUrls){//Send a request to metamask to add and change to the chainMetaMaskSwitchChainchainSwitchDetails=new MetaMaskSwitchChain
{ChainId=chainId};MetaMaskEthereumRequestrequest=new MetaMaskEthereumRequest
{Method="wallet_switchEthereumChain",Parameters=new MetaMaskSwitchChain[]{
chainSwitchDetails
}};try{await MetaMaskUnity.Instance.Wallet.Request(request);}catch(Exceptione){//We have to add the chain...
Debug.LogWarning(e.Message);
AddMetamaskChain(chainId, chainName, tokenSymbol, rpcUrls);}}//Add the chain to MetaMaskprivateasyncvoidAddMetamaskChain(stringchainId,stringchainName,stringtokenSymbol,string[]rpcUrls){MetaMaskAddChainaddChainDetails=new MetaMaskAddChain
{ChainId=chainId,ChainName=chainName,RpcUrls=rpcUrls,NativeCurrency=new MetamaskNativeCurrency(){Decimals=18,Name=tokenSymbol,Symbol=tokenSymbol}};MetaMaskEthereumRequestrequest=new MetaMaskEthereumRequest
{Method="wallet_addEthereumChain",Parameters=new MetaMaskAddChain[]{
addChainDetails
}};try{await MetaMaskUnity.Instance.Wallet.Request(request);}catch(Exceptione){
Debug.LogWarning(e.Message);//Failed to add the chain}}//Classes for Metamask chain switchingpublicclassMetaMaskSwitchChain{[JsonProperty("chainId")]publicstringChainId{get;set;}}publicclassMetaMaskAddChain{[JsonProperty("chainId")]publicstringChainId{get;set;}[JsonProperty("chainName")]publicstringChainName{get;set;}[JsonProperty("rpcUrls")]publicstring[] RpcUrls {get;set;}[JsonProperty("nativeCurrency")]publicMetamaskNativeCurrencyNativeCurrency{get;set;}}publicclassMetamaskNativeCurrency{[JsonProperty("decimals")]publicintDecimals{get;set;}[JsonProperty("name")]publicstringName{get;set;}[JsonProperty("symbol")]publicstringSymbol{get;set;}}
The text was updated successfully, but these errors were encountered:
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.
The text was updated successfully, but these errors were encountered: