Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unity: Add and Switch Chains #880

Open
WoodsFiend opened this issue Jun 6, 2024 · 0 comments
Open

Unity: Add and Switch Chains #880

WoodsFiend opened this issue Jun 6, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@WoodsFiend
Copy link

WoodsFiend commented Jun 6, 2024

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.

//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; }
}
@WoodsFiend WoodsFiend added the enhancement New feature or request label Jun 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant