Extracting Information from web3 py transaction

  Kiến thức lập trình

I am trying to build a wallet tracking software that tracks an ethereum wallet’s profitability through the trades they make.

I am using infura’s api key and the web3 python library.

How can I properly get if the transaction is a buy or a sell? I thought I could do this by checking the to and from address returned in the transaction object. However, the from is always the wallet I am tracking.

The first block number included below is a buy transaction and the second one is a sell. Both interacting with the same token.

Note:
I have used a random wallet I found on the blockchain, this is not my own.
I have removed my infura api key on line 6. To run it create a free api key from infura

from pprint import pformat
from web3 import Web3


wallet_address = "0x212A2F7B3Fd7Aa2D7A33599A9D065db42CE42900"
web3 = Web3(Web3.HTTPProvider('https://mainnet.infura.io/v3/INCLUDE YOUR INFURA API KEY HERE))
if web3.is_connected() == False:
    print("Could not connect to infura")
    exit(1)

def getTxnFromBlock(wallet_address, block_num):
    block = web3.eth.get_block(block_num, full_transactions=True)
    for tx in block.transactions:
        if tx['from'] == wallet_address or tx['to'] == wallet_address:
            return tx
    return None

def getTransactionType(txn):
    if txn['from'] == wallet_address:
        return "Sell"
    elif txn['to'] == wallet_address:
        return "Buy"
    return "Unknown"

def printTxn(txn):
    if txn == None:
        print("No transaction found for the given wallet address in the specified block: {block_num}")
        return
    
    print("Transaction Type: " + getTransactionType(txn))
    print(format(pformat(getTxnFromBlock(wallet_address, txn['blockNumber']))))

def main():

    # confirmed buy txn
    block_num = 20000310
    printTxn(dict(getTxnFromBlock(wallet_address, block_num)))
    print("------------------------------------------------------")
    # confirmed sell txn
    block_num = 20002255
    printTxn(dict(getTxnFromBlock(wallet_address, block_num)))
    
if __name__ == "__main__":
    main()

Here is the code output. The first txn should be “buy” but is “sell”:

Transaction Type: Sell
AttributeDict({'accessList': [], 'blockHash': HexBytes('0x1cfedf3a8dcb20bbadfe31d6236641abb419beac830864c9d221100953230fb5'), 'blockNumber': 20000310, 'chainId': 1, 'from': '0x212A2F7B3Fd7Aa2D7A33599A9D065db42CE42900', 'gas': 600000, 'gasPrice': 85343730281, 'hash': HexBytes('0xde1b649b0dae45e8109a734780b49b366c6fb6643261f81839e944e3c755a697'), 'input': HexBytes('0x4b9ead9e0000000000000000000000000000000000000000000001c75ba7360e533c2680000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000b1a2bc2ec50000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000003e80000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000d8e8438cf7beed13cfabc82f300fb6573962c9e3'), 'maxFeePerGas': 93787335558, 'maxPriorityFeePerGas': 80000000000, 'nonce': 334, 'r': HexBytes('0xa088ec776415129270aac62ef0c3284f47e8ab25342a3469d2c66475b505b6be'), 's': HexBytes('0x2360b6879e2b5ef23ab2591e7ead90e6d039fc79eb09f0db7e79cb069541e065'), 'to': '0xB5AE54c696e28b088Be785b73f118CAE04A0DF87', 'transactionIndex': 0, 'type': 2, 'v': 0, 'value': 50000000000000000, 'yParity': 0})
------------------------------------------------------
Transaction Type: Sell
AttributeDict({'accessList': [], 'blockHash': HexBytes('0x0d380c25176507990e646a64376fad66dcdc9e91c9714df56e428b6f3ca2e929'), 'blockNumber': 20002255, 'chainId': 1, 'from': '0x212A2F7B3Fd7Aa2D7A33599A9D065db42CE42900', 'gas': 850039, 'gasPrice': 9122918725, 'hash': HexBytes('0x5228c64ca50f54d384a4ca019e9609a89839409b4f0b0de51096738a5a52db56'), 'input': HexBytes('0x845d76f70000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000002000000000000000000000000d8e8438cf7beed13cfabc82f300fb6573962c9e3000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'), 'maxFeePerGas': 11042597452, 'maxPriorityFeePerGas': 5000000000, 
'nonce': 336, 'r': HexBytes('0x7f04026c3eaa4b33a1418a8de75c5e47597da2e4cb2b474c445c50a35ca4f332'), 's': HexBytes('0x72acf1da81da97b87dc52c62c1931e8357fe5f7350a04cfcfb18c4f75f8bd063'), 'to': '0xB5AE54c696e28b088Be785b73f118CAE04A0DF87', 'transactionIndex': 2, 'type': 2, 'v': 1, 'value': 0, 'yParity': 1})

LEAVE A COMMENT