How to trigger change blockchain network request on MetaMask

0 votes

I am using web3 for my first app test and I'd like to make it so MetaMask will prompt the user to change the network to Binance (BSC) network if it is not selected already, just like here:

metamask requesto change network

How to trigger such a request?

Mar 29, 2022 in Blockchain by Soham
• 9,710 points
1,020 views

1 answer to this question.

0 votes

was finally able to find the answer:

await window.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x61' }], // chainId must be in hexadecimal numbers });
A more comprehensive answer would check if MetaMask is installed and if this one has installed the chain your Dapp wants to connect to, and if it is not install it:

// Check if MetaMask is installed // MetaMask injects the global API into window.ethereum if (window.ethereum) { try { // check if the chain to connect to is installed await window.ethereum.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x61' }], // chainId must be in hexadecimal numbers }); } catch (error) { // This error code indicates that the chain has not been added to MetaMask // if it is not, then install it into the user MetaMask if (error.code === 4902) { try { await window.ethereum.request({ method: 'wallet_addEthereumChain', params: [ { chainId: '0x61', rpcUrl: 'https://data-seed-prebsc-1-s1.binance.org:8545/', }, ], }); } catch (addError) { console.error(addError); } } console.error(error); } } else { // if no window.ethereum then MetaMask is not installed alert('MetaMask is not installed. Please consider installing it: https://metamask.io/download.html'); }
answered Mar 31, 2022 by Rahul
• 9,680 points

Related Questions In Blockchain

0 votes
1 answer

How to change "gasLimit" on Azure private blockchain?

Before deploying your network on Azure you ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,220 points
867 views
0 votes
1 answer

How to create a new wallet on a blockchain?

Firstly, you need to have a clear ...READ MORE

answered Apr 4, 2018 in Blockchain by Christine
• 15,790 points
1,415 views
+1 vote
1 answer
0 votes
1 answer

How can i create a simple P2P network on a blockchain?

To develop your own cryptocurrency, you need ...READ MORE

answered Apr 20, 2018 in Blockchain by Christine
• 15,790 points
2,048 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,942 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,468 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,459 views
0 votes
1 answer

how Localbitcoins and other bitcoin wallet able to send/receive without network fee

Organizations that offer online wallets aren't obligated ...READ MORE

answered Mar 31, 2022 in Blockchain by Rahul
• 9,680 points
584 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP