-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Comments
Relates to MetaMask/api-specs#104 |
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: Therefore, from a programatic standpoint. I need to ensure that I put my 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: |
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
The text was updated successfully, but these errors were encountered: