MetaMask / metamask-extension

:globe_with_meridians: :electric_plug: The MetaMask browser extension enables browsing Ethereum blockchain enabled websites
https://metamask.io
Other
12k stars 4.91k forks source link

[Bug]: `wallet_switchEthereumChain` never emits `chainChanged` if `net_version` RPC is disabled #24247

Open holic opened 6 months ago

holic commented 6 months ago

Describe the bug

I have a chain where the old RPC method net_version was not enabled/whitelisted by default and we noticed that MetaMask switching chains to this network would not emit the chainChanged event that tools like wagmi depend on.

Expected behavior

I would expect MetaMask to use eth_chainId for this initial call, or at least fallback to it, so chainChanged can be properly emitted in cases where net_version is not supported by the RPC.

Screenshots/Recordings

Steps to reproduce

Add a chain where the RPC method net_version is disabled. Specifically, ours returned

{"error":{"code":-32610,"message":"rpc method is not whitelisted"},"id":9999,"jsonrpc":"2.0"}

Listen for chainChanged events with:

window.ethereum.on('chainChanged', (chainId) => console.log('MM switched to chain', chainId));

Then call wallet_switchEthereumChain to the target chain. You'll see the RPC call return but you won't see an event emission, even though the MetaMask extension will have switched to that chain.

Error messages or log output

Unclear where to find errors/logs in MetaMask extension.

Version

11.14.1

Build type

None

Browser

Brave

Operating system

MacOS

Hardware wallet

No response

Additional context

No response

Severity

No response

holic commented 6 months ago

I was previously seeing an issue with deleting these custom networks but turns out MetaMask won't let you delete a network you're currently connected to. I was able to delete it after switching to another network.

holic commented 6 months ago

Okay, narrowed this down to MetaMask not handling a lack of net_version RPC method. Updated the issue description.

vandan commented 6 months ago

from @jiexi :

https://github.com/MetaMask/providers/blob/4f12239247b30a03884df5c6623429d215e78e4f/src/StreamProvider.ts#L198-L202 networkVersion ends up being sent to the provider as loading if net_version fails which is explicitly guarded against in the lines linked in Provider

vandan commented 6 months ago

Possibly related to: https://github.com/MetaMask/metamask-extension/pull/20652

github-actions[bot] commented 3 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 45 days if there is no further activity. The MetaMask team intends on reviewing this issue before close, and removing the stale label if it is still a bug. We welcome new comments on this issue. We do not intend on closing issues if they report bugs that are still reproducible. Thank you for your contributions.

github-actions[bot] commented 3 days 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 45 days if there is no further activity. The MetaMask team intends on reviewing this issue before close, and removing the stale label if it is still a bug. We welcome new comments on this issue. We do not intend on closing issues if they report bugs that are still reproducible. Thank you for your contributions.