12

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?

Jesbus
  • 10,478
  • 6
  • 35
  • 62
  • See also: https://ethereum.stackexchange.com/questions/16313/how-can-i-view-event-logs-for-an-ethereum-contract – Onshop Mar 10 '21 at 19:33

4 Answers4

10

If you're using Web3 v1 then you can use web3.eth.getTransactionReceipt(tx). It returns logs property as a part of a result.

You can categorize events by looking at their topics property.

Sample getTransactionReceipt result:

{
    "transactionHash": "0x7e2e90d913246933b30049c568d9a9768eca5be6fed331656458c2a479f30908",
    "transactionIndex": 0,
    "blockHash": "0x2af7bb564cc6a4155895b3238d5a0f227ecae2bfc5e65d24be5066f7a0e1b035",
    "blockNumber": 127,
    "from": "0x9442ed348b161af888e6cb999951ae8b961f7b4b",
    "to": "0xf1365f7f85c20885cab2472d36c59861bcfbd071",
    "gasUsed": 298630,
    "cumulativeGasUsed": 298630,
    "contractAddress": null,
    "logs": [
        {
            "logIndex": 0,
            "transactionIndex": 0,
            "transactionHash": "0x7e2e90d913246933b30049c568d9a9768eca5be6fed331656458c2a479f30908",
            "blockHash": "0x2af7bb564cc6a4155895b3238d5a0f227ecae2bfc5e65d24be5066f7a0e1b035",
            "blockNumber": 127,
            "address": "0xF1365F7F85C20885caB2472d36c59861BCfbD071",
            "data": "0x",
            "topics": [
                "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                "0x0000000000000000000000000000000000000000000000000000000000000000",
                "0x0000000000000000000000005b1b89a48c1fb9b6ef7fb77c453f2aaf4b156d45",
                "0x0000000000000000000000000000000000000000000000000000000000000000"
            ],
            "type": "mined",
            "id": "log_3b3793c3"
        },
        {
            "logIndex": 1,
            "transactionIndex": 0,
            "transactionHash": "0x7e2e90d913246933b30049c568d9a9768eca5be6fed331656458c2a479f30908",
            "blockHash": "0x2af7bb564cc6a4155895b3238d5a0f227ecae2bfc5e65d24be5066f7a0e1b035",
            "blockNumber": 127,
            "address": "0xF1365F7F85C20885caB2472d36c59861BCfbD071",
            "data": "0x00000000000000000000000000000000000000000000000000000000000dbba00000000000000000000000005b1b89a48c1fb9b6ef7fb77c453f2aaf4b156d45",
            "topics": [
                "0xc0300f89fa66c737609c90575f912c101a539bdc6659f1562ba9b0868f8d181d",
                "0x0000000000000000000000000000000000000000000000000000000000000000"
            ],
            "type": "mined",
            "id": "log_f5493494"
        }
    ],
    "status": true,
    "logsBloom": "0x00000000000000000000000000000100000000000000000000000000000000002000000000000000000000000000000000000000000000040000000000000000000000000000000000800008000008000000000000000000000000000000000000000000020000000000000000000800000000000000000000400010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000002000000000000000000020000000000200000000000000000000020000000000000000000000000000000000000000000000000000000000000000000",
    "v": "0x2dc32acb6eb",
    "r": "0xc61fe8837727dda287ab1253006832ca7546a191580dd2e3d676f5e77bdd3c9f",
    "s": "0x6db737b383a9cfd5f59bc41351adf0665fea86b6708c123f7103413d43972859"
}
Daniel
  • 359
  • 4
  • 16
2

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
Shamit Verma
  • 534
  • 2
  • 5
  • This seems to contradict the answer from Daniel Kmak, which clearly shows the logs available in the transaction receipt. – Adam Spiers Jul 15 '21 at 09:45
1

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.

Jesbus
  • 10,478
  • 6
  • 35
  • 62
1

This worked for me with a contract instance 'remittance' calling the 'release' function emitting an event called 'HashCreated':

await remittance.release(passBytes32, {from: broker});

remittance.getPastEvents('HashCreated', {}, function(error, events){ console.log(events); }) .then(function(events){ console.log(events) });

Derived from and credit to Zack McGinnis https://ethereum.stackexchange.com/a/45403/48189

Onshop
  • 136
  • 3