Metamsk Edition: TolletadteThereumchain does not work on Polygon Main, but not Polygon Mumbai
As a metamic user, you are probably not alien to switch to a different blockchain networks. However, when using the Metamasc’s ‘Wallet_Addethereumchain’ method, it does not seem to work the way it is expected when it goes from the landfill to the Mumbai Polygon.
In this article, we will study the problem and explore the possible solutions to solve this problem.
Problem
When you try to switch between different blockchain networks using Metamascs’ Wallet_Addethereumchain, the following error may occur:
Javascript
Ethereum: I can’t add more than one net. The Mumbai Polygon network already exists.
`
This indicates that Metamascus is trying to add a new network (Polygon Mumbai).
Problem
The method of Wallet_Addethereumchain
does not appear to take into account the presence of more Ethereum network. When you ask for a new chain, Metamascus uses the first network available in your portfolio. However, when you are switching between different networks, it seems that it neglects existing networks and tries to add them again.
Decision
You can use the following approach to solve this problem:
- List of available networks
: Before you try to switch between networks, list all Ethereum networks available using the Metamašk
Listlaccounts' Metamascus method.
- Check the existing network : Check that there are no duplicated network names in your portfolio.
- Switch to Polygon Mumbai : If you want to go to Polygon Mumbai, use the following code:
Javascript
CONST PolygonmumbainetworkName = ‘Mumbai’ Polygon;
CONST IDECTWORKKNAMES = Wait for Leafalloccounts ();
CONST exists indiantingWorkdinex = existing.
If (existing existing! == -1) {{
// Remove the existing network from your portfolio.
CONST INSIDETWORKACCount = Wait Metamass.getttTWorkaccount (existing);
Wait for emmoveneetworkfromwallet (existing existing (existing existing);
It
`
Keep in mind that this approach suggests that you have the networks in your portfolio data. If not, you may need to retrieve a list of available networks using ListlalaCounts (), then cycles through each network to find the one that matches the desired name.
3
Javascript
Const Polygonmumbainework = {
ID: ‘Polygon Mumbai’,
Chainid: 431911,
Address: ‘0x …’ // Replace the generated address for training in Mumbai.
};
In anticipation of metamask.addnetwork (Polygonmumbaineetwork);
`
Conclusion
Switching between the Ethereum network using the Wallet_Addethereumchain ‘Wallet_adethereumchain’ method can be slightly more engaged than the switching between different blockchain chains. However, following these steps and check that the problem is not a matter of configuring your portfolio or network availability, you should be able to resolve the problem.
Example uses the case
Here is an example of a code clip showing how to switch from Polygon Mainnet to Polygon Mumbai:
`Javascript
CONST MAINNETTWORKNAME = ‘POLYGON MAINNET’;
CONST IDECTWORKKNAMES = Wait for Leafalloccounts ();
Const maintendindex = existing existing.Findindex (Network => network.name === MainetnetworkName);
If (Mainnetindex! == -1) {
// Remove the existing network from your portfolio.
Const Mainnetaccount = Wait Metamask.getnetworkCount (Mainnenningex);
Go to Emomovenetworkformwallet (Mainnetaccount.