Skip to main content
BlockScanner from tonutils.tools.block_scanner follows the masterchain, discovers every new shard block by walking back from the shard tips each masterchain block references, and emits typed events for each block and its transactions. Use it to build indexers, payment trackers, or any service that needs a live stream of on-chain activity.

Requirements

The scanner works over the native ADNL protocol and accepts only a LiteClient or LiteBalancer — HTTP clients are not supported. Connect the client before starting the scanner:
Public lite servers may be unstable under load. For continuous scanning, use a private lite-server config with LiteBalancer.from_config. Reduce rps_limit if you hit rate limits; increase it for faster scanning on dedicated nodes.

Storage

To survive restarts, give the scanner a storage object that persists the last processed masterchain seqno. Storage is any object matching BlockScannerStorageProtocol — two async methods: The scanner calls set_mc_seqno after each fully processed masterchain block; resume() reads the saved value with get_mc_seqno and continues from the next block. A minimal file-backed implementation:
Any backend works — a database row or a Redis key implement the same two methods. Storage is optional; without it the scanner runs, but resume() raises RuntimeError.

Handlers

Create the scanner and register handlers with decorators (or pass them as on_block= / on_transactions= / on_error= constructor arguments):
For each shard block, BlockEvent is emitted first, then TransactionsEvent with that block’s transactions. Transactions are fetched only when an on_transactions handler is registered. Exceptions raised inside on_block / on_transactions handlers are routed to the error handler; the on_error handler must never raise — exceptions inside it are silently dropped. All events are frozen dataclasses sharing three base fields: Event-specific fields: BlockEvent — a shard block discovered by the scanner:
  • block — shard block identifier (BlockIdExt with workchain, shard, seqno, root_hash, file_hash).
TransactionsEvent — transactions fetched for a shard block:
  • block — shard block identifier (BlockIdExt).
  • transactions — list of Transaction objects from this block.
ErrorEvent — an error raised during scanning or handler execution:
  • error — the raised exception.
  • event — the related event, or None.
  • handler — the handler (or client call) that raised, or None.
  • block — the related shard block, or None for masterchain-level errors.

Lifecycle

The scanner offers three entry points; each runs until stop() is called: Call stop() in a finally block so the loop shuts down cleanly, then close the client.

Full Example

Block Scanner Example

Full runnable block scanner script on GitHub.