Metamask Wallet Add Ethereum Chain Error: Wrong Chain ID

As a developer, you may have encountered issues integrating MetaMask into your Android app to add custom Ethereum networks via the Wallet_addEthereumChain RPC function. In this article, we will delve deeper into the issue and provide solutions to resolve the issue with the wrong chain ID.

Problem

When using the wallet_addEthereumChain RPC function in a Metamask Android app, it is essential to ensure that the added network is correctly identified by the wallet. The chainId property of the Ethereum network object returned by this function is crucial for correctly identifying and managing custom networks.

Error

In your case, the error message indicates that the chain ID returned by Metamask is incorrect. Specifically, it says `wrong chain id''. This suggests that there may be a problem with the way you pass or access thechainIdproperty when adding the custom network to your wallet.

Code Review

Let's take a closer look at the code snippet:

const Chain = await provider.getChain("customNetwork");

const chainid = Chain.chainId;

wallet_addEthereumChain({

network: chain,

from: accountAddress,

});

Notice that we pass chainIdas an argument to thewallet_addEthereumChainfunction. However, in your code, you just pass the custom network object directly:

const Chain = await provider.getChain("customNetwork");

wallet_addEthereumChain({

from: accountAddress,

});

As we can see, when adding the custom network to the wallet, you are passing an empty {}object instead ofchainId. This is probably causing the issue with the wrong chain ID.

Workaround

Metamask: wallet_addEthereumChain is not working in metamask android app

To fix this issue, make sure you are passing the correctchainIdvalue when callingwallet_addEthereumChain. Here is an updated code snippet:

const Chain = await provider.getChain("customNetwork");

const chainid = Chain.chainId;

wallet_addEthereumChain({

network: chain,

from: accountAddress,

});

Additional Tips

To ensure proper identification of custom Ethereum networks, make sure that:

By following these steps, you should be able to resolve the wrong chain ID issue and successfully integrate your custom Ethereum network into your Android app using MetaMask.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *