pythonblockchainethereumweb3pybinance-smart-chain

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


Following the web3 documentation https://web3py.readthedocs.io/en/stable/examples.html#looking-up-transactions , I can get the transaction information like i have done below.

But it seems like i cant get the exact amount of token swapped in the transaction? In the random transaction in the example the person swapped 55 BUSD for 20,997.266937044506585321 Shit coin tokens, how can i accesss that information?

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'}



Solution

  • As covered in this article https://medium.com/coinmonks/discovering-the-secrets-of-an-ethereum-transaction-64febb00935c

    The transfer information is found under "input:" in the dictionary which is returned by web3.eth.get_transaction(transaction tx) The problem is that the data needs to be decoded from a hexadecimal value.

    This is why we use this function on the web3 contract on the Router ABI:

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

    funcj_obj contains the decoded output:

    {'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)