Web3js
如何使用 web3.js 獲取事務發出的事件
我正在用 Javascript 編寫使用者界面,並且正在使用 web3.js 庫。
我有確認交易的交易雜湊。我想獲取該事務發出的所有事件的數組。
我不一定需要所有事件,只需要我的智能合約通過執行該交易發出的事件。
這可以使用 web3.js 完成嗎?有什麼好方法可以做到這一點?
如果您使用的是 Web3 v1,那麼您可以使用
web3.eth.getTransactionReceipt(tx)
. 它返回logs
屬性作為結果的一部分。您可以通過查看事件的
topics
屬性來對事件進行分類。樣本
getTransactionReceipt
結果:{ "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" }
這有點令人費解,因為 Web3 不直接暴露事務發出的事件,但可以通過跟踪事務來查詢。
- 跟踪交易:https ://github.com/ethereum/go-ethereum/wiki/Management-APIs#debug_tracetransaction
- 它將為每條交易指令返回一個資訊塊。(本例中為 PUSH1)
{深度:1,錯誤:“”,gas:162106,gasCost:3,記憶體:null,op:“ PUSH1 ”,pc:0,堆棧:
$$ $$, 貯存: {} } 3. 過濾操作碼 LOG0 – LOG4。這些指令用於引發事件。
- 從屬於 OPCODE 塊的數據中解碼事件名稱