[Ethereum] How to get the value of the transaction

blockspythonraw-transactiontransactionsweb3.py

I am using web3.py but I need help to know the value of the transaction sent.
I know that the information is stored in the input but how do I parse the string. I know nothing about ABI.
Here is the tx_hash = 0xb6a594e6e9579d76870d54618b8df25e6662a200f1d2f3c9e27f506e851fa092
and here is the input: 0xa9059cbb000000000000000000000000453a0961ed6badd98fc396b01ab9b5939d9e27f1000000000000000000000000000000000000000000000000000000000000000a

How do I get the value of the transaction in a simple value ?

Best Answer

New Answer (Based on comment)

To get the value of ERC20 tokens being transferred, you need to parse the input data of the transaction.

Per the standard, the transfer function looks as follows:

function transfer(address _to, uint256 _value) public returns (bool success)

This means that you must look for the final parameter in the input data. An example transaction can be seen here. In this example, the input data is as follows:

0xa9059cbb000000000000000000000000dfbc84ccac430f2c0455c437adf417095d7ad68e0000000000000000000000000000000000000000000000158b5ff8fb42b95000

0xa9059cbb

This is the hash of the method signature.

000000000000000000000000dfbc84ccac430f2c0455c437adf417095d7ad68e

This is the address where the tokens are being sent.

0000000000000000000000000000000000000000000000158b5ff8fb42b95000

This is the amount (in Hex) and is the value you are looking for. In this example, this comes out to 397.424645 tokens.


Original Answer

The value of the transaction is actually not in the input data, but rather in its own value field. If you know the hash, you can get the value.

>>> tx_data = web3.eth.getTransactionReceipt(tx_hash)
>>> print(tx_data["value"])
1000000000000000000

If you get the transaction receipt, the value field will be included in the output. That is the amount of Wei sent in the transaction.

Related Topic