我正在尝试通过币安智能链区块链使用 web3js,但在理解交易数据时遇到了障碍。
查看 example 的这笔交易,有 3 次交易转移(代币转移),大部分时间有 2 次(到目前为止我已经看到了 2、3 和 5)。 我不明白是什么决定了单笔交易的转账次数。以及如何使用 web3js 检索该数据。
如果交易是关于出售代币而不是购买,我想知道在该交易中支付的 BNB 数量和收到的代币数量,反之亦然。
我设法获得了支付的价格和代币数量,但仅适用于有 2 次代币转移的交易。但如果有 3 个或更多,我将无法获得此信息。
web3.eth.getTransaction('0x899e7f3c2138d051eb5246850ded99d519ab65eba58e5f806245cf346ab40e83').then((result) => {
console.log(result)
console.log(web3.utils.fromWei(result.value))
let tx_data = result.input;
let input_data = '0x' + tx_data.slice(10); // get only data without function selector
let params = web3.eth.abi.decodeParameters([
{
indexed: false,
internalType: 'uint256',
name: 'value',
type: 'uint256'
},
{
indexed: false,
internalType: 'uint256',
name: 'ethReceived',
type: 'uint256'
},
]
, input_data);
console.log(params)
})
这部分代码只为我提供了 2 次令牌传输的数据。无论交易中有多少次转账,如何让它始终归还我支付/收到的现金/代币的金额?是否有可能??从我始终可以看到,交易中的第一次转移和最后一次转移将是我感兴趣的值。有没有一种简单的方法来获得这些值?我正在努力理解这一点并使用 ABI 进行解码。它们可以有点通用吗??
答案 0 :(得分:0)
“令牌转移”信息来自事件日志。大多数令牌标准定义了一个事件 Transfer(address indexed from, address indexed to, uint256 value)
,因此您可以在交易中查找此事件的日志。
事件日志在 getTransactionReceipt() 中可用,而不是常规的 getTransaction()。
事件定义中的 indexed
修饰符意味着该值将在 topics
属性中可用(topics[0]
是事件签名的 keccak256 哈希值,遵循索引值)。 “未编入索引”的值然后存储在 data
属性中 - 根据其定义的顺序进行排序。
const transferEventSignature = web3.utils.keccak256('Transfer(address,address,uint256)'); // 0xddf252...
const jsonAbi = [{
"constant" :true,
"inputs": [],
"name": "decimals",
"outputs": [{"name":"","type":"uint8"}],
"type": "function"
}]; // simplified JSON abi that is only able to read decimals
web3.eth.getTransactionReceipt('0x899e7f3c2138d051eb5246850ded99d519ab65eba58e5f806245cf346ab40e83').then(async (result) => {
for (const log of result.logs) {
if (log.topics[0] !== transferEventSignature) {
continue; // only interested in Transfer events
}
const from = web3.eth.abi.decodeParameter('address', log.topics[1]);
const to = web3.eth.abi.decodeParameter('address', log.topics[2]);
const value = web3.eth.abi.decodeParameter('uint256', log.data);
const tokenContractAddress = log.address;
const contractInstance = new web3.eth.Contract(jsonAbi, tokenContractAddress);
const decimals = await contractInstance.methods.decimals().call();
console.log('From: ', from);
console.log('To: ', to);
console.log('Value: ', value);
console.log('Token contract: ', tokenContractAddress);
console.log('Token decimals: ', decimals);
console.log('---');
}
});
输出:
From: 0xC6A93610eCa5509E66f9B2a95A5ed1d576cC9b7d
To: 0xE437fFf464c6FF2AA5aD5c15B4CCAD98DF38cF52
Value: 31596864050517135
Token contract: 0x78F1A99238109C4B834Ac100d1dfCf14e3fC321C
Token decimals: 9
---
From: 0xE437fFf464c6FF2AA5aD5c15B4CCAD98DF38cF52
To: 0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16
Value: 4064578781674512
Token contract: 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c
Token decimals: 18
---
From: 0x58F876857a02D6762E0101bb5C46A8c1ED44Dc16
To: 0xC6A93610eCa5509E66f9B2a95A5ed1d576cC9b7d
Value: 2552379452401563824
Token contract: 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56
Token decimals: 18
注意:某些令牌实现不正确(即不遵循令牌标准)并且未将事件参数标记为 indexed
。在这种情况下,topics[0]
仍然相同,但地址 from
和 to
不在 topics
中,但您必须从data
字段。 address
的长度为 64 个十六进制字符(在实际 40 个字符的地址前加上零)。