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 aLiteClient 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 matchingBlockScannerStorageProtocol — 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:
resume() raises RuntimeError.
Handlers
Create the scanner and register handlers with decorators (or pass them ason_block= / on_transactions= / on_error= constructor arguments):
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 (BlockIdExtwithworkchain,shard,seqno,root_hash,file_hash).
TransactionsEvent — transactions fetched for a shard block:
block— shard block identifier (BlockIdExt).transactions— list ofTransactionobjects from this block.
ErrorEvent — an error raised during scanning or handler execution:
error— the raised exception.event— the related event, orNone.handler— the handler (or client call) that raised, orNone.block— the related shard block, orNonefor masterchain-level errors.
Lifecycle
The scanner offers three entry points; each runs untilstop() 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.