How to get arguments from an emitted event in hardhat?

72 views Asked by At

I've been trying to get the arguments of an event that was emitted from a contract function call. But had no luck so far.

based on what i've found something like this should work:

  const tx = await eventEmitter.emitBothEvents(42, "foo");

  const receipt = await tx.wait()

  for (const event of receipt.events) {
    console.log(`Event ${event.event} with args ${event.args}`);
  }

(https://github.com/fvictorio/hardhat-examples/blob/master/reading-events/scripts/getEventsFromTx.js)

But receipt.events is always undefined no matter what function i call. So.. been there any updates to this or something? any other ways i can achieve this?

1

There are 1 answers

1
The matador On

found a solution:

const tx = await myContract.myFunction()
  result = await tx.wait()

  function findEventArgs(logs, eventName) {
    let _event = null;
  
    for (const event of logs) {
      if (event.fragment && event.fragment.name === eventName) {
        _event = event.args;
      }
    }
    return _event
  }

  console.log(findEventArgs(result.logs, "myEventName"))