1

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 ?

  • What is the value of the transaction? – goodvibration Mar 20 at 16:46
  • @goodvibration The value is 0 because I transferred tokens and not ether. I need to get the amount of erc20 tokens sent – strangethingspy Mar 20 at 18:33
  • So your notion of "value" is incorrect here. The only way to interpret "value of a transaction" is as the amount of wei-ether passed to the destination address (whether this address is an externally-owned account or a contract). You're asking about the value of a parameter passed to a function in a contract. So this is how you should ask the question - how can I tell the value of parameter "x" passed to function "y", using the transaction hash? - you can't. But you can check the balanceOf your account (or the totalSupply of the contract) before and after the transaction, and figure it out. – goodvibration Mar 20 at 18:55
  • Alternatively, there should be a Transfer event in the logs of the transaction receipt (which you can obtain using the transaction hash). – goodvibration Mar 20 at 18:55
  • @goodvibration Sorry for the misunderstanding, I now tried to do: w.eth.getTransactionReceipt('0xe95730480647c4298ec0e2376f619f7425d9e4cf4b1c7ba0415b1a8a81b54a21') and if I take the value in the data, and do int('0x0000000000000000000000000000000000000000000000000000000000000050',0) I see that I get the correct value. Is this approach correct ? – strangethingspy Mar 20 at 19:10
0

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.

  • 1
    The problem is when transferring erc20 tokens, I need to know how many tokens I have transferred. When doing as you told me to do, I get a value of 0 because I transferred erc20 tokens and not eth @shane – strangethingspy Mar 20 at 18:31
  • @strangethingspy I have updated the answer. – shane Mar 20 at 19:24
  • 1
    The problem is: how do I parse this using web3.py ? – strangethingspy Mar 20 at 19:33
  • The length of the parameters in the input data is a fixed number (in this case). There are 65 characters in each parameter. In this case, you can simply take the last 65 characters of the input data, search for the first non-zero number (from the left), and you will know that what remains is your value. – shane Mar 20 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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