1

I have a contract with the getOwnerAddress function that emits a OwnerReturned event:

...
function getOwnerAddress() external returns (address) {
    emit OwnerReturned("Owner");
    return owner;
}
event OwnerReturned(string message);
...

I'm trying to check whether a transaction emitted an OwnerReturned event:

let ownerTransaction = await contract.getOwnerAddress({
    nonce:  await signer.getNonce()
});
let ownerResult = await ownerTransaction.wait();
let ownerEvents = ownerResult.events;
if (ownerEvents !== undefined)  {
    let ownerReturnedEvent = ownerEvents.find(e => e.event === 'Owner');
    console.log(ownerReturnedEvent);
} else  {
    console.log("Undefined events!");
}

The transaction executes, however the ownerEvents is undefined.

Am I doing something wrong? How do I check if the transaction had OwnerReturned event emitted?

1
  • What ethers.js version are you using?
    – Mila A
    Commented Feb 25 at 16:34

1 Answer 1

1

If you're using ethers.js ^= v6, the issue is no surprise.

You'd need to access the logs property of the transaction receipt for reading the events that were emitted during the transaction execution.

Like this:

tx = await connectedToken.transfer(receiver.address, amount)
result = await tx.wait()
console.log('transaction events', result.logs);

In your particular case, it's equivalent to the following code snippet:

let ownerTransaction = await contract.getOwnerAddress({
    nonce:  await signer.getNonce()
});
let ownerResult = await ownerTransaction.wait();
let ownerEvents = ownerResult.logs;

if (ownerEvents !== undefined)  {
    let ownerReturnedEvent = ownerEvents.find(e => e.topics[0] === 'Owner');
    console.log(ownerReturnedEvent);
} else  {
    console.log("Undefined events!");
}

txReceipt.logs is an array readonly Log[] (https://docs.ethers.org/v6/api/providers/#Log)

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.