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 badges2 Answers
Reset to default 4If 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
版权声明:本文标题:javascript - How to get Transaction Receipt Event Logs? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741303065a2371208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论