Metamask: Metamask: wallet_addEthereumChain is not working when using polygon mainet but works with polygon mumbai
Metamask Issue: WalletAddEthereumChain not working on Polygon mainnet but not Polygon Mumbai
As a Metamask user, you are probably familiar with the frustration of switching between different blockchains. However, when using MetaMask’s wallet_addEthereumChain
method, it does not work as expected when migrating from Polygon Mainnet to Polygon Mumbai.
In this article, we will take a closer look at this issue and look at possible solutions.
Issue
When trying to switch between different blockchains using MetaMask’s wallet_addEthereumChain
method, you may encounter the following error:
Ethereum: Cannot add more than one network. Network "Polygon Mumbai" already exists.
This means that Metamask is trying to add a new network (Polygon Mumbai) even though it already has an existing network (Polygon main network).
Problem
It seems that the wallet_addEthereumChain
method does not take into account the presence of multiple Ethereum networks. When you request a new chain, MetaMask will use the first available network in your wallet. However, when switching between different networks, the computer ignores the existing networks and tries to add them again.
Solution
To solve this problem, you can use the following approach:
- List available networks: Before trying to switch between networks, list all available Ethereum networks using the
listAllAccounts
method of MetaMask.
- Check existing network
: Make sure there are no duplicate network names in the wallet.
- Switch to Polygon Mumbai: If you want to switch to Polygon Mumbai, use the following code:
const polygonMumbaiNetworkName = 'Polygon Mumbai';
const existingNetworkNames = await listAllAccounts();
const existingNetworkIndex = existingNetworkNames.findIndex(network => network.name === polygonMumbaiNetworkName);
if (existingNetworkIndex !== -1) {
// Remove existing network from wallet.
const existingNetworkAccount = await MetaMask.getNetworkAccount(existingNetworkIndex);
await removeNetworkFromWallet(existingNetworkAccount.networkId, existingNetworkIndex);
}
Note that this approach assumes that you have a networks
object in your wallet data. Otherwise, you may need to get a list of available networks using listAllAccounts()
and then loop through each network to find the one that matches your desired name.
- Add the Polygon Mumbai network
: After deleting the existing network, add the new one to your wallet using:
const polygonMumbaiNetwork = {
id: 'Polygon Mumbai',
chain id: 431911,
address: '0x...' // Replace with the generated Polygon Mumbai address.
};
await MetaMask.addNetwork(polygonMumbaiNetwork);
Conclusion
Switching between Ethereum networks using the wallet_addEthereumChain
method of Metamask can be a bit more complicated than switching between different blockchains. However, if you follow the steps below and make sure that the problem is not related to your wallet configuration or network availability, you should be able to resolve it.
Usage Example
Here is a sample code snippet showing how to switch from the Polygon mainnet to Polygon Mumbai:
“`javascript
const mainnetNetworkName = ‘Main Polygon Network’;
const existingNetworkNames = await listAllAccounts();
const mainnetIndex = existingNetworkNames.findIndex(network => network.name === mainnetNetworkName);
if (mainnetIndex !== -1) {
// Remove existing network from wallet.
const mainnetAccount = await MetaMask.getNetworkAccount(mainnetIndex);
await removeNetworkFromWallet(mainnetAccount.