6

I'm not sure if this question makes sense or if I'm looking at things correctly but it's coming from my understanding that ethersjs is an abstraction on top of MetaMask (and other providers)...

I've been able to get events from ethereum.on('chainChanged') and ethereum.on('accountsChanged') but I'm wondering if there is a way to do this through the ethersjs events or interface.

1

4 Answers 4

1

I don't see any official document for that in https://docs.ethers.io/

The way I do is to listen to chainChanged event directly from external Web3provider.

ethers.providers.Web3Provider(window.ethereum).provider.on('chainChanged')

ethers.providers.Web3Provider(window.ethereum).provider.on('accountsChanged')

1

Check out this link for different etherjs events and event types:

https://docs.ethers.io/v5/api/providers/provider/#Provider--event-methods

You can also check this link: https://github.com/ethers-io/ethers.js/blob/master/packages/providers/src.ts/base-provider.ts#L2047

1

You can initialize ethers and set a provider variable

const provider = new ethers.providers.Web3Provider(window.ethereum);

then you add/remove events by

const { provider: ethereum } = provider;
ethereum.on('accountsChanged', someFunction}; 
ethereum.off('accountsChanged', someFunction};
5
  • 1
    Thanks for this example. I was struggling with the way to listen with a Web3Provider instance. I checked this page docs.ethers.io/v5/concepts/best-practices/#best-practices and I tried to listen for accounts change like this provider.on('accountsChanged', (accounts) => { console.log(accounts) }) but It was not working I had to do: provider.provider.on(...). Do you know why it works with the network event ?
    – miko7358
    Commented Mar 23, 2022 at 9:13
  • 2
    @AdrienG I believe because the 'network' event is an event on the ethers Web3Provider object. The 'accountChanged' event is on the window.ethereum object so you have to access it by provider.provider.on. Commented Mar 23, 2022 at 10:07
  • 1
    That makes sense ! thanks for your explanation ;)
    – miko7358
    Commented Mar 23, 2022 at 10:57
  • Where can I find a list of events ethers.js supports? Searching accountsChanged in their documentation does not find anything at all, lol.
    – Qwerty
    Commented Mar 31, 2022 at 15:52
  • 3
    @Qwerty ethers just wraps a web3Provider, like window.ethereum. So you can still access all the expected EIP-1193 events by accessing provider.provider.ethereum. Now, ethers.js has some events of their own which can be found here docs.ethers.io/v5/api/providers/provider/…. You can register your "accountsChanged" handler without ethers. Commented Apr 1, 2022 at 5:02
0

I had the same question, glad to see some talk of it, I really wish Ethers would be explicit about this and state that they DO NOT handle certain events.

The way I have them working is to target the actual MetaMask provider itself. The syntax when using ethers should be something like


Provider["provider"].on( event, callback )

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.