9

I need to listen to the specific contract events and do some task once the event is emitted. I didn't find proper docs on ethers.js documentation.

Can anybody tell me how can I keep listening to my contract events using ethers.js?

9

This answer assumes that you understand how to connect to a contract using Ethers.

Your question specifies listening for an event to be emitted, and to do a task based on that.

Here is the link to the Ethers v5 documentation, which we'll expand on a bit below: https://docs.ethers.io/v5/api/providers/provider/#Provider--events

(Ethers v4 is here and here)

Basically, the on function is what you're looking for. Using a provider, you can set up an on to trigger something whenever a particular event is emitted. This will trigger the callback (the second argument in the on, a function) anytime that event is emitted on the blockchain. If you are looking for the on to be triggered only when the events of a particular contract are called, you'll need to use a filter - the provider will trigger the callback anytime this event is emitted from any contract.

If you look at the example in the link above to the v5 docs (at least right now 22/09/2020), towards the bottom there are examples of how to use filters (either a general filter or a topic filter - we'll discuss the first). Basically:

filter = {
    address: THE_ADDRESS_OF_YOUR_CONTRACT,
    topics: [
        // the name of the event, parnetheses containing the data type of each event, no spaces
        utils.id("Transfer(address,address,uint256)")
    ]
}
provider.on(filter, () => {
    // do whatever you want here
    // I'm pretty sure this returns a promise, so don't forget to resolve it
})

There are many more details about how and what you can filter in the links above, but this is the fundamental building block. I hope this helps!

Update: Based on a comment, I thought I should add that on can also be used with a contract object. If you have a variable representing a contract (which we'll call contract), you can also hook an on onto it. Here's a link to a section in the v4 docs which has an example (which at least as of now isn't in the v5 docs yet) - scroll down to the "listening to an Event" code example. It has the same format where you pass in an event name or filter for the first arg, and then can write a function callback as the second. This will only return events from contract.

5
  • I need WebSocket Provider for that right? I get this error when i tried websocket with ganache ``` Error: VM Exception while processing transaction: revert at WebSocketProvider._this._websocket.onmessage (/home/manzik/esatya/rahat/hapi/server/node_modules/@ethersproject/providers/lib/websocket-provider.js:115:37) ``` Sep 23 '20 at 3:23
  • Also, Can you help me with this - docs.ethers.io/v5/api/contract/contract How can i use "contract.on(event,listener)" - what should I put in event there Sep 23 '20 at 3:26
  • Pro tip: If you find the v5 docs hard to read on anything, look at the v4 docs, and try to figure out if it's still the same: docs.ethers.io/v4/… Scroll down a bit, look at the code example of "Listening to Events" - you'll see it works the same way as the provider example here. Sep 23 '20 at 10:19
  • @MYANZIKshrestha I updated the answer too! Sep 23 '20 at 10:25
  • @MYANZIKshrestha I only noticed the WebSocket question now. I can't figure out what's wrong from the error - it might need a new question with examples of your code, though I'm not sure. Sep 23 '20 at 11:12
0

// event in ERC-20: Transfer(address indexed src, address indexed dst, uint val)

contract.on("Transfer", (to, amount, from) => {
    console.log(to, amount, from);
});

You can read here: https://docs.ethers.io/v5/api/contract/contract/#Contract--events

Also good examples here: https://docs.ethers.io/v5/concepts/events/

And discussion topic with more info: https://github.com/ethers-io/ethers.js/issues/492

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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