menu
announcement

Spectrum is now read-only. Learn more about the decision in our official announcement.

Truffle Suite

Sweet tools for smart contracts

Channels
Team

Decoding events within truffle test tx receipt

February 17, 2021 at 12:02pm

Decoding events within truffle test tx receipt

February 17, 2021 at 12:02pm
I'm trying to look at the emitted events from a transaction within some tests I'm running, but it seems like they're not being decoded properly. Instead of an 'event' and 'args' field, I'm only seeing 'topics' with some hex values. From a Stack Overflow post it seems like this might happen with external contracts, but the events in question are defined and emitted within the contract where the transaction is executed. Additionally, when I add --show-events, I can see the events report the correct information with event names and args e.t.c. It just doesn't seem to be in this form in the .logs array of the result. This is happening in Truffle 5.1.49 and 5.1.50. Any advice? Cheers!

February 17, 2021 at 1:44pm
For now I'm using .getPastEvents on the contract which seems to be returning the 'event' and 'args' fields I'm after.

February 17, 2021 at 9:19pm
Hi, could you elaborate on this a bit? I'm a little confused as to exactly what you're doing
Like, it sounds like you're talking about the structure of some object. what object exactly? how are you obtaining it?

February 18, 2021 at 10:46am
Oh sorry, yeah I could've been much more clear. I'm looking at the events in .logs on the result that's returned from running a transaction on a contract.

February 18, 2021 at 5:56pm
Sorry, I still need more information here. Like -- different ways of invoking a transaction will return results in different forms. I'm going to guess from what you say that you're probably using the Truffle contract abstraction and invoking things that way, but sometimes we get people who are doing things, like, say, getting the underlying Web3 contract abstraction and using that instead, or directly sending a transaction or fetching a receipt with web3 or ethers. And obviously all of this is going to affect the result! So I wanted to be certain of exactly what you were doing here.
Hi, I can confirm I'm using the Truffle contract abstraction, as I'm using artifacts.require('MyContract') and then MyContract.new() on this. I've just noticed it might be something to do with the '--stacktrace' option which I had set while running tests, it seems like the response object from executing a transaction with and without the option is quite different.
Just confirming, it looks like the '--stacktrace' option is the culprit. The events display as expected when not using it. Is this expected? The response objects also have quite different fields.
With --stacktrace: { to: '0x57Dae64f612f50D10381476fAafd625fB3552652', from: '0x821aEa9a577a9b44299B9c15c88cf3087F3b5544', contractAddress: null, transactionIndex: 0, gasUsed: BigNumber { _hex: '0x73df' }, logsBloom: '...', blockHash: '0xfb3050693a87cf8d9ab01d690036c769b7801805426bf1ae01f9bc77d1d090b8', transactionHash: '0xc084cac0c1b152406514758dfbc406eb8a9f07a96a33dcbb5cec5b508f87c9ab', logs: [], blockNumber: 17, confirmations: 1, cumulativeGasUsed: BigNumber { _hex: '0x73df' }, status: 1, byzantium: true }
Without --stacktrace: { tx: '0xc084cac0c1b152406514758dfbc406eb8a9f07a96a33dcbb5cec5b508f87c9ab', receipt: { transactionHash: '0xc084cac0c1b152406514758dfbc406eb8a9f07a96a33dcbb5cec5b508f87c9ab', transactionIndex: 0, blockHash: '0x2a6cf25e8f749c2541d3acf95afa5a8d6f8aa0e39f31969e5e15d9b945be68a3', blockNumber: 17, from: '0x821aea9a577a9b44299b9c15c88cf3087f3b5544', to: '0x57dae64f612f50d10381476faafd625fb3552652', gasUsed: 29663, cumulativeGasUsed: 29663, contractAddress: null, logs: [], status: true, logsBloom: '...', rawLogs: [] }, logs: [] }
(Note: I've taken out the logs, I figured the difference in fields would be a better hint)
I see! very interesting. yes --stacktrace messes with how things work a bit, but this shouldn't happen... it's supposed to keep things essentially compatible. would you mind filing this as a bug on GitHub? ( https://github.com/trufflesuite/truffle/issues/new ) I'll have to look into what's causing this. In the meantime if you need a workaround you could try downgrading to Truffle 5.1.65... I'm pretty sure I know which PR introduced this, but I'll have to figure out what went wrong there. Thanks for bringing this to our attention!
OK, I've put up this PR to fix the problem: https://github.com/trufflesuite/truffle/pull/3816 hopefully it can make it into tomorrow's release! otherwise it should go out next week

February 19, 2021 at 12:36pm