0

I am fetching the latest blocks transactions using web3 and then I use the getTransaction(txhash) function to get the transaction object. I was wondering, how could I get the information about which token has been transferred with this transaction? The object itself does not contain a property like contract address, only from, to, value and so on.

Is this even possible? If not, how can I determine which token had been sent with a transaction without explicitly subscribing to a contract address and only fetch these transactions? I want to fetch all token transactions using web 3 and monitor the transfers.

Regards

1
  • can you show us an example of the transaction? there is a difference between from, to, value in the transaction and the from, to, value from the logs included in the transaction receipt – Majd TL May 7 at 12:23
1

What you are trying to do will never work, find another way :) see the graph protocol or other resources that work to index the blockchain.

Back to the Question:

There is a difference between from, to, value in the transaction and the from, to, value in the logs included in the transaction receipt

web3.eth.getTransaction('0x9fc7641.....').then(console.log);

{ "hash": "0x9fc7641...", "nonce": 2, "blockHash": "0xef95f2...", "blockNumber": 3, "transactionIndex": 0, "from": "0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b", "to": "0x6295ee1b4f6dd65047762f924ecd367c17eabf8f", "value": '123450000000000000', "gas": 314159, "gasPrice": '2000000000000', "input": "0x57cb2fc4" }

Here from: is the transaction sender, to: the contract address (could be another contract and not the token contract) and the value is the amount of ether in Wei.

And var receipt = web3.eth.getTransactionReceipt('0x9fc764...').then(console.log);

{ "status": true, "transactionHash": "0x9fc7641...", "transactionIndex": 0, "blockHash": "0xef95f...", "blockNumber": 3, "contractAddress": "0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe", "cumulativeGasUsed": 314159, "gasUsed": 30234, "logs": [{ // logs where the emitted events are shown }, ...] }

If the Token is ERC20 then in the logs you will see a Transfer event which includes from: the old owner of the token, to: the new owner of the token, value/amount: is the amount of token that is transfered. (ignor the "contract address" it is only shown if a contract is deployed in the transaction)

So long story short, if the user interacting with the contract directly and not through routers and others contract then you need both. The first one to get the contract address, and the second one to get the from, to, and token amount

I have no idea how to get the contract address if the sender using a router or somehow other contracts, probably there are other events to track

2
  • Thank you for your detailed reply, I appreciate it even more so cause I am new to the whole topic. Could you clarify what the normal or usual process is, when for example someone buys a token with his eth balance on an exchange like binance for example? Are these buy transactions showed in the blockchain and retrievable through web3? I just want to monitor which wallet exchanged eth for something and then have that outputted to me. – Chris S May 7 at 17:24
  • no the buy, transfer and trade on centralized exchanges like binance and coinbase are not on Chain, those tokens are managed and traded inside the exchange's servers and not on the blockchain, so you can't trace them. only the deposit and withdrawal from centralized exchange happen on different chains. – Majd TL May 8 at 12:05

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.