Get token transfer detail from transaction hash with web3js
Asked Answered
C

5

14

enter image description here

With web3js, how do you figure out that there was 40000 tokens transfer from the transaction's hash?

Catherin answered 28/12, 2017 at 8:49 Comment(0)
P
14

There's a very good blog post on Medium using the exact method you're interested in.

(Stealing from the post):

  1. Retrieve the input data from web3.eth.getTransaction() This will return the hex data for the function and parameters sent in the transaction. It will look something like 0xa9059cbb0000000000000000000000007adee867ea91533879d083dd47ea81f0eee3a37e000000000000000000000000000000000000000000000000d02ab486cedbffff.
  2. The first 32 bits (0xa9059cbb) is the sha3 encoded text of the function signature.
  3. Every 256 bit block after that is an argument passed in.
  4. After parsing out the block corresponding to the number of tokens in the parameter list, use web3.utils to convert to decimal.
Pickel answered 28/12, 2017 at 18:50 Comment(3)
The first encoded text of the function signature (0xa9059cbb...) is 34 bits instead of 32.Catherin
Which function in web3.utils to use you convert 000000000000000000000000000000000000000000000000d02ab486cedbffff value into number?Awlwort
@Awlwort web3js.readthedocs.io/en/1.0/web3-utils.html#hextonumberPas
H
12

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

enter image description here

Homothermal answered 23/8, 2018 at 9:9 Comment(5)
how to know which rec20 token?Honshu
@Honshu the to node contains the token contract addressPas
@Homothermal 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?Hookah
is it possible to do the same for the following ? ftmscan.com/tx/…Fuchs
@Homothermal how would you decode a transaction 'more complicated' like this? etherscan.io/tx/…Pya
D
0

Ethereum smart contract transaction input data decoder Uses ethereumjs-abi for decoding. https://github.com/miguelmota/ethereum-input-data-decoder

Doughman answered 26/4, 2018 at 9:46 Comment(0)
B
0

I had the same issue, and what helped me was including method data among other parameters. You can get yours in the ABI of the token you are interacting with (in your case you go to https://etherscan.io/ and find your method's description) and insert it into you transaction parameters as data, so it will look like this: yourTransferMethod() .send({ from: senderAddress, gasPrice, data: { "constant": false, "inputs": [ { "name": "to", "type": "address" }, { "name": "value", "type": "uint256" } ], "name": "transfer", "outputs": [ { "name": "", "type": "bool" } ], "payable": false, "stateMutability": "nonpayable", "type": "function" }, });

Don't forget to add parameters in input! If you are not sure how to do it, try to test writing the contract on https://etherscan.io/ and extracting transaction data in hex from there, as shown in the image http://joxi.ru/xAepNGqCVWqdK2. To decode hex to the data you need use https://lab.miguelmota.com/ethereum-input-data-decoder/example/

I hope it works for you 👍

Bacon answered 14/12, 2023 at 16:12 Comment(0)
P
-3

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.

Perfectionist answered 8/12, 2021 at 18:12 Comment(2)
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.Wellbeloved
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewImmediate

© 2022 - 2024 — McMap. All rights reserved.