Ethers.js: How to retrieve transactionHash while an event was emitted?

Created on 18 Feb 2021  路  2Comments  路  Source: ethers-io/ethers.js

I'm listening on a contract with

contract.on('EventName', async (arg) => {
  doSomething(arg)
});

Is it possible to retrieve more info about the transaction where the event was emitted, like the hash?

I tried something like:

contract.on('EventName', async (transactionHash, arg) => {
  console.log(transactionHash);
  doSomething()
});

With that, the program crashes but I can see the infos I want in the stack trace. However, how can I access them in the correct way?

discussion

Most helpful comment

The args of the Listener function are in the following fashion:

contract.on('EventName', async (arg1, arg2, ..., argLast, event) => {
  console.log(event.transactionHash);
});

If you are not sure how many args gonna be, you can do a generalised way:

contract.on('EventName', async (...args) => {
  console.log(args[args.length - 1].transactionHash);
});

All 2 comments

The args of the Listener function are in the following fashion:

contract.on('EventName', async (arg1, arg2, ..., argLast, event) => {
  console.log(event.transactionHash);
});

If you are not sure how many args gonna be, you can do a generalised way:

contract.on('EventName', async (...args) => {
  console.log(args[args.length - 1].transactionHash);
});

@zemse Many thanks. That was exactly what I was searching for. This issue can be closed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pointtoken picture pointtoken  路  24Comments

bpierre picture bpierre  路  43Comments

282931 picture 282931  路  42Comments

ruzpuz picture ruzpuz  路  26Comments

fmsouza picture fmsouza  路  51Comments