Skip to content

Feature Request : Add Custom RPC To Metamask #15

@scco

Description

@scco

Im not good with react , so maybe some of you guys already tweaked this app
or would be nice to help me out for some better UX on this app - by having
a simple way to add a custom RPC ( eg chain 137 polygon ) ?

i would think a simple way would be to just add a little code piece where that {blockchain.errorMsg}
appears right now - and add some
<button>switch network to provider...</button> t

hat triggers a function something like this :
( its from a vue project i am working in a bigger team but yea react / advanced js is not my core skill )

Thank you so much


  async function switchNetwork() {
    // Chain ID must be a 0x-prefixed hex string without paddingL https://eth.wiki/json-rpc/API
    const chainId = hexStripZeros(BigNumber.from(DGRANTS_CHAIN_ID).toHexString());

    // First try switching chains
    try {
      await rawProvider.value?.send('wallet_switchEthereumChain', [{ chainId }]);
    } catch (err) {
      const { code } = err as { code: number };

      // This error code indicates that the chain has not been added to MetaMask: https://docs.metamask.io/guide/rpc-api.html#other-rpc-methods
      // In this case, add the chain for the user
      if (code === 4902) {
        await rawProvider.value?.send('wallet_addEthereumChain', [
          {
            chainId,
            chainName: CHAIN_INFO.label,
            nativeCurrency: CHAIN_INFO.nativeCurrency,
            rpcUrls: [CHAIN_INFO.rpcUrl],
            blockExplorerUrls: [CHAIN_INFO.explorer],
          },
        ]);
      } else {
        throw err;
      }
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions