Metamask Issue: WalletAddEthereumChain not working on Polygon mainnet but not Polygon Mumbai
As a Metamask user, you are probably no stranger to the frustration of switching between different blockchains. However, when using MetaMask’s wallet_addEthereumChain
method, it doesn’t seem to work as expected when switching from Polygon mainnet to Polygon Mumbai.
In this article, we will dive into the issue and explore possible solutions to fix it.
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) when it already has an existing network (Polygon mainnet).
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 uses the first available network in your wallet. However, when switching between different networks, it seems to ignore the existing networks and tries to add them again.
Solution
To solve this issue, you can use the following approach:
- List available networks: Before trying to switch between networks, list all available Ethereum networks using MetaMask’s
listAllAccounts
method.
- Check existing network: Check if there are any duplicate network names in your 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 you have a networks
object in your wallet data. If not, you may need to get a list of available networks using listAllAccounts()
and then loop through each network to find the one that matches the desired name.
- Add the Polygon Mumbai network: After deleting the existing network, add a new one to the wallet using:
const polygonMumbaiNetwork = {
id: 'Polygon Mumbai',
chainId: 431911,
address: '0x...' // Replace with the generated Polygon Mumbai address.
};
await MetaMask.addNetwork(polygonMumbaiNetwork);
Conclusion
Switching between Ethereum networks using Metamask’s wallet_addEthereumChain
method can be a bit more complicated than switching between different blockchains. However, by following these steps and verifying that the issue is not a wallet configuration issue or network availability, you should be able to resolve the issue.
Sample use case
Here is a sample code snippet showing how to switch from Polygon mainnet to Polygon Mumbai:
“`javascript
const mainnetNetworkName = ‘Polygon Mainnet’;
const existingNetworkNames = await listAllAccounts();
const mainnetIndex = existingNetworkNames.findIndex(network => network.name === mainnetNetworkName);
if (mainnetIndex !== -1) {
// Remove the existing network from the wallet.
const mainnetAccount = wait for MetaMask.getNetworkAccount(mainnetIndex);
wait for removeNetworkFromWallet(mainnetAccount.
Leave a Reply