alchemy
alchemy copied to clipboard
Eliminate polling for wallet account change
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.
You should be very careful about relying on any injected provider. Use the provider returned by Web3Modal.
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?
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
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?
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.