我正在使用 Etherscan API 获取交易数据。这是我得到的示例结果:
{
blockNumber: '7409930',
timeStamp: '1639151980',
hash: '...',
nonce: '4124',
...
input: '0x9d90e4c8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002000000000000000000000000093238bb66b5d15b4152c5db574e3397ff1b1a450',
contractAddress: '',
cumulativeGasUsed: '403775',
gasUsed: '1162315',
confirmations: '191308'
}
我现在需要确定此交易的事件类型(合同方法,例如TransferOwnership
,stakeTokens
...)。此数据存储在input
此对象的属性中。
我设法使用abi-decoder
库来完成这项工作,但我想使用 ethers 的实用方法(无论哪种方式)来完成同样的事情。
我目前的实现:
const abiDecoder = require("abi-decoder");
abiDecoder.addABI(contractAbi);
// "item" is transaction data, input property is encoded stuff from which I want to get the contract method used by this transaction
const decodedInput = abiDecoder.decodeMethod(item.input);
// contract method
console.log(decodedInput.name);
我正在阅读以太的文档(https://docs.ethers.io/v5/api/utils/abi/coder/),但我无法弄清楚。