blocknative / web3-onboard

Client library to onboard users to web3 apps
https://onboard.blocknative.com/
MIT License
833 stars 493 forks source link

[Feature]: Add Polygon RPC settings to Metamask if not present #791

Closed jadbox closed 2 years ago

jadbox commented 2 years ago

Is your request related to a problem?

Our platform uses the Polygon network and most users have not added Polygon to their Metamask account.

Feature Description

When a user selects Metamask and the client requests network ID 137 or 80001, there's an API that Onboard can call to add the network if it doesn't exist.

Docs to prompt to add network: https://docs.metamask.io/guide/rpc-api.html#wallet-addethereumchain

Related discussion: https://forum.moralis.io/t/automatically-populating-metamask-network-settings/1496

Polygon RPC network settings for Metamask: https://docs.polygon.technology/docs/develop/metamask/config-polygon-on-metamask/

Example code that tells Metamask to connect to a network ID and adds the Network RPC if it doesn't exist for the client:

try {
  await ethereum.request({
    method: 'wallet_switchEthereumChain',
    params: [{ chainId: '0xf00' }],
  });
} catch (switchError) {
  // This error code indicates that the chain has not been added to MetaMask.
  if (switchError.code === 4902) {
    try {
      await ethereum.request({
        method: 'wallet_addEthereumChain',
        params: [
          {
            chainId: '0xf00',
            chainName: '...',
            rpcUrls: ['https://...'] /* ... */,
          },
        ],
      });
    } catch (addError) {
      // handle "add" error
    }
  }
  // handle other "switch" errors
}

Alternative Solutions

na

Anything else?

No response

jadbox commented 2 years ago

Optionally: This could be configurable too to allow other network provides other than Polygon to get added by allowing the user to pass in configuration options for the RPC if it needs to be added.

taylorjdawson commented 2 years ago

@jadbox Are you asking for this to be built in? You should be able to do this now using the provider that onboard returns

jadbox commented 2 years ago

@taylorjdawson While that could be possible, the user is presented with a "change your wallet" to a network they do not have configured in their MetaMask. The ideal useflow is the user should be first prompted to add the network and then get a prompt to change their wallet to it. I've submitted a PR that does attempt to first auto add the Polygon network.

PR: https://github.com/blocknative/onboard/pull/792

taylorjdawson commented 2 years ago

@jadbox Ahh I understand now, I like that idea! I will circle back with the team to discuss. This may be something we wait to add in v2.

taylorjdawson commented 2 years ago

@jadbox This is added in v2! So it looks like we won't add it to v1 -- sorry for any inconvenience!

jadbox commented 2 years ago

@taylorjdawson Okay thanks for the heads up. When is v2 ready? I guess I'll stick on my fork with this feature until v2 is ready for production.

jadbox commented 2 years ago

@taylorjdawson Also are there docs on how to achieve this in v2 I can look at? Or an example? Or a v2 PR for the feature to inspect?