Sub topic 3
Tracking Token Transfers Through Event Logs
Assume you know what a blockchain transaction, wallet address, and smart contract are. You should also be comfortable reading basic Python and understand that Ethereum uses account balances, while Bitcoin commonly uses unspent transaction outputs (UTXOs). This chapter focuses on Ethereum-style token transfers.
Why Event Logs Matter
A native coin transfer, such as sending ETH, can be represented by a transaction value. Token transfers work differently. An ERC-20 token is controlled by a smart contract. When the contract processes a transfer, it usually emits a Transfer event.
An event log is a record attached to a transaction receipt. It contains:
- The contract address that emitted the event.
- A topic identifying the event type.
- Indexed event arguments in additional topics.
- Other event arguments in a data field.
- The transaction hash and block location.
For the standard ERC-20 event, the Solidity declaration is:
event Transfer(
address indexed from,
address indexed to,
uint256 value
);The first topic is the keccak-256 hash of the event signature:
Transfer(address,address,uint256)
The from and to addresses are indexed, so they appear as 32-byte values in topics. The token amount is normally encoded in the data field as a 256-bit unsigned integer.
Querying Logs
An Ethereum node exposes logs through the JSON-RPC method eth_getLogs. Filtering by the token contract prevents unrelated contracts from appearing in the result.
{
"jsonrpc": "2.0",
"id": 1,
"method": "eth_getLogs",
"params": [
{
"fromBlock": "0x10d4f00",
"toBlock": "0x10d4f0a",
"address": "<TOKEN_CONTRACT_ADDRESS>",
"topics": [
"0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
]
}
]
}The block numbers are hexadecimal. This request finds Transfer events emitted by one token contract during a small block range.
You can add a second topic to filter by a particular sender or recipient. An indexed address must be left-padded to 32 bytes. For example, a recipient filter belongs in the second topic position because from is the first indexed argument and to is the second.
Reading and Validating Results
A returned log typically includes topics, data, blockNumber, transactionHash, and logIndex. Decode the address topics and convert the hexadecimal amount to an integer. Then use the token contract's decimals metadata to display a human-readable amount:
display amount = raw amount / 10^decimals
Do not assume every token uses 18 decimals. The contract may use 6, 8, or another value.
| Field | Meaning |
|---|---|
| `address` | Contract that emitted the event |
| `topics[0]` | Event signature identifier |
| `topics[1]` | Indexed `from` address |
| `topics[2]` | Indexed `to` address |
| `data` | Raw token amount |
From Logs to Transfer Records
A useful transfer record keeps the block number, transaction hash, log index, sender, recipient, raw amount, and token contract. The pair (transactionHash, logIndex) usually identifies one log within a transaction. Store both values so repeated transfers in the same transaction are not accidentally merged.
Logs can be affected by chain reorganizations. A transaction first included in one block may later be replaced. Production systems should wait for confirmations, track block hashes, and remove or revise records from orphaned blocks.
Exercise
Why should a token-transfer query filter by both the token contract address and the Transfer event topic?
Show answer
The contract filter selects one token implementation, while the topic filter selects logs representing the Transfer event instead of other events emitted by that contract.