我正在使用 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/),但我无法弄清楚。
您可以尝试以下推荐的内容:https://github.com/ethers-io/ethers.js/issues/423。但如果您与 BSC 交互,这是不可能的,因为错误输入数据太大,导致大数错误
Number can only safely store up to 53 bits
const Web3 = require('web3'); const web3 = new Web3();
常量日志= [ { “地址”:“0x537bD9BA6fd037EE001e070F3989d8b3E77bf30B”, “主题”:[ “0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef”, “0x0000000000000000000000000000000000000000000000000000000000000000”, “0x000000000000000000000000ad5292d3d35f57cc0d7876cfd7b583dc99637b0d” ], “数据”:“0x00000000000000000000000000000000000000000000152d02c7e14af6800000”, “区块号”:46160226, “交易哈希”:“0x77c64c25eeeecaf73ff51a9ac42163773544e7c4ff51cad4c69549b64ca4e116”, “交易索引”:2, “块哈希”:“0x25d5e5257ade627c241480503609520337c7e4924df1639fbf15f63925e0e029”, “日志索引”:4, “已删除”:假, “id”:“log_70b5e3f1” } ];
常量transferEventAbi = [ { “索引”:正确, “姓名”:“来自”, “类型”:“地址” }, { “索引”:正确, “姓名”:“至”, “类型”:“地址” }, { “索引”:假, “名称”:“值”, “类型”:“uint256” } ];
logs.forEach(log => { if (log.topics[0] === "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef") { const 解码 = web3.eth.abi.decodeLog( 转移事件Abi, 日志数据, log.topics.slice(1) // 排除第一个主题,因为它是事件签名 ); console.log("已解码的传输事件:",已解码); } });