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

[Bug]: Network changed events fired with a delay #13375

Open
SpasiboKojima opened this issue Jan 21, 2022 · 26 comments
Open

[Bug]: Network changed events fired with a delay #13375

SpasiboKojima opened this issue Jan 21, 2022 · 26 comments

Comments

@SpasiboKojima
Copy link

Describe the bug

I hope I'm at the right place because it doesn't seem much of a bug, but maybe something can be done about it.
I'm connecting Metamask to my app, and right after user grants access I check the chainId, if it's not the one I expect I do wallet_switchEthereumChain, and if right after that I do login process again where this time it passes chainId check and subscriptions happen, and after going this far MM emits events disconnect or/and accountsChanged. Alternatively, without auto-login after chain switch if you click fast enough on login button again after network switch you'll catch this events.
BUT it doesn't happen if the target chain is mainnet, in my case it's Binance Smart Chain and it's Testnet that I'm testing.
Again, events related to network switch fire much later after it actually happened

Steps to reproduce

  1. Try to login into app with wrong network
  2. Have a wallet_switchEthereumChain request asking you to change network to the right one
  3. Click on login button again within 1.5 seconds or automatically try to connect through web3
  4. Have chainId check passed but still get an event related to changing networks much later

Error messages or log output

No response

Version

10.8.1

Build type

No response

Browser

Chrome, Firefox

Operating system

Linux

Hardware wallet

No response

Additional context

No response

@kevinghim kevinghim added area-customNetworks needs-reproduction Sev3-low Low severity; minimal to no impact upon users labels Jan 24, 2022
@mloit
Copy link

mloit commented Feb 2, 2022

I believe I'm seeing a related issue. Metamask (injected provider) disconnects whenever the chain is changed to the Binance Test Network. I can freely switch from it to any network, or between any other networks I've tried, but as soon as I switch back into the binance testnet instead of getting a chainChanged event, I get a disconnect event.

main.js:450 onChainChanged: 0x38
main.js:450 onChainChanged: 0x3
main.js:450 onChainChanged: 0x4
main.js:468 onDisconnect: Error: MetaMask: Disconnected from chain. Attempting to connect.
    at l._handleDisconnect (inpage.js:1:39598)
    at l._handleDisconnect (inpage.js:1:43404)
    at l._handleChainChanged (inpage.js:1:40205)
    at l._handleChainChanged (inpage.js:1:45320)
    at o.<anonymous> (inpage.js:1:37771)
    at i (inpage.js:1:54958)
    at o.emit (inpage.js:1:55498)
    at inpage.js:1:54022
    at f.write [as _write] (inpage.js:1:54045)
    at w (inpage.js:17:29414)

If I reload the page, I am connected just fine. It only breaks when I switch into the testnet from metamask.

@ErnoW
Copy link

ErnoW commented Feb 4, 2022

I can confirm that this is an issue with some Binance Smartchain Testnet RPCs.

When switching from any chain to Binance Smartchain Testnet, for some RPCs a disconnect event is fired. (and results in a delay)

This RPC results in the above error MetaMask: Disconnected from chain. Attempting to connect, but succeeds after the retry:

https://data-seed-prebsc-1-s1.binance.org:8545

This one seems to work without issues:

https://data-seed-prebsc-2-s3.binance.org:8545

Both are from the binance docs

@tmm
Copy link

tmm commented Feb 19, 2022

Can confirm this happens for other chains and is affecting wagmi library users.

To reproduce:

  1. Open CodeSandbox (https://vwngww.csb.app) and connect wallet
  2. Switch to any non MM default chain (Arbitrum One, Polygon Mainnet, etc.)

Using MM 10.9.3. cc @kevinghim

@vm06007
Copy link

vm06007 commented Mar 21, 2022

same issue observed, any solution?

@tmm
Copy link

tmm commented Apr 4, 2022

@kevinghim here's a very simple reproduction (using MetaMask 10.11.3) that you can run in the browser console. Let me know if you need any more info.

  1. Add event listener for disconnect event
window.ethereum.on('disconnect', () => alert('disconnect'));
  1. Connect account
await window.ethereum.request({ method: 'eth_requestAccounts' });
  1. Using a custom chain (i.e. Optimism), switch:
await window.ethereum.request({
  method: 'wallet_switchEthereumChain',
  params: [{ chainId: '0xa' }],
})

Or add and switch:

await window.ethereum.request({
  method: 'wallet_addEthereumChain',
  params: [
    {
      chainId: '0xa',
      chainName: 'Optimism',
      nativeCurrency: {name: 'Ether', symbol: 'ETH', decimals: 18},
      rpcUrls: ['https://mainnet.optimism.io'],
      blockExplorerUrls: ['https://optimistic.etherscan.io'],
    },
  ],
});
  1. After a successfully switching to the custom chain, disconnect event fires and browser alert is created.

@vm06007
Copy link

vm06007 commented Apr 4, 2022

is this still an issue or has been fixed?

@mqklin
Copy link

mqklin commented May 18, 2022

I have the same problem. Switching to custom network sometimes fires disconnect event.

@einaralex
Copy link

I've been having this issue for a while, that Metamask will disconnect when I switch to Mumbai.

I was certain this was a Metamask issue but found out today that I was missing an await, causing the changeChain event to be interrupted.

@elias-garcia
Copy link

Same problem here, we are experiencing this issue when switching from Goerli to another network... Is this planned to be fixed?

@einaralex
Copy link

@elias-garcia What other network? Ropsten just very recently got deprecated, could that be the issue?

@elias-garcia
Copy link

elias-garcia commented Oct 20, 2022

@elias-garcia What other network? Ropsten just very recently got deprecated, could that be the issue?

Hi @einaralex! We are experiencing it when switching from Goerli to the zkEVM Testnet in our bridge.

This happened live in Jordi's presentation at Devcon, you can take a look to the exact moment here:

https://www.youtube.com/watch?v=3A-LotQjc2k&t=5873s

You can see how after switching the network, the onDisconnect event is being fired and it's kicking out the user to the login page. The problem here is that we've not been able to reproduce the bug in a deterministic way yet. Looks like it's happening randomly, sometimes it doesn't fire, sometimes it fires.

Details of the zkEVM Testnet network just in case it's helpful for something:

RPC URL: https://public.zkevm-test.net:2083
Chain ID: 1402
Currency symbol: ETH

@platschi
Copy link

+1, we are experiencing the same issue when switching between Ethereum Mainnet and Optimism Mainnet.

@webdott
Copy link

webdott commented Oct 22, 2022

Experiencing the same issue switching from Polygon to Celo and vice versa

@turbowizardry
Copy link

It seems like this bug is hit every time you switch to a custom network. When I switch between the default Ethereum network loaded in MM and testnets, no disconnect happens. I added a custom Ethereum node with Alchemy as the RPC and now the Dapp disconnects when I switch to it, as well as any other custom network I've added (Optimism, ZKSync etc).

@0xhashmap
Copy link

Has anyone found a solution/workaround for this issue?

@istankovic
Copy link

Bump.

@0xflumedev
Copy link

Be fantastic to get this fixed. Still occurring today.

@aiiiden
Copy link

aiiiden commented Feb 21, 2023

Any Update?

@KarthickSakthi
Copy link

is this issue fixed? still occuring for me!

@michecode
Copy link

This is still broken for me but I was able to implement a workaround. I'm using web3modal but I assume you can just put this in whatever function handles your disconnect event.

const handleDisconnect = (error) => {
  // @DEV: see https://github.com/MetaMask/metamask-extension/issues/13375
  // 1013 indicates that MetaMask is attempting to reestablish the connection
  // https://github.com/MetaMask/providers/releases/tag/v8.0.0
  if (error.code === 1013) {
    console.warn('MetaMask logged connection error 1013: "Try again later"');
      return;
    }
  _disconnectWeb3(dispatch);
};

Stole this from Uniswap/web3-react#709 thank you @niZmosis

@Charlie85270
Copy link

+1, still have the issue

@wwhchung
Copy link

wwhchung commented May 5, 2023

Confirmed that this is still an issue. disconnect events are erroneously being emitted when chain switch happens. In our case, because we respect this event, we disconnect an authenticated account.

@Matko95
Copy link

Matko95 commented Jul 3, 2023

+1, still reproducible

@D-Laur
Copy link

D-Laur commented Aug 18, 2023

+1, issue still exits

@xinnanyemm
Copy link

Please note that label:team-dapp-api has been replaced by label:team-devex . Thanks.

@DarkProtocol
Copy link

+1 have same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests