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

4902 error code #812

Closed
vyorkin opened this issue Jun 15, 2023 · 2 comments
Closed

4902 error code #812

vyorkin opened this issue Jun 15, 2023 · 2 comments
Assignees
Labels
pending Pending resolution. question Further information is requested team-devrel team-wallet-api-platform wallet Related to wallet content.

Comments

@vyorkin
Copy link

vyorkin commented Jun 15, 2023

HI! I was not able to find an EIP that mentions 4902 error code.
Here the latest code is 4901.

Ref: https://docs.metamask.io/wallet/reference/rpc-api/#returns-4

@alexandratran alexandratran added question Further information is requested wallet Related to wallet content. team-wallet-api-platform labels Jun 27, 2023
@vandan
Copy link
Contributor

vandan commented Jan 3, 2024

Relates to MetaMask/api-specs#104

@httpJunkie
Copy link
Contributor

httpJunkie commented Jan 18, 2024

I'm assuming that if you are getting an error code of 4902 that you are trying to programmatically switch a user to a different chain . The reason this code would come up (according to docs) is because you are switching to a chain that has not first been added.

Here is my solution based on what I normally do and what the docs suggest with some additional explanation.

As a dev, when you build a page that does something like switch a user to another chain, you might not find in manual or automated testing any issues with using code like the following which would switch someone to Linea:

await window.ethereum.request({
    method: 'wallet_switchEthereumChain',
    params: [{ chainId: 0xe704 }],
})

This will work for you and a lot of your users, until you encounter someone that maybe doesn't have an updated MetaMask that includes that chain by default or maybe that chainId is dynamic and most chains that can be switched to are default in MetaMask but others are not.

The problem is that you should never assume that the user will have the chain you want to switch them to. For instance, MetaMask has two default chains that cannot be removed: Ethereum Mainnet and Linea. However, there was a time when Linea did not exist and when it rolled out as a new default chain, not every user is updated at the same time.

Also there are other default chains that come with MetaMask like Polygon that as a user I can remove:
Screenshot 2024-01-18 at 9 28 57 AM

Therefore, from a programatic standpoint. I need to ensure that I put my wallet_switchEthereumChain code into a try/catch and if it fails use wallet_addEthereumChain.

The code in vanilla JS for switching to let's say "Polygon' looks something like this:

const addSwitchNetwork = async () => {
  if (provider) {
    try {
      await window.ethereum.request({
        method: 'wallet_switchEthereumChain',
        params: [{ chainId: '0x4' }]
      })
    } catch (error) {
      if (switchError.code === 4902) {
        try {
          await window.ethereum.request({
            method: 'wallet_addEthereumChain',
            params: [
              {
                chainId: '0x4',
                blockExplorerUrls: ['https://polygonscan.com/'],
                chainName: 'Polygon Mainnet',
                nativeCurrency: {
                  decimals: 18,
                  name: 'Polygon',
                  symbol: 'MATIC'
                },
                rpcUrls: ['https://polygon-rpc.com']
              }
            ]
          })

        } catch (error) {
          // user rejects the request to "add chain" or param values are wrong, maybe you didn't use hex above for `chainId`?
          console.log(`wallet_addEthereumChain Error: ${error.message}`)
        }
      }
      // handle other "switch" errors
    }
  }
}

In this code, we try the switch and if it doesn't exist we catch that 4902 code and add the chain and it will automatically switch them after the add. Try it out and LMK if this fixes your problem.

Also, if you need a tool to bookmark that does the conversion of Numeric to Hex for ChainId, check out:
eserialize

@alexandratran alexandratran added the pending Pending resolution. label Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending Pending resolution. question Further information is requested team-devrel team-wallet-api-platform wallet Related to wallet content.
Projects
None yet
Development

No branches or pull requests

4 participants