Handle ERC-20 event quirks
Not every token emits a canonical Transfer. Indexing by event alone under-counts tokens that move funds without one, and mis-scales those with unusual decimals.
Procedure
- Match the canonical topic, remembering ERC-20 amounts are not indexed here:
topic0 = keccak("Transfer(address,address,uint256)") = 0xddf252ad...23b3ef;fromandtoare indexed,valuesits in data. - Tokens that return no boolean (USDT-style) cannot be judged by a call's return; the event is the record. Do not require a return value.
- Read
decimals()once per token and store it; an 18-decimal default is wrong for USDC (6) and WBTC (8):cast call $TOKEN "decimals()(uint8)" --rpc-url $RPC - For fee-on-transfer tokens, trust the emitted
valueover a computed balance delta; index the event amount. - Detect event-less movers by comparing indexed balance deltas against event sums per block; flag tokens where they diverge.
- Store the raw amount and the decimals; render human units from both, never bake the scaled value into storage.
- Cache
decimals()andsymbol()per token in a metadata table so re-indexing does not spameth_call. - For tokens whose
Transfertopic differs, match the exact 32-byte topic0, not a prefix.
Pitfalls
- Assuming every token has 18 decimals scales USDC by 1e12 wrongly.
- Requiring a boolean return treats USDT-like tokens (which return nothing) as failures and drops real transfers.
- Rebase tokens change balances with no
Transfer; event-only indexing misses them. - Proxy tokens emit
Transferfrom the proxy or the implementation; filter on both when the token was upgraded. - A token upgraded behind a proxy can change
decimals()mid-history; store decimals per block range if it ever changes. - Assuming
valueis uint256 for a signed-amount token mis-decodes; check the ABI rather than the convention.
Verification
cast call $TOKEN "decimals()(uint8)" --rpc-url $RPC
psql -c "SELECT sum(value) FROM transfers WHERE token=$TOKEN AND block_number BETWEEN $A AND $B;"
# compare against the holder balanceOf delta across the same range
Report the token's decimals, whether it returns a boolean, and event-sum versus balance-delta agreement.