Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request : Add Custom RPC To Metamask #15

Open
scco opened this issue Oct 27, 2021 · 0 comments
Open

Feature Request : Add Custom RPC To Metamask #15

scco opened this issue Oct 27, 2021 · 0 comments

Comments

@scco
Copy link

scco commented Oct 27, 2021

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;
      }
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant