如何使用web3 js获取交易明细中转账的token?

How to get tokens transferred in transaction details using web3 js?

我正在使用 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);

但是我还没有得到交易的代币名称

给我一些建议,谢谢

要获取代币符号,您需要调用代币合约的函数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();