@secondlayer/sdk 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -71,6 +71,11 @@ type PrintPayload = {
71
71
  * parent `StreamsEvent`. */
72
72
  type StreamsEventPayload = StxTransferPayload | StxMintPayload | StxBurnPayload | StxLockPayload | FtTransferPayload | FtMintPayload | FtBurnPayload | NftTransferPayload | NftMintPayload | NftBurnPayload | PrintPayload;
73
73
  type StreamsEventBase = {
74
+ /**
75
+ * Globally unique, monotonic position of this event (`<block>:<index>`). Use
76
+ * it as the primary key of your projection rows — replaying a batch then
77
+ * upserts cleanly. Don't synthesize your own id from `tx_id`/`event_index`.
78
+ */
74
79
  cursor: string
75
80
  block_height: number
76
81
  block_hash: string
@@ -167,9 +172,32 @@ type StreamsEventsStreamParams = {
167
172
  maxEmptyPolls?: number
168
173
  signal?: AbortSignal
169
174
  };
175
+ /**
176
+ * The checkpoint the SDK computes for a batch. Persist `cursor` inside the same
177
+ * transaction as your projection writes, then resume from it via `fromCursor`.
178
+ * It is the position to advance to: `next_cursor` normally, or the last
179
+ * finalized event when `finalizedOnly` is set.
180
+ */
181
+ type StreamsBatchContext = {
182
+ cursor: string | null
183
+ };
184
+ /**
185
+ * The checkpoint for a reorg rollback. Persist `cursor` (the rewind position)
186
+ * inside the same transaction as your rollback so the two commit atomically.
187
+ */
188
+ type StreamsReorgContext = {
189
+ cursor: string
190
+ };
170
191
  type StreamsEventsConsumeParams = {
171
192
  fromCursor?: string | null
172
193
  mode?: "tail" | "bounded"
194
+ /**
195
+ * Emit only finalized (immutable) events and never surface reorgs. The SDK
196
+ * checkpoints at the last finalized event and re-reads the unfinalized tail
197
+ * until it settles. Trades finality lag for zero reorg handling; `onReorg` is
198
+ * ignored.
199
+ */
200
+ finalizedOnly?: boolean
173
201
  types?: readonly StreamsEventType[]
174
202
  notTypes?: readonly StreamsEventType[]
175
203
  contractId?: StreamsFilterValue
@@ -177,7 +205,20 @@ type StreamsEventsConsumeParams = {
177
205
  recipient?: StreamsFilterValue
178
206
  assetIdentifier?: string
179
207
  batchSize?: number
180
- onBatch: (events: StreamsEvent[], envelope: StreamsEventsEnvelope) => Promise<string | null | undefined> | string | null | undefined
208
+ /**
209
+ * Apply a page of canonical events. Persist `ctx.cursor` in the same
210
+ * transaction as your writes. Returning a cursor overrides `ctx.cursor` as
211
+ * the resume point (advanced manual control); returning nothing uses it.
212
+ */
213
+ onBatch: (events: StreamsEvent[], envelope: StreamsEventsEnvelope, ctx: StreamsBatchContext) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>
214
+ /**
215
+ * Roll your projection back to `reorg.fork_point_height`, persisting
216
+ * `ctx.cursor` in the same transaction. Called once per *new* reorg
217
+ * (deduped in-memory, fork-ascending) before the SDK rewinds and re-reads the
218
+ * now-canonical events. Omit it to ignore reorgs (events stay canonical, but
219
+ * stale rows from an orphaned fork are left in place).
220
+ */
221
+ onReorg?: (reorg: StreamsReorg, ctx: StreamsReorgContext) => Promise<void> | void
181
222
  emptyBackoffMs?: number
182
223
  maxPages?: number
183
224
  maxEmptyPolls?: number
@@ -579,5 +620,24 @@ type DecodedEventColumns = {
579
620
  /** JSONB overflow for non-flat types (e.g. print's decoded value). */
580
621
  payload?: unknown
581
622
  };
623
+ /**
624
+ * Helpers for Streams cursors. A cursor is the opaque `<block>:<index>` string
625
+ * that marks a position in the event stream; treat the format as an
626
+ * implementation detail and go through these helpers instead of string-building
627
+ * it at call sites.
628
+ */
629
+ declare const Cursor: {
630
+ /**
631
+ * Cursor at the foot of `height`. Resuming from it re-reads every event
632
+ * strictly above block `height` (cursors are exclusive), so this is the
633
+ * position to rewind to after a reorg whose fork point is `height`.
634
+ */
635
+ atHeight(height: number): string
636
+ /** Parse a `<block>:<index>` cursor. Throws `ValidationError` if malformed. */
637
+ parse(cursor: string): {
638
+ blockHeight: number
639
+ eventIndex: number
640
+ }
641
+ };
582
642
  type DecodedEventRow = DecodedFtTransfer | DecodedNftTransfer | DecodedStxTransfer | DecodedStxMint | DecodedStxBurn | DecodedStxLock | DecodedFtMint | DecodedFtBurn | DecodedNftMint | DecodedNftBurn | DecodedPrint;
583
- export { isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, createStreamsClient, ValidationError, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, STREAMS_EVENT_TYPES, RateLimitError, NftTransferPayload, NftTransferEvent, FtTransferPayload, FtTransferEvent, FetchLike, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, AuthError };
643
+ export { isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, createStreamsClient, ValidationError, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, STREAMS_EVENT_TYPES, RateLimitError, NftTransferPayload, NftTransferEvent, FtTransferPayload, FtTransferEvent, FetchLike, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, Cursor, AuthError };