Setup Watchers (Blockchain Indexers)
Overview
Watchers are services that index blockchain data (blocks, transactions, events) and feed deposits and other on-chain events into the platform (DB, ledger, notifications).
What watchers do
- Connect to chain RPC (TON, TRON, etc.).
- Track last processed block/cursor (stored in DB or file).
- Poll or subscribe for new blocks/transactions.
- Parse transfers and contract events (e.g. TRC20).
- Persist deposits and emit events (e.g. NATS) for downstream (wallet, bot, ledger).
High-level flow
flowchart LR
A[Chain RPC] --> B[Watcher]
B --> C[Cursor Store]
B --> D[Deposit / Event]
D --> E[DB + Outbox]
E --> F[NATS / Bot]
Setup steps
1. Cursor and idempotency
- Store
chain_id+last_block(orlast_tx_hash) in a table (e.g.chain_cursors). - On restart, resume from last persisted cursor; never process the same block twice (idempotent inserts by
tx_hashor equivalent).
2. Configuration
- RPC URL, poll interval, confirmation depth (e.g. 12 blocks).
- Use env or config file; no secrets in code.
3. Deposit pipeline
- Parse tx → user (e.g. by deposit address from Wallet), amount, asset, tx_hash.
- Insert into
deposits(or equivalent) with idempotency key. - Write to outbox for notifications (e.g. “Deposit confirmed”) and ledger credits.
4. Reorg handling
- For PoW/PoS chains, use confirmation depth; for TON/TRON, follow chain best practices (e.g. finality rules).
Where watchers live
- TON/TRON watchers may live in the Watcher service or in chain-specific packages under
packages/blockchain/. - Gateway does not run watchers; they run as separate processes or containers.
Related
- Microservices
- Database Schema
- Migration Phase 0 (TON/TRON watchers)