admin管理员组

文章数量:1279236

I need to get the events emitted by my smart contract and consume them in the front end via web3.

I made some event on my contract that returns event winner and ticket number:

event Winner(uint256 ticketNumber, address winner);

So I emit this event, and I see it on transaction logs.

From Etherscan:

OK! What I need is the data: ticketNumber: 1, winner: 0x........ How did I get this from web3?

Im trying to use:

 await web3.eth.getTransactionReceipt(txnHash, function (error, result) {
          console.log(result);
        });

But when I check console log, I cannot see this information, I suspect that result.logs.data is the right info, but I don't know for sure, and I don't know how to translate:

"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000005964b608ea267bfe9ef77707fce8105a2d145e7a"

Anybody have an idea?

I need to get the events emitted by my smart contract and consume them in the front end via web3.

I made some event on my contract that returns event winner and ticket number:

event Winner(uint256 ticketNumber, address winner);

So I emit this event, and I see it on transaction logs.

From Etherscan:

OK! What I need is the data: ticketNumber: 1, winner: 0x........ How did I get this from web3?

Im trying to use:

 await web3.eth.getTransactionReceipt(txnHash, function (error, result) {
          console.log(result);
        });

But when I check console log, I cannot see this information, I suspect that result.logs.data is the right info, but I don't know for sure, and I don't know how to translate:

"0x00000000000000000000000000000000000000000000000000000000000000010000000000000000000000005964b608ea267bfe9ef77707fce8105a2d145e7a"

Anybody have an idea?

Share Improve this question edited Feb 11, 2024 at 23:54 Yilmaz 49.7k18 gold badges216 silver badges268 bronze badges asked Mar 6, 2022 at 4:31 Rafael BarleraRafael Barlera 751 gold badge1 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

If you read the docs, there is getPastEvents method.

myContract.getPastEvents('MyEvent', {
    filter: {myIndexedParam: [20,23], myOtherIndexedParam: '0x123456789...'}, // Using an array means OR: e.g. 20 or 23
    fromBlock: 0,
    toBlock: 'latest'
}, function(error, events){ console.log(events); })
.then(function(events){
    console.log(events) // same results as the optional callback above
});

you can also create event listeners:

contract.events.Winner()
.on('data', (event) => {
    console.log(event);
})
.on('error', console.error);

Docs about subscription to events

If you don't have the contract ABI with you, you can follow these steps to retrieve the events,
Step 1 - use await web3.eth.getTransactionReceipt(txHash) as shown here.
Step 2 - You would receive an object with a field logs. This would be an array of objects with the length equal to the number of events emitted in that transaction.
Step 3 - In each of these objects there would be two fields that would be important to us. data and topic. The data field will contain all the unindexed parameters of the given event. To decode that you can use web3.eth.abi.decodeLog(inputs, hexString, topics) as shown here.
Step 4 - You can get the name of the event from the first element of the field topic. The first element here corresponds to the keccak256 of the event signature. web3.utils.sha3(string) can be used to to hash your event signature to check if the first entry of topics array match. More info here.
Step 5- If your event has indexed parameters, they can be found from the rest of the entries of the the topics array. To convert then to human readable form, follow the same steps as step 3.

本文标签: javascriptHow to get Transaction Receipt Event LogsStack Overflow