python - 使用 Web3 从 Txn Hash 获取交易中的代币数量

标签 python blockchain ethereum web3py binance-smart-chain

遵循 web3 文档 https://web3py.readthedocs.io/en/stable/examples.html#looking-up-transactions ,我可以像下面那样获取交易信息。

但我似乎无法获得交易中交换的代币的确切数量? 在示例中的随机交易中,该人用 55 BUSD 交换了 20,997.266937044506585321 Shit coin 代币,我如何访问该信息?

from web3 import Web3
import json
bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))

result = web3.eth.get_transaction("0x77c5949df82ce8625d9e578fc696bc7264c99dbea4f763ce1fcdbe59dac5f029")

tx_json = Web3.toJSON(result)
json_obj = json.loads(tx_json)

print(json_obj)


output:

{'blockHash': '0x337d54009908f38c18b23bb5c22a1d0f204619513a76d4f8906708616e37f9cd', 'blockNumber': 19758221, 'from': '0x6Fa03B624d296ca2ecAbc75740228Ee9E0d018Ec', 'gas': 624870, 'gasPrice': 50000
00000, 'hash': '0x77c5949df82ce8625d9e578fc696bc7264c99dbea4f763ce1fcdbe59dac5f029', 'input': '0x5c11d795000000000000000000000000000000000000000000000002fb474098f67c000000000000000000000000000000
000000000000000000044cd383c04096e0098700000000000000000000000000000000000000000000000000000000000000a00000000000000000000000006fa03b624d296ca2ecabc75740228ee9e0d018ec00000000000000000000000000000
00000000000000000000000000062d9eb130000000000000000000000000000000000000000000000000000000000000003000000000000000000000000e9e7cea3dedca5984780bafc599bd69add087d56000000000000000000000000bb4cdb9c
bd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000031b35fdaa0780a75dd08a1a631c03e34fcef7173', 'nonce': 117, 'to': '0x10ED43C718714eb63d5aA57B78B54704E256024E', 'transactionIndex': 45, 'value
': 0, 'type': '0x0', 'v': 147, 'r': '0xbcf641aaa83dc321f185d961fde7625b7ad09a86390646015c8b92193186adcb', 's': '0x33546afae31ff2b87d797ed45abe7ad3494760578d36791ff03a76dc11446d7e'}


最佳答案

您可以通过交易收据中的Swap事件获取交易中的代币数量。 web3-ethereum-defi library附带一个现成的函数来提取事件及其数据。

analysis: TradeSuccess = analyse_trade(web3, uniswap_v2, tx_hash)
print("Swapped in", analysis.amount_in)
print("Swapped out", analysis.amount_out)

请注意,更复杂的交换可能有多个输入和输出。

For more information see eth_defi.uniswap_v2.analysis.analyse_trade_by_hash function 。我建议您查看其源代码以加深理解。

关于python - 使用 Web3 从 Txn Hash 获取交易中的代币数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73073828/

相关文章:

blockchain - Hyperledger 中的节点数限制

ethereum - 由于重新混合时出现气体错误,重入代码未执行

python - 如何使用python在同一个字典中调用方法

python - 通过字典中的系列映射创建新的二进制列

以不同概率选择列表元素的 Pythonic 方法

docker - 错误: Post "https://localhost:7053/participation/v1/channels": EOF Channel creation failed

python - 使用 python 和 boto 的亚马逊 FPS 的最小示例?

hyperledger-fabric - 从 super 账本结构中的节点 sdk 初始化网络 channel 时出错

blockchain - 如何追踪以太坊状态变量的历史?

rust - `paint_evm::Event` 未针对 `Event` 实现