Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch Metamask Network to chain 1 (Ethereum Mainnet)

This past week Metamask introduced a new function called "wallet_addEthereumChain" which allows the user to automatically add a new Etheruem RPC to their wallet when prompted. This function also allows the user to change the network they are connected to, for example, if I already have Binance Smart Chain connected to metamask, calling wallet_addEthereumChain changes the active network to BSC. However, when trying this for the Ethereum chain, it gives an error that you cannot add mainnet RPC. I have used the following code to change to change from Ethereum Mainnet to Binance Smart Chain, and it works fine: switchToBinance: async function () {

        let ethereum = window.ethereum;
        const data = [{
            chainId: '0x38',
            chainName: 'Binance Smart Chain',
            nativeCurrency:
                {
                    name: 'BNB',
                    symbol: 'BNB',
                    decimals: 18
                },
            rpcUrls: ['https://bsc-dataseed.binance.org/'],
            blockExplorerUrls: ['https://bscscan.com/'],
        }]
        /* eslint-disable */
        const tx = await ethereum.request({method: 'wallet_addEthereumChain', params:data}).catch()
        if (tx) {
            console.log(tx)
        }
    },

However when I try the Exact thing, metamask throws an exception saying I cannot add a mainnet RPC: switchToEthereum: async function () {

        let ethereum = window.ethereum;
        const data = [{
      chainId: '0x1',
      chainName: 'Ethereum',
      nativeCurrency: {
        name: 'Ethereum',
        symbol: 'ETH',
        decimals: 18,
      },
      rpcUrls: ['https://mainnet.infura.io/v3/undefined'],
      blockExplorerUrls: ['https://etherscan.io'],
    }]
        /* eslint-disable */
        const tx = await ethereum.request({method: 'wallet_addEthereumChain', params:data}).catch()
        if (tx) {
            console.log(tx)
        }
    },

However, the request for adding a new RPC connection, and changing the active RPC connection is the same. So is there a way to change the active Ethereum provider from a custom chain to Mainnet (chain ID-1)

like image 991
Alexander Barry Avatar asked Mar 11 '21 18:03

Alexander Barry


People also ask

Can you swap between networks on MetaMask?

If you're on Mainnet, simply switch your network, and approve the transaction in MetaMask. You can then select which token you would like to swap and click “Approve Transaction”.

How do I switch to Ethereum Mainnet on MetaMask?

To switch from Binance Smart Chain to Ethereum mainnet: Click on the MetaMask browser extension icon. Click on the dropdown menu at the top of the pop up window. Click on the "Ethereum Mainnet" option at the top of the dropdown menu. The DODO portal will automatically refresh once you have switched to Ethereum Mainnet.

How do I use metamask as an Ethereum wallet?

The easiest way to use MetaMask as an Ethereum wallet is to use it as a browser extension for your own web browser. The MetaMask browser plugin is specially designed for the Google Chrome browser. However, it can also be installed on the following browsers:

Is it possible to add mainnet RPC for Ethereum?

However, when trying this for the Ethereum chain, it gives an error that you cannot add mainnet RPC. I have used the following code to change to change from Ethereum Mainnet to Binance Smart Chain, and it works fine: switchToBinance: async function () {

How do I add a new network to metamask?

It will open a menu where you can select a different network. At the bottom of the aforementioned list with networks, you will see the button “Add Network”. This will allow you to do exactly what it says, add support for another network to your installation of Metamask.

Why does wallet_addethereumchain not support mainnet?

It's something in the way MetaMask is implementing the RPC call. I suggest opening an issue on MetaMask's Github (and post a link to it here for posterity). as this issue comment point, for security reason wallet_addEthereumChain not support mainnet.


3 Answers

as this issue comment point, for security reason wallet_addEthereumChain not support mainnet. But there a new EIP to resolve this issue, follow EIP-3326 to find release info, and this discuss to see draft progrss.

like image 139
izayl Avatar answered Oct 23 '22 00:10

izayl


chainId: '0x38' // error because await not decimal 

Number('0x38').toString(10) //56 - chain ID of BSC

Right:

chainId: `0x${Number(56).toString(16)}`

OR:

chainId: `0x86`
like image 42
Dmytro Slutskyi Avatar answered Oct 22 '22 23:10

Dmytro Slutskyi


Simply you have to prefix 0x with the chainId.

Example:

 await ethereum.request({
    method: 'wallet_switchEthereumChain',
    params: [{ chainId: '0x1' }],
 })
like image 32
Ibad Shaikh Avatar answered Oct 22 '22 23:10

Ibad Shaikh