Search code examples
python-3.xethereumweb3py

Trying to tokenise Ethereum TX sender "from" to crosscheck values


Trying to get back to python with a web3 project. I am trying to extrapolate precise values from a tx (from, to, value) to crosscheck via python if those values are the expected one.

txtest = web3.eth.get_transaction('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4')
finaltx = txtest.to
print('This is the raw tx result ', txtest)
print('This is the sender address: ', finaltx)

perfectly works:

This is the raw tx result  AttributeDict({'blockHash': HexBytes('0x3ec30c2f44d15e55e3caee66ae238819ae2809d81a0df6b0ca58fa722a78ae91'), 'blockNumber': 16751195, 'from': '0xDAFEA492D9c6733ae3d56b7Ed1ADB60692c98Bc5', 'gas': 21000, 'gasPrice': 22256602635, 'maxFeePerGas': 22256602635, 'maxPriorityFeePerGas': 0, 'hash': HexBytes('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4'), 'input': '0x', 'nonce': 252468, 'to': '0xe688b84b23f322a994A53dbF8E15FA82CDB71127', 'transactionIndex': 166, 'value': 274458557610409937, 'type': '0x2', 'accessList': [], 'chainId': '0x1', 'v': 0, 'r': HexBytes('0xe2a1cf9bf7ec838468a03929c535a536462e6e4cde2978e4a1e39826a738f534'), 's': HexBytes('0x08a9073666cb98adcd7166ffc216f1b23b09b9463a703440427047cae6d95217')})
This is the sender address:  0xe688b84b23f322a994A53dbF8E15FA82CDB71127

but if i try to fetch the "from" in the AttributeDict with finaltx = txtest.from then i can't run the code:

txtest = web3.eth.get_transaction('0x877af182f545cde910544f2f869429fc7bd66f24fb58b42b1f691667f26c95c4')
finaltx = txtest.to
print('This is the raw tx result ', txtest)
print('This is the sender address: ', finaltx)
addressfrom = txtest.from

errors with

finaltx = txtest.from
                     ^
SyntaxError: invalid syntax

Thank you in advance!


Solution

  • Found the solution.

    Might sound dumb from me but as from is reserved for code, in order to call addressfrom = txtest.from in a readable state for python, it has to be passed with getattr(), so the solution is:

    addressfrom = getattr(txtest, 'from')
    print(addressfrom)
    

    which outputs the right address ^^