查询event log
从ethereum json-rpc文档的文档中找到一个同时指定多个事件以 OR
或者 AND
查询的方法.以下是查询 Approval 或 Transfer 事件的方法:
{
"jsonrpc":"2.0",
"method":"eth_getLogs",
"params":[{
"topics":[["0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925","0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"]],
"address": "0xa7f6c9a5052a08a14ff0e3349094b6efbc591ea4",
"fromBlock": "0x499a",
"toBlock": "0x18999"
}],
"id":74
}
topics
字段中指定查询条件的语法参考上面链接。
ERC20代币交易详情解析
通过getTransactionReceipt
在ropsten测试网上查询到交易号为0x7e9eaf019928b7f8cf6f5fafa626a58d477f4800054f7101ffe941fda353abcb
的交易详情
{
"jsonrpc": "2.0",
"id": 74,
"result": {
"blockHash": "0xbc4dfb56410f28476ac6dbdaf68513a4b01ac0084fb21cc81b6803bd956d6365",
"blockNumber": "0x81b4",
"contractAddress": null,
"cumulativeGasUsed": "0xca9d",
"from": "0xdb33dfd3d61308c33c63209845dad3e6bfb2c674",
"gasUsed": "0xca9d",
"logs": [
{
"address": "0xa7f6c9a5052a08a14ff0e3349094b6efbc591ea4",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
"0x000000000000000000000000db33dfd3d61308c33c63209845dad3e6bfb2c674",
"0x0000000000000000000000006f2a8ee9452ba7d336b3fba03cac27f7818aead6"
],
"data": "0x0000000000000000000000000000000000000000000000056bc75e2d63100000",
"blockNumber": "0x81b4",
"transactionHash": "0x7e9eaf019928b7f8cf6f5fafa626a58d477f4800054f7101ffe941fda353abcb",
"transactionIndex": "0x0",
"blockHash": "0xbc4dfb56410f28476ac6dbdaf68513a4b01ac0084fb21cc81b6803bd956d6365",
"logIndex": "0x0",
"removed": false
}
],
"logsBloom": "0x0000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000008c000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000010000000000000000000001000000200000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000020002000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000",
"root": "0xb6824b5b1ff9cab405435d9735dfe3971755b78b955c957b96adfe96a0552217",
"to": "0xa7f6c9a5052a08a14ff0e3349094b6efbc591ea4",
"transactionHash": "0x7e9eaf019928b7f8cf6f5fafa626a58d477f4800054f7101ffe941fda353abcb",
"transactionIndex": "0x0"
}
}
这个交易从"from": "0xdb33dfd3d61308c33c63209845dad3e6bfb2c674"
发送到合约地址"to": "0xa7f6c9a5052a08a14ff0e3349094b6efbc591ea4"
.这个合约为ERC20代币合约.从topics
的第一个元素可以看出合约中产生了Transfer
事件(topics第一个元素一定是事件的keccak哈希). topics
的第二个字段是转出代币的地址,第三个字段是接收者地址.ERC20代币Transfer
事件的签名为
event Transfer(address indexed from, address indexed to, uint tokens);
我们注意到Transfer
事件的第一个和第二个参数被标记为indexed
, 因此他们的值被放在 topics array 中. 由于tokens参数没有标记为indexed
, 所以他的值被放在data
字段. 如果事件中有多个字段未标记为indexed
, 那么他们的值都会被记录在data
字段中。