繁体   English   中英

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

[英]get amount of tokens in a transaction from Txn Hash using Web3

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

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

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 库带有一个现成的函数来提取事件及其数据。

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

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

有关更多信息,请参阅 eth_defi.uniswap_v2.analysis.analysis_trade_by_hash 函数 我建议您查看它的源代码以获得更多理解。

如本文所述https://medium.com/coinmonks/discovering-the-secrets-of-an-ethereum-transaction-64febb00935c

传输信息在 web3.eth.get_transaction(transaction tx) 返回的字典中的“input:”下找到。问题是数据需要从十六进制值解码。

这就是我们在路由器 ABI 上的 web3 合约上使用这个函数的原因:

func_obj, func_params = contractbuy.decode_function_input(result["input"]) 这将返回一个存储在这两个对象中的元组。

funcj_obj 包含解码后的输出:

{'amountIn': 55000000000000000000, 'amountOutMin': 20306659718028100372871, 'path': ['0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c', '0x31B35FdAa0
780a75dd08A1A631c03e34FCeF7173'], 'to': '0x6Fa03B624d296ca2ecAbc75740228Ee9E0d018Ec', 'deadline': 1658448659}

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

# uniswap factory address and abi = pancakeswap factory
uniswap_factory = '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73'  
uniswap_factory_abi = json.loads("[{super long abi}]")
contract = web3.eth.contract(address=uniswap_factory, abi=uniswap_factory_abi)

#pancakeswap router abi
panRouterContractAddress = '0x10ED43C718714eb63d5aA57B78B54704E256024E'
panabi = "[{super long abi}]"
contractbuy = web3.eth.contract(address=panRouterContractAddress, abi=panabi)

result = web3.eth.get_transaction("0x77c5949df82ce8625d9e578fc696bc7264c99dbea4f763ce1fcdbe59dac5f029")
#tx_json = Web3.toJSON(result)
#json_obj = json.loads(tx_json)

#returns a tupple packed in these two objects
func_obj, func_params = contractbuy.decode_function_input(result["input"])

print(func_params)

暂无
暂无

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

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