With web3js, how do you figure out that there was 40000 tokens transfer from the transaction's hash?
4 Answers
There's a very good blog post on Medium using the exact method you're interested in.
(Stealing from the post):
- Retrieve the
input
data fromweb3.eth.getTransaction()
This will return the hex data for the function and parameters sent in the transaction. It will look something like0xa9059cbb0000000000000000000000007adee867ea91533879d083dd47ea81f0eee3a37e000000000000000000000000000000000000000000000000d02ab486cedbffff
. - The first 32 bits (0xa9059cbb) is the sha3 encoded text of the function signature.
- Every 256 bit block after that is an argument passed in.
- After parsing out the block corresponding to the number of tokens in the parameter list, use
web3.utils
to convert to decimal.

- 10,175
- 10
- 35
- 48
-
The first encoded text of the function signature (0xa9059cbb...) is 34 bits instead of 32. – Chris Peng Feb 05 '18 at 03:18
-
1Which function in `web3.utils` to use you convert `000000000000000000000000000000000000000000000000d02ab486cedbffff` value into number? – Sallu Mar 11 '18 at 02:58
-
@Sallu https://web3js.readthedocs.io/en/1.0/web3-utils.html#hextonumber – Justin Feb 01 '19 at 12:39
I will try to show an example how you do this:
lets take this Tx:
0xa543a3a7b6498bc9aec6989d99228be07855cdd23cfbf491489e8d4088b4a94c
This is Tx to a contract that send some amount of token to address
The received data from web3.eth.getTransaction()
input:
0xa9059cbb00000000000000000000000092e707288dc221d864cf4a8c710c143e97225d7d000000000000000000000000000000000000000000000059f37b9220158a8000
Now the first 34 bits represent text of the function signature (0xa9059cbb)
The next 256 bit block represent the address we want send the token to:
00000000000000000000000092e707288dc221d864cf4a8c710c143e97225d7d
The second block represent the amount (in hex) of tokens that were sent to the address:
000000000000000000000000000000000000000000000059f37b9220158a8000
We will convert the hex to decimal with any conversion function or with this web site: https://www.rapidtables.com/convert/number/hex-to-decimal.html
we will see that after the conversion we get 1659305000000000000000 it the number of token that sent to the address.
I hope it help

- 335
- 4
- 15
-
-
-
1@Tomer are you able to determine the currency used? I am subscribing to NFT sales on OpenSea where various currencies are allowed (DAI, WETH, USDC, ETH). For ETH txs I can just use the value field. But for others I'd like to know the amount of tokens and the type of token. Any suggestions? – Tyler Pashigian Nov 30 '21 at 23:13
-
is it possible to do the same for the following ? https://ftmscan.com/tx/0xd3f8424c0f05efc7e628f05b8bc94e53b80a85c7c035909b4c0a2189af3074eb – Yagiz Ozen Feb 18 '22 at 09:55
-
@Tomer how would you decode a transaction 'more complicated' like this? https://etherscan.io/tx/0x77e678921f642e327a7ad426bd7ee1db8011c31d1d61ec8991db0e87a7c4419a – giacomomaraglino May 10 '22 at 17:39
Ethereum smart contract transaction input data decoder Uses ethereumjs-abi for decoding. https://github.com/miguelmota/ethereum-input-data-decoder

- 21
- 1
just use web3.eth.getTransaction(transaction_address)
let transaction= await web3.eth.getTransaction("0X....")
console.log(JSOM.stringlify(transaction))
just not forget to define your provider and your web3 object before.

- 17,736
- 16
- 35
- 75
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 09 '21 at 01:45
-
This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30540037) – MD. RAKIB HASAN Dec 13 '21 at 11:48