web3.eth.abi.decodeLog(inputs, hexString, topics) 函数中 inputs 如何获取

官方的Demo是这样的。文档中有提到,第一个参数是 json 格式的参数,就是需要解码的数据类型。问题是这个数据类型怎么获取。

文档地址:https://web3js.readthedocs.io/en/v1.2.11/web3-eth-abi.html#decodelog

找到对应的合约地址,然后去看相应的函数里面具体是什么类型。

例如:

Chainlink contract transfer event

转移事件

event Transfer(address indexed from, address indexed to, uint256 value);

如果想解码需要的 inputs

[{
    type: 'address',
    name: 'from',
    indexed: true
},{
    type: 'address',
    name: 'to',
    indexed: true
},{
    type: 'uint256',
    name: 'value'
}]

原文:https://ethereum.stackexchange.com/questions/79788/what-is-input-in-web3-eth-abi-decodeloginputs-hexstring-topics

web3py transaction 数据解析

使用 web3py 代码得到的是一个字典结构的数据,但是大部分的数据都是 HexBytes,非常头疼这些数据的具体内容,查阅了一些资料。

首先是代码:

from web3 import Web3

w3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/f6c8f8c206d6431599b73ead57ab9737'))
print(w3.eth.get_transaction_receipt('0x0acf73a58fbb879ffc43150aef0fc31cbe94cab036ba29948923994852643e83'))

返回结果:

有一些内容可以直接看出是什么,但是也有很多 HexBytes 的不知道是什么,查到很多资料之后才知道这些是经过编码的。编码规律是有资料的,ABI 接口中。

比如 topics 中一个很常见的:

topics[0]

0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

 

这是  Transfer 就是转账的函数

Transfer(address,address,uint256)

topics[1]是从哪个账户转出; topics[2] 是转入哪个账户;data 是转账的数值。

etherscan 可以解析出所有的信息,格式化的很好。

例如:

https://etherscan.io/tx/0x0acf73a58fbb879ffc43150aef0fc31cbe94cab036ba29948923994852643e83