[Ethereum] How to get events emitted by a transaction with web3.js

eventsjavascripttransactionsweb3js

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?

Best Answer

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"
}
Related Topic