5

I'm trying to find how etherscan finds the "Tokens Transferred" using the logs of a given transaction. Using this 0x52caaf79bf913064a70a6c9d917fd4190cdb099fe79e7d3a9dfe0600e1cfbc81 arbitrary transaction as an example.

enter image description here

I used the below code (web3.py) to find the logs in the transaction receipt:

from web3 import Web3
provider = Web3.HTTPProvider('https://mainnet.infura.io/v3/XXXXXXXXXXXXX')
w3 = Web3(provider)

receipt=  w3.eth.getTransactionReceipt('0x52caaf79bf913064a70a6c9d917fd4190cdb099fe79e7d3a9dfe0600e1cfbc81')

Running receipt["logs"] generates the logs which can be found in Etherscan under "event logs". However, only 3 log indices result in the tokens transferred section (logs 156, 158 and 162).

Can someone help me understand how does etherscan knows that only these should appear in the picture seen above, from amongst all the logs?

1 Answer 1

4

It searches for all events:

  1. Which have been emitted by the designated ERC20 token contract, which in your case is:

    To: Contract 0xc73e0383f3aff3215e6f04b0331d58cecf0ab849

    This value should appear in the to field of the transaction receipt

  2. Whose first topic (topics[0]) is equal to the hash of the signature of the Transfer event:

    In Javascript, this value is Web3.utils.keccak256("Transfer(address,address,uint256)")

    You may use the equivalent code in Python if you need to...

3
  • thanks for your answer, I'll write up a python script that takes in the receipt as an argument and returns all the transactions that took place, using the rules you mentioned above...
    – KALEB
    Feb 26, 2020 at 19:32
  • goodvibration can you please tell me if possible why I am not getting the SAI symbol?
    – KALEB
    Feb 26, 2020 at 22:25
  • @KALEB Do you have the python code for doing this somewhere? If possible, would you able to share it in the answers?
    – Jithin Ks
    Apr 28 at 5:39

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.