## 一、交易信息获取 ### 1.1 合约事件例子定义 举例,比如合约中事件如下:(以下内容均使用该事件例子) “` event Transfer(address indexed from, address indexed to, uint256 value); “` – 事件名称:Transfer – 事件的参数:address, address, uint256 – 注意:此事件的from和to参数前有indexed标记,value没有indexed标记 ### 1.2

web3调用中文文档:https://www.wenjiangs.com/doc/pr2nvcat

curl 调用:

curl -s -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"eth_getTransactionByHash","params":["0xae2a33da8396a6bc40e874b0f32b9967113a3dbf071ab1290c44c62d86873d36"],"id":1}' http://127.0.0.1:8545

eth_getTransactionReceipt

{   "jsonrpc": "2.0",   "id": 1,   "result": {     "blockHash": "0xb0d0e3b6c5e59b7b3e7e16701f6d6cb0c3c93487415b03839e88b3f7a241c528",     "blockNumber": "0xd19505",     "contractAddress": null,     "cumulativeGasUsed": "0x6c847e",     "effectiveGasPrice": "0x274daee580",     "from": "0xb8262c6a2dcabd92a77df1d5bd074afd07fc5829",     "gasUsed": "0xa169",     "logs": [       {         "address": "0xdac17f958d2ee523a2206206994597c13d831ec7",         "topics": [           "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",           "0x000000000000000000000000b8262c6a2dcabd92a77df1d5bd074afd07fc5829",           "0x000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec7"         ],         "data": "0x000000000000000000000000000000000000000000000000000000016512c902",         "blockNumber": "0xd19505",         "transactionHash": "0xae2a33da8396a6bc40e874b0f32b9967113a3dbf071ab1290c44c62d86873d36",         "transactionIndex": "0x71",         "blockHash": "0xb0d0e3b6c5e59b7b3e7e16701f6d6cb0c3c93487415b03839e88b3f7a241c528",         "logIndex": "0xa0",         "removed": false       }     ],     "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000010000000000000000000000000000000000000000000000000000000008000000000080000000000000000000000000000000000000000000000000000000000000000000200000000000000010000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000080000000000000000000000000000000000000004000000002000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000200",     "status": "0x1",     "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",     "transactionHash": "0xae2a33da8396a6bc40e874b0f32b9967113a3dbf071ab1290c44c62d86873d36",     "transactionIndex": "0x71",     "type": "0x2"   } }

注意:getTransaction返回结果中包含input,而getTransactionReceipts返回结果中包含logs。

二、input解析

2.1 input内容解析

input=0x则为非合约调用,否则为合约方法调用。

以合约方法function transfer(address to, uint tokens) 为例;

input数据分为3个部分:

  • 第一部分: 4 字节,是方法名的哈希

例如:a9059cbb,具体可参见:

三、logs解析

  1. 取出transactionReceipt中logs;
  2. 取出logs中一条log;
  3. 使用event.js得到transferEvent,然后用transferEvent的decode方法解析log;

若是公链转币,

getTransactionReceipt返回信息字段详情可参考:https://infura.io/docs/ethereum/json-rpc/eth-getTransactionReceipt


以上,就是今天分享的全部内容了,希望大家通过以上方式可以解决自己的实际需求,解决自己目前所遇到的问题。

如果文章中有不太正确的地方,欢迎指正,可以添加我的个人微信,备注:地区-职业方向-昵称,欢迎来撩,加入

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注