How can I get only to and from addresses from a transaction Hash using Web3. I am unable to get details of to and from using web3.eth.getTransaction() method. Please help!!
-
It is not possible to obtain to and from from the transaction hash alone (it is a hash), you need at least the raw transaction.– Ismael ♦Mar 18, 2021 at 5:16
-
Can I get raw transaction hash from mempool?– Yash AttalMar 19, 2021 at 6:07
-
What web3 provider are you using? getTransaction should return an object with to and from. If it is failing with getTransaction it might fail with other apis.– Ismael ♦Mar 19, 2021 at 13:47
2 Answers
You can use the hash to get the transaction receipt. The transaction receipt will include the to and from addresses among other things. There are similar methods for inspecting the pending transaction before it is mined.
https://web3js.readthedocs.io/en/v1.3.4/web3-eth.html#gettransactionreceipt
Hope it helps.
If you use web3.py, you need some python logic but you can make it work!
Assuming that you already have a tx hash, use the eth.getTransaction
method to retrieve the tx details and place them in a variable.
Then we convert the 'AttributeDict' object received to dictionary that python can read and extract the data.
tx_hash = "0x31ffad3241a97b6f235141d88bdc257e562a4c506a77da85010fde7519cd3296"
details = web3.eth.getTransaction(tx_hash)
# convert the 'AttributeDict' object to a dictionary
parsedDict = dict(details)
# create a list of info you want to retrieve from the hash
info = ['from','to']
# loop to extrapolate the information
for key, val in parsedDict.items():
for word in info:
if word == key:
print(key +':', val)
it will print this:
from: 0xe398aD530c47C16151DaE087F19A2e87C689E1d0
to: 0x0D0707963952f2fBA59dD06f2b425ace40b492Fe