3

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 ?

6
  • What is the value of the transaction? Commented Mar 20, 2019 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 Commented Mar 20, 2019 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. Commented Mar 20, 2019 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). Commented Mar 20, 2019 at 18:55
  • 1
    @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 ? Commented Mar 20, 2019 at 19:10

2 Answers 2

4

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.

4
  • 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 Commented Mar 20, 2019 at 18:31
  • @strangethingspy I have updated the answer. Commented Mar 20, 2019 at 19:24
  • 1
    The problem is: how do I parse this using web3.py ? Commented Mar 20, 2019 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. Commented Mar 20, 2019 at 20:41
2

I'm going to add to the accepted answer, because I dislike parsing the input to some arbitrary length. What I found was that the actual hex value is also contained in the web3.eth.getTransactionReceipt(tx_hash) response.

If you inspect the response, there is a logs[] property, which for me always contains 1 object. This object has data property, which is excatly the hex value of the transaction. Example:

logs: [{
   "address": "0x905ed504db11Dd3b8C2bBe79E9A0A6c39E1A0deb",
   "blockNumber": 3262252,
   "data": "0xea"
}]

When i take this value I easily convert it to a number: web3.utils.toBN('0xea')

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.