繁体   English   中英

如何使用 web3 js 获取交易明细中的代币转移?

[英]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);

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

在此处输入图像描述

给我一些建议,谢谢

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

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

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM