How to Make a Custom Chain Connect Button With Ethers.js

Viewed 1340

I want to make a connect to custom network like that at the bottom of arbiscan.io

How would I do this using ethers.js framework?

Clicking the button should add arbitrum network to user's Metamask.

1 Answers

you should use ethereum library:

async function changeNetwork(){
await ethereum.request({ method: 'wallet_addEthereumChain',
   params:[
      {chainId: "IN_HEXADECIMAL_FORM",
      chainName: "CHAIN_NAME",
      nativeCurrency: {
         name: "NATIVE_CURRENCY_NAME",
         symbol: "NATIVE_CURRENCY_SYMBOL",
         decimals: NATIVE_CURRENCY_DECIMALS //In number form
      },
      rpcUrls: ["BLOCKcHAIN_RPC_URL"],
      blockExplorerUrls?: ["BLOCKCHAIN_EXPLORER"]
   ]});
}

for more info see this document

Related