0

I have this code that is printing out all the vars of the class; however, I am only interested in the "input" var. Is there an easy way to access this this "'input': '0x095ea7b3000000000000000000000000e428127cc2055e5c9d756ddce356aa2bf8da379b0000000000000000000000000000000000000000000000015ba31f54770ec000'

from pprint import pprint
from textwrap import indent
from web3 import Web3

bsc = "https://bsc-dataseed.binance.org/"  # for ETH use Infura
w3 = Web3(Web3.HTTPProvider(bsc))
print("Connected to web3: ", w3.isConnected())
# ----------------------------------

# enter tx_hash 0x24cc5476a1c96fb9ebe2daf63a970643195532682843aed692249033b1844440
tx_hash = "0x24cc5476a1c96fb9ebe2daf63a970643195532682843aed692249033b1844440"

# raw_data = w3.eth.get_raw_transaction(tx_hash)
# print("\n\n", raw_data.hex())

data = w3.eth.get_transaction(tx_hash)
pprint(vars(data))

below is my output

  {'blockHash': 
  HexBytes('0x4146c9f1f78c8e37f778723ef0365cdedb96c06d8b0956bc04d0c2e4087541e0'),
 'blockNumber': 10707576,
 'from': '0x2CfF6fB5a463735D65c29d933CE7f1C45350cb33',
 'gas': 66177,
 'gasPrice': 5000000000,
 'hash': HexBytes('0x24cc5476a1c96fb9ebe2daf63a970643195532682843aed692249033b1844440'),
 'input': '0x095ea7b3000000000000000000000000e428127cc2055e5c9d756ddce356aa2bf8da379b0000000000000000000000000000000000000000000000015ba31f54770ec000',
 'nonce': 2064,
 'r': HexBytes('0x695d48b9b33402a922b9e33febaf637aa9105c4bf18e57a7d6ced489d9c9588d'),
 's': HexBytes('0x16d481ca432653ed29283e8e21f86eb4c5d359cc7553b037e2c62e3b0ef43c6c'),
 'to': '0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56',
 'transactionIndex': 134,
 'type': '0x0',
 'v': 148,
 'value': 0}
1
  • This is my solution for now but there must be a better way: <code> data = w3.eth.get_transaction(tx_hash) string_out = dict(vars(data)) tx_data = string_out.get("input") print(tx_data)<\code> Commented Sep 12, 2021 at 21:14

1 Answer 1

1
print(vars(data)['input'])

It's a dictionary:

>>> type(vars(data))
<class 'dict'>

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.