ERC20|ERC20 交易记录分析

ERC20代币的转账交易不是ETH的交易结构,那么如何从eth链中得到ERC20的交易信息呢?
ERC20转账后Transaction的记录是这样的:

{ hash: '0x023692023a0068a751fcee1c417c966f74e8a19f263af60898e53d55376b9ff2', nonce: 13, blockHash: '0xcc66c0d6e0e06cbb49bb418a2a6ca1f00caf747b9f4b9823ba40155b2c2bb531', blockNumber: 14, transactionIndex: 0, from: '0xc4cbe50fc291e3b7eebd83012658aeb41df1dd8d', to: '0xa8a3fdda4b017d09a5226e5f2f9f0fcdba394898', value: BigNumber { s: 1, e: 0, c: [ 0 ] }, gas: 100000, gasPrice: BigNumber { s: 1, e: 0, c: [ 1 ] }, input: '0xa9059cbb000000000000000000000000c0bdcba12c1cdf1d2afe2bf5ec5533b03a3c332d0000000000000000000000000000000000000000000000000000000000000017' }

value是0,不能据此得到ERC20交易信息。
需要用到 TransactionReceipt,代码:
var transaction = web3.eth.getTransactionReceipt("0x023692023a0068a751fcee1c417c966f74e8a19f263af60898e53d55376b9ff2"); console.log(transaction);

结果:
{ transactionHash: '0x023692023a0068a751fcee1c417c966f74e8a19f263af60898e53d55376b9ff2', transactionIndex: 0, blockHash: '0xcc66c0d6e0e06cbb49bb418a2a6ca1f00caf747b9f4b9823ba40155b2c2bb531', blockNumber: 14, gasUsed: 36569, cumulativeGasUsed: 36569, contractAddress: null, logs: [ { logIndex: 0, transactionIndex: 0, transactionHash: '0x023692023a0068a751fcee1c417c966f74e8a19f263af60898e53d55376b9ff2', blockHash: '0xcc66c0d6e0e06cbb49bb418a2a6ca1f00caf747b9f4b9823ba40155b2c2bb531', blockNumber: 14, address: '0xa8a3fdda4b017d09a5226e5f2f9f0fcdba394898', data: '0x0000000000000000000000000000000000000000000000000000000000000017', topics: [Array], type: 'mined' } ], status: '0x1', logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000200000000000000000000000000000000008000000000010000000000000000000000040000000000000000000000000000000000000000000000000000000000410000000000000000000000000000000000000000008000000000000000000000000000000000000002000000000000000000000000000000000000000000000000002000000000002000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000' }

其中的logs是关键,重点看一下:
[ { logIndex: 0, transactionIndex: 0, transactionHash: '0x023692023a0068a751fcee1c417c966f74e8a19f263af60898e53d55376b9ff2', blockHash: '0xcc66c0d6e0e06cbb49bb418a2a6ca1f00caf747b9f4b9823ba40155b2c2bb531', blockNumber: 14, address: '0xa8a3fdda4b017d09a5226e5f2f9f0fcdba394898', data: '0x0000000000000000000000000000000000000000000000000000000000000017', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x000000000000000000000000c4cbe50fc291e3b7eebd83012658aeb41df1dd8d', '0x000000000000000000000000c0bdcba12c1cdf1d2afe2bf5ec5533b03a3c332d' ], type: 'mined' } ]

address 是合约ID。
data 是交易额。
topics 包含了我们关注的信息:
  • topics[0] - 事件的keccka的hash
  • topics[1] - from 地址
  • topics[2] - to 地址
【ERC20|ERC20 交易记录分析】这样就可以从链上得到某个ERC20代币的交易记录了。

    推荐阅读