0

我正在使用 web3js 获取交易详情

我的代码:

const transactionHash = this._req.query.transactionHash;

const transaction = await this._web3.eth.getTransactionReceipt(transactionHash);

const logs = await transaction.logs;

const log = await logs.find(i => i.transactionHash === transactionHash);

const topics = await log.topics;

const test = await this._web3.eth.abi.decodeParameter('bytes32', topics[0]);

const from = await this._web3.eth.abi.decodeParameter('address', topics[1]);

const to = await this._web3.eth.abi.decodeParameter('address', topics[2]);

const value = await this._web3.eth.abi.decodeParameter('uint256', log.data);

const amount = await this._web3.utils.fromWei(value);

但我仍然没有得到交易的令牌名称

在此处输入图像描述

给我一些建议,谢谢

4

1 回答 1

1

要获取代币符号,您需要调用代币合约的函数symbol()

由于Transfer事件是由代币合约发出的,因此您在log.address属性中拥有它的地址。然后你只需要调用symbol()函数:

const abiJson = [
    {"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}
];

const contract = new web3.eth.Contract(abiJson, log.address);
const symbol = await contract.methods.symbol().call();
于 2022-01-11T19:11:03.017 回答