1

Usually, using truffle I can check for events like this:

    let { logs } = await myContract.doSomethingImportant( xxx );

    // 1500 tokens are expected to get
    expectEvent.inLogs(logs, 'myImportantEvent', {
      arg1: 111,
      arg2: 'and so on'
    });

However, logs contains only the events that was fired directly inside of the contract function withdrawVestedTokensByTimestamp:

function doSomethingImportant(uint256 x) public returns(uint256) {
    // ... do something else here ...

    emit myImportantEvent(x, amount);

    return amount;
}

However, if in this function a token transfer will happen or another function is called, which would fire events itself, they don't appear in the logs.

Running tests with truffle test --show-events will list all of the expected events, but how can I catch them inside truffle test?

1

The openzeppelin-test-helpers library that you are using can actually retrieve all events from a transaction. expectEvent.inLogs(logs, ...); actually scans logs for each event that was emitted during the transaction and will look for the event name (and values) that you specified.

In your case, you simply need to add another expectEvent.inLogs(logs, ...); to your test case that has the expected values from your other event. This will catch it and do exactly as you expect.

Additionally, since this expectEvent searches through all of the events in a transaction, you can actually have multiple of the same events get emitted in a single transaction. For example, if you had the line emit myImportantEvent(x + 1, amount); immediately after the first myImportantEvent, you could write a test and it would be able to pick up both of them.

  • But in my example, logs contains only the event of my own contract, not those events, that was dispatched internally thru calls to other contracts and their transfer-methods – delete Jun 14 at 6:59

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.