1

Im trying to programmatically switch the metamask network of the user. Using wallet_addEthereumChain

It's working fine when requesting a network change for Polygon mumbai, however it doesn't work when using Polygon Mainet, and I get : Chain ID returned by RPC URL https://polygon-rpc.com does not match 0x137

If I add manually the network on metamask, using the same information it works.

  • This code above is working, for polygon mumbai
const { ethereum } = window;
 await ethereum.request({
                id: 1,
                jsonrpc: "2.0",
                method: "wallet_addEthereumChain",
                params: [
                  {
                    chainId: "0x13881",
                    rpcUrls: ["https://rpc-mumbai.maticvigil.com"],
                    chainName: "Polygon Testnet Mumbai",
                    nativeCurrency: {
                      name: "tMATIC",
                      symbol: "tMATIC", // 2-6 characters long
                      decimals: 18,
                    },
                    blockExplorerUrls: ["https://mumbai.polygonscan.com/"],
                  },
                ],
              }); 

const { ethereum } = window;
await ethereum.request({
                  id: 1,
                  jsonrpc: "2.0",
                  method: "wallet_addEthereumChain",
                  params: [
                    {
                      chainId: "0x137",
                      rpcUrls:[  "https://polygon-rpc.com"],

                      chainName: "Polygon Mainnet",
                      nativeCurrency: {
                        name: "MATIC",
                        symbol: "MATIC", // 2-6 characters long
                        decimals: 18,
                      },
                      blockExplorerUrls: ["https://polygonscan.com/"],
                    },
                  ],
                });

  • Here is a screenshoot of what the user is supposing to get on his metamask wallet when i fire the request.

enter image description here

2 Answers 2

5

Try to convert to hex the chainId with web3.utils.toHex() function:

chainId = '137'; chainId = web3.utils.toHex(chainId);

chainData = [{
    chainId: chainId,
    chainName: 'Matic(Polygon) Mainnet',
    nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 },
    rpcUrls: ['https://polygon-rpc.com'],
    blockExplorerUrls: ['https://www.polygonscan.com'],
}];
1
  • That worked! Thanks! You saved me some headaches...
    – romain1304
    Dec 16, 2021 at 7:34
2
import { utils } from 'ethers';

const networkMap = {
  POLYGON_MAINNET: {
    chainId: utils.hexValue(137), // '0x89'
    chainName: "Matic(Polygon) Mainnet", 
    nativeCurrency: { name: "MATIC", symbol: "MATIC", decimals: 18 },
    rpcUrls: ["https://polygon-rpc.com"],
    blockExplorerUrls: ["https://www.polygonscan.com/"],
  },
  MUMBAI_TESTNET: {
    chainId: utils.hexValue(80001), // '0x13881'
    chainName: "Matic(Polygon) Mumbai Testnet",
    nativeCurrency: { name: "tMATIC", symbol: "tMATIC", decimals: 18 },
    rpcUrls: ["https://rpc-mumbai.maticvigil.com"],
    blockExplorerUrls: ["https://mumbai.polygonscan.com/"],
  },
};


await window.ethereum.request({
    method: "wallet_addEthereumChain",
    params: [networkMap.MUMBAI_TESTNET],
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.