MetaMask / metamask-mobile

Mobile web browser providing access to websites that use the Ethereum blockchain
https://metamask.io
Other
2.17k stars 1.12k forks source link

Metamask wallet_switchEthereumChain does not throw error code #3312

Closed dojh111 closed 9 months ago

dojh111 commented 3 years ago

Describe the bug The Metamask RPC API Provider method wallet_switchEthereumChain as stated here is supposed to throw the error code 4902 when the ethereum chain to switch to does not exist.

This however, does not happen on metmask mobile for both IOS and Android, while working fine on the desktop extension for Metamask.

To Reproduce Steps to reproduce the behavior

Use the code as stated in the official Metamask documentation, and visit the site with the Metamask Mobile dApp browser:

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', rpcUrl: 'https://...' /* ... */ }],
      });
    } catch (addError) {
      // handle "add" error
    }
  }
  // handle other "switch" errors
}

Expected behavior When joining a new network that has not been added before, the popup to add new ethereum chain does not show up, when it should.

Gameluo commented 3 years ago

When I use this method to add BSC networks on mobile platforms, nothing happens when I call MetaMask. Is this method not supported in the current version of mobile platforms?

dojh111 commented 3 years ago

@Gameluo I faced the same problem as you, and found out that if you have the line of code:

switchError.code === 4902

you have to remove it to make the thing work. This problem only affects mobile platforms and not the desktop version

alexdupre commented 2 years ago

Metamask mobile returns a generic code -32603, instead of 4902 as MM extension :-(

andrepimenta commented 2 years ago

We are working on fixing this, but as a workaround you can use if(switchError.code === 4902 || switchError?.data?.orginalError?.code === 4902) and should work on mobile as well

julien51 commented 1 year ago

Metamask mobile returns a generic code -32603, instead of 4902 as MM extension :-(

Indeed. We found the same thing...

https://github.com/unlock-protocol/unlock/pull/10491

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 7 days with no activity. If you feel this was closed in error please reopen and provide evidence on the current production app. Thank you for your contributions.

alexdupre commented 1 year ago

Come on, how difficult can it be to fix it?

mightymatth commented 1 year ago

Come on, how difficult can it be to fix it?

Exactly, seems like a total neglect.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity in the last 90 days. It will be closed in 7 days. Thank you for your contributions.

mightymatth commented 1 year ago

ofc there is no activity because we're waiting for someone from hundreds of employees to fix this bug. if we wait a little bit more, maybe the bug will get annoyed and fix itself on its own.

github-actions[bot] commented 10 months ago

This issue has been automatically marked as stale because it has not had recent activity in the last 90 days. It will be closed in 7 days. Thank you for your contributions.

github-actions[bot] commented 9 months ago

This issue was closed because there has been no follow activity in 7 days. If you feel this was closed in error please provide evidence on the current production app in a new issue or comment in the existing issue to a maintainer. Thank you for your contributions.