ERC20 Tokens Transferred Information from Transaction Hash
Asked Answered
S

1

6

EtherScan provides a API for transaction details It's part of Geth/Parity Proxy APIs by name eth_getTransactionByHash, But I am unable to get information what ERC20 token was transferred and how many.

I need Token Details and Number of token transferred with the help of Transaction Hash.
enter image description here

Sexism answered 7/9, 2018 at 12:37 Comment(0)
E
23

You are using the wrong API.

To get an ERC20 transfer's information, you need the transaction receipt, since transfer information is recorded in a transfer event log. You should be using eth_getTransactionReceipt.

This will give you a response such as this, for this tx:

{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "blockHash": "0xc5e5a515898983d1370d40b03fc05ae08be861af746a1577796153a149a1bb20",
    "blockNumber": "0x5ff5dd",
    "contractAddress": null,
    "cumulativeGasUsed": "0xe85fb",
    "from": "0xd7afd4441fccc118b9207b0e136f4ef9319b3c79",
    "gasUsed": "0x9034",
    "logs": [
      {
        "address": "0x0d8775f648430679a709e98d2b0cb6250d2887ef",
        "topics": [
          "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
          "0x000000000000000000000000d7afd4441fccc118b9207b0e136f4ef9319b3c79",
          "0x00000000000000000000000069d9e9aff57ec73582ad1ce441726dba7ea78fe0"
        ],
        "data": "0x0000000000000000000000000000000000000000000001054aefee8ba6d00000",
        "blockNumber": "0x5ff5dd",
        "transactionHash": "0x3265c1461d3f167c756fbc062ae3a2dc279b44a9c3ca2194271d4251cd0c1655",
        "transactionIndex": "0x1b",
        "blockHash": "0xc5e5a515898983d1370d40b03fc05ae08be861af746a1577796153a149a1bb20",
        "logIndex": "0xa",
        "removed": false
      }
    ],
    "logsBloom": "0x04000000002000000200000000000000002000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000008000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000200010000000000000000000000000000000000000000000000100000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    "status": "0x1",
    "to": "0x0d8775f648430679a709e98d2b0cb6250d2887ef",
    "transactionHash": "0x3265c1461d3f167c756fbc062ae3a2dc279b44a9c3ca2194271d4251cd0c1655",
    "transactionIndex": "0x1b"
  }
}

Of this, this logs section is important.

The format of an ERC20 transfer log is Transfer(address from, address to, uint256 value). When you take the keccak hash for Transfer(address,address,uint256), you get 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef for the topic, as shown in the response above.

The next two value in that log are the from and to addresses respectively, packed as normal for ETH (zero padded until 32 bytes). Lastly, the data within the log is the value of the ERC20 token that was transferred (BAT in this example).

The address that emits the log, 0x0d8775f648430679a709e98d2b0cb6250d2887ef in this case, is the token contract. You can then read the token symbol, name, and decimals from this contract using eth_call API to read the token information.

Ever answered 7/9, 2018 at 15:2 Comment(2)
"When you take the keccak hash for Transfer(address,address,uint256), you get 0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef" This has just helped me so much, thank you!Leucocratic
On a side note, is there an easy way to find the other Keccak hashes for other function logs?Leucocratic

© 2022 - 2024 — McMap. All rights reserved.