Checkpoints
Commit your rows and the cursor together, and a crash resumes where it stopped.
, , and when a reorg lands.
import { Index } from "@secondlayer/sdk";
const index = new Index();
await index.events.consume({
eventType: "ft_transfer",
fromCursor: await db.loadCheckpoint(),
fromHeight: 0,
onBatch: async (events, ctx) => {
await db.transaction(async (tx) => {
for (const row of events) await tx.upsert(row);
await tx.saveCheckpoint(ctx.cursor);
});
return ctx.cursor;
},
onReorg: (reorg) => db.deleteFromHeight(reorg.fork_point_height),
});
fromCursorisnullon the first run, andfromHeight: 0backfills as far as your instance holds. Older history is restored from the archive, free.- A reorg rewinds the cursor to the fork point and fires
onReorg. Delete inclusive of the fork block: Reorgs. - Fold running balances in a sink's
onRollback: Sinks. - Where to run it: Deploy.
consume is on sl.streams.events, sl.index.events and sl.index.contractCalls. Every signature is in the SDK reference.
| Option | Effect |
|---|---|
eventType | A literal narrows the rows: "ft_transfer" hands onBatch typed amount and sender, "print" hands it payload.topic |
contractId | One id, or up to 20 to follow a protocol on one cursor. Mutually exclusive with trait |
txContext: true | Joins the submitting transaction into each event (tx_sender, tx_type, ...). Same on walk and list |
finalizedOnly: true | Holds delivery to rows at or below tip.finalized_height, so onReorg is unnecessary |
signal | Abort, checked between batches and never mid-commit; see Deploy |
index.contractCalls.consume() | The same loop for decoded calls. Already carries sender and function_name, so it needs no txContext |
finalizedOnly trades freshness for safety, and can put you hundreds of blocks behind the tip.
ctx says where the sweep has reached, so liveness needs no bookkeeping of its own.
| Field | Is |
|---|---|
cursor | The checkpoint to commit with your rows |
height | Highest canonical block a row was delivered from; parks on a quiet filter |
scannedHeight | Highest block verified; an empty page proves nothing matches up to the tip. Rolled back on a reorg |
tipHeight | Chain tip as of this page's read |
blocksBehind | tipHeight - scannedHeight, floored at 0; about 0 for a caught-up tail even when height is old |
Every feed offers three verbs. They are not interchangeable.
| Verb | Gives you | Follows cursors | Checkpoints | Reorgs | Ends |
|---|---|---|---|---|---|
list() | One page plus the envelope | No; you pass cursor back | No | Reports reorgs[]; you act | Immediately |
walk() | An async iterable of rows | Yes | No | Ignored | On catching the tip |
consume() | A loop calling your onBatch | Yes | Yes; you commit the cursor | Rewinds and fires onReorg | Never; tails until aborted |
walk() into a database is a trap: a crash restarts from zero, a fork leaves orphaned rows behind, and it stops at the tip. Writing rows you intend to keep is consume().