I'm writing a user interface in Javascript and I'm using the web3.js library.

I have the transaction hash of a confirmed transaction. I would like to get an array of all events emitted by that transaction.

I don't necessarily need all events, just the ones emitted by my smart contract by the execution of that transaction.

Can this be done using web3.js? What's a good way to accomplish this?


This is a bit convoluted since Web3 does not directly expose events emitted by a transaction, but it can be queried by tracing the transaction.

  1. Trace transaction : https://github.com/ethereum/go-ethereum/wiki/Management-APIs#debug_tracetransaction
  2. It will return a block of info for each instruction of transaction. (PUSH1 in this example)

{ depth: 1, error: "", gas: 162106, gasCost: 3, memory: null, op: "PUSH1", pc: 0, stack: [], storage: {} }

  1. Filter for opcodes LOG0 -- LOG4. These instructions are used for raising events.

enter image description here

  1. Decode Event name from the data that is part of OPCODE block

So far, I've solved this in a slightly dirty way:

  1. Use getTransaction(txhash) to get the block number in which the transaction was confirmed.

  2. Use getPastEvents("allEvents", {fromBlock: blockNumber, toBlock: blockNumber}) to get all events emitted by my contract in that block

  3. Filter out the events with the correct txhash

I would prefer not to fetch and loop over all events in a block just to get the ones emitted by one transaction, so I'm not going to accept my own answer.

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.