3

I have indexed some values on my events. When I run tests, why don't they show up ?

Example :

Events emitted during test:
---------------------------

Upgrade(_tokenId: <indexed>, _upgradeType: 1)

---------------------------

Why isn't it showing the token id?

Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
M. Mez
  • 75
  • 1
  • 8

2 Answers2

0

The callback return object is different depending on if you create a global filter or a contract filter. You'd need to add your client code to give a better answer, but this looks like a global filter whose response separates out the event data (non-indexed fields) from the topics (the indexed fields). From the documentation:

  • data: String - contains one or more 32 Bytes non-indexed arguments of the log.
  • topics: Array of Strings - Array of 0 to 4 32 Bytes DATA of indexed log arguments. (In solidity: The first topic is the hash of the signature of the event (e.g. Deposit(address,bytes32,uint256)), except if you declared the event with the anonymous specifier.)

With a contract filter, you can get to each argument using args regardless if it's indexed or not.

Example:

var contractFilter = contractInstance.Upgrade({}, (e, r) => {
  console.log(r.args._tokenId);
});
Adam Kipnis
  • 10,175
  • 10
  • 35
  • 48
0

My truffle-assertions library can log all emitted events inside a transaction in the same way as truffle normally outputs them on error, except that it also displays indexed arguments' values correctly.

The library can be installed through npm

npm install truffle-assertions

Then, it can be imported at the top of your test file

const truffleAssert = require('truffle-assertions');

And finally it can be used to output all emitted events inside a transaction.

let result = contractInstance.function({from: account[0]});
truffleAssert.prettyPrintEmittedEvents(result);
Rosco Kalis
  • 567
  • 3
  • 13
  • how do you make it work particular to this library, if the solidity function is a get function with a return value+ a event emit. The first param in ```truffleAssert.eventEmitted``` wants a tx receipt , but You would get a result value.... – 0xAnon May 24 '21 at 05:33
  • 1
    You can't meaningfully emit events in a view function. See https://ethereum.stackexchange.com/questions/46198/emit-event-in-view-function-or – Rosco Kalis May 25 '21 at 07:21