Web3JS – Handling Contract Events with Truffle

eventstruffletruffle-contractweb3js

It seems that there is an issue with handling of contract events and I am not sure if it is at the EVM level or just web3js.

I have 2 contracts "SomeCoin" and "SomeConcert". Both contracts emit "Transfer" event when there is a transfer of item from one address to another. For "SomeCoin" the object of transfer is a coin/token, for "SomeConcert" the object of transfer is concert tickets (that is priced in "SomeCoins").

When I called "SomeConcert" method to purchase tickets, the logic will transfer some "SomeCoins" from my address to the "SomeConcert" address. All is well and good except for the event handling. I saw 2 events emitted "Buy" event emitted from "SomeConcert" contract and "Transfer" event from "SomeCoin" contract. If I renamed the "Transfer" event in "SomeCoin" contract to "TransferCoin" then I will only see the "Buy" event.

So, apparently either web3 or the underlying layer confuses the events emitted by inner layer contract with the outer layer contract. At least this is the conclusion I have come to.

Is this a web3js bug? Is there anything I missed out in the event handling that might have prevented the confusion. The code I have for event handling is rather standard


...
return someConcertInstance.buyTickets(3, {from: customer1});
}).then(function(receipt) {
assert.equal(receipt.logs.length, 1, "should have received 1 events for buy-ticket)");
assert.equal(receipt.logs[0].args._numberOfTickets, 3, "Number of tix bought must be 3");

The test case failed on the first assert and I found out 2 events are emitted (transfer and buy).

Best Answer

Sorted out in the comments above, moving to an answer for future readers.

The code in the question is using truffle. When sending a transaction in truffle, the result you get back has a logs field, which is assembled via this code: https://github.com/trufflesuite/truffle-contract/blob/develop/contract.js#L44.

That code discards logs with topics that don't match a known event signature in the ABI of the contract you're calling. I consider that behavior to be a design bug.

Related Topic