alchemy icon indicating copy to clipboard operation
alchemy copied to clipboard

Eliminate polling for wallet account change

Open roienatan opened this issue 4 years ago • 5 comments

Currently we poll every 2000ms for account change in MetaMask. Instead, we can use a listener for account changes, like this:

window.ethereum.on("accountsChanged", (accounts: Array<any>) => {
  //Handle the new accounts.
  //"accounts" will always be an array, but it can be empty.
  //The current account is at accounts[0]
});

See also https://docs.metamask.io/guide/ethereum-provider.html#events.

roienatan avatar Dec 01 '20 12:12 roienatan

You should be very careful about relying on any injected provider. Use the provider returned by Web3Modal.

dkent600 avatar Dec 01 '20 15:12 dkent600

You should be very careful about relying on any injected provider. Use the provider returned by Web3Modal.

mm but where the injected provider come from? It's according the wallet you choose?

roienatan avatar Dec 01 '20 15:12 roienatan

You should be very careful about relying on any injected provider. Use the provider returned by Web3Modal.

mm but where the injected provider come from? It's according the wallet you choose?

Yes, from the wallet you choose in the Web3Modal UI

dkent600 avatar Dec 01 '20 16:12 dkent600

You should be very careful about relying on any injected provider. Use the provider returned by Web3Modal.

mm but where the injected provider come from? It's according the wallet you choose?

Yes, from the wallet you choose in the Web3Modal UI

So, if the injected web3 is set according the Web3ModalUI, what's the issue here?

roienatan avatar Dec 01 '20 16:12 roienatan

Sorry I wasn't clear: Web3Modal doesn't inject anything. It returns the provider (you can see this in arc.ts).

Metamask has in the past injected a provider, but is ending that practice.

dkent600 avatar Dec 01 '20 16:12 dkent600