Skip to main content
Build a production-ready ERC-20 transfer indexer that handles chain reorganizations correctly.

Overview

This example demonstrates building an indexer that:
  • Backfills historical Transfer events from a specific block range
  • Watches for new blocks in real-time
  • Handles chain reorgs by removing re-orged transfers and adding new ones
  • Parses ERC-20 Transfer events from transaction receipts

Full Example

Key Patterns

Computing Event Signatures

Use Keccak256 to compute the event topic hash:

Parsing Indexed Parameters

ERC-20 Transfer has two indexed address parameters stored in topics:

Handling Reorgs

BlockStream emits a reorg event with both removed and added blocks:

Graceful Shutdown

Use AbortController to cleanly stop the watch loop:

Production Considerations

This example uses an in-memory database for simplicity. In production, you should:
  • Use a persistent database (PostgreSQL, SQLite, etc.)
  • Implement database transactions for atomicity during reorgs
  • Add retry logic for RPC failures
  • Implement checkpointing to resume from the last processed block

Database Transactions

Wrap reorg handling in a database transaction:

Checkpointing

Save progress to resume after restarts: