@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.
package/dist/index.d.ts CHANGED
@@ -417,6 +417,11 @@ type PrintPayload = {
417
417
  * parent `StreamsEvent`. */
418
418
  type StreamsEventPayload = StxTransferPayload | StxMintPayload | StxBurnPayload | StxLockPayload | FtTransferPayload | FtMintPayload | FtBurnPayload | NftTransferPayload | NftMintPayload | NftBurnPayload | PrintPayload;
419
419
  type StreamsEventBase = {
420
+ /**
421
+ * Globally unique, monotonic position of this event (`<block>:<index>`). Use
422
+ * it as the primary key of your projection rows — replaying a batch then
423
+ * upserts cleanly. Don't synthesize your own id from `tx_id`/`event_index`.
424
+ */
420
425
  cursor: string
421
426
  block_height: number
422
427
  block_hash: string
@@ -513,9 +518,32 @@ type StreamsEventsStreamParams = {
513
518
  maxEmptyPolls?: number
514
519
  signal?: AbortSignal
515
520
  };
521
+ /**
522
+ * The checkpoint the SDK computes for a batch. Persist `cursor` inside the same
523
+ * transaction as your projection writes, then resume from it via `fromCursor`.
524
+ * It is the position to advance to: `next_cursor` normally, or the last
525
+ * finalized event when `finalizedOnly` is set.
526
+ */
527
+ type StreamsBatchContext = {
528
+ cursor: string | null
529
+ };
530
+ /**
531
+ * The checkpoint for a reorg rollback. Persist `cursor` (the rewind position)
532
+ * inside the same transaction as your rollback so the two commit atomically.
533
+ */
534
+ type StreamsReorgContext = {
535
+ cursor: string
536
+ };
516
537
  type StreamsEventsConsumeParams = {
517
538
  fromCursor?: string | null
518
539
  mode?: "tail" | "bounded"
540
+ /**
541
+ * Emit only finalized (immutable) events and never surface reorgs. The SDK
542
+ * checkpoints at the last finalized event and re-reads the unfinalized tail
543
+ * until it settles. Trades finality lag for zero reorg handling; `onReorg` is
544
+ * ignored.
545
+ */
546
+ finalizedOnly?: boolean
519
547
  types?: readonly StreamsEventType[]
520
548
  notTypes?: readonly StreamsEventType[]
521
549
  contractId?: StreamsFilterValue
@@ -523,7 +551,20 @@ type StreamsEventsConsumeParams = {
523
551
  recipient?: StreamsFilterValue
524
552
  assetIdentifier?: string
525
553
  batchSize?: number
526
- onBatch: (events: StreamsEvent[], envelope: StreamsEventsEnvelope) => Promise<string | null | undefined> | string | null | undefined
554
+ /**
555
+ * Apply a page of canonical events. Persist `ctx.cursor` in the same
556
+ * transaction as your writes. Returning a cursor overrides `ctx.cursor` as
557
+ * the resume point (advanced manual control); returning nothing uses it.
558
+ */
559
+ onBatch: (events: StreamsEvent[], envelope: StreamsEventsEnvelope, ctx: StreamsBatchContext) => void | string | null | undefined | Promise<void> | Promise<string | null | undefined>
560
+ /**
561
+ * Roll your projection back to `reorg.fork_point_height`, persisting
562
+ * `ctx.cursor` in the same transaction. Called once per *new* reorg
563
+ * (deduped in-memory, fork-ascending) before the SDK rewinds and re-reads the
564
+ * now-canonical events. Omit it to ignore reorgs (events stay canonical, but
565
+ * stale rows from an orphaned fork are left in place).
566
+ */
567
+ onReorg?: (reorg: StreamsReorg, ctx: StreamsReorgContext) => Promise<void> | void
527
568
  emptyBackoffMs?: number
528
569
  maxPages?: number
529
570
  maxEmptyPolls?: number
@@ -979,6 +1020,25 @@ type DecodedEventColumns = {
979
1020
  /** JSONB overflow for non-flat types (e.g. print's decoded value). */
980
1021
  payload?: unknown
981
1022
  };
1023
+ /**
1024
+ * Helpers for Streams cursors. A cursor is the opaque `<block>:<index>` string
1025
+ * that marks a position in the event stream; treat the format as an
1026
+ * implementation detail and go through these helpers instead of string-building
1027
+ * it at call sites.
1028
+ */
1029
+ declare const Cursor: {
1030
+ /**
1031
+ * Cursor at the foot of `height`. Resuming from it re-reads every event
1032
+ * strictly above block `height` (cursors are exclusive), so this is the
1033
+ * position to rewind to after a reorg whose fork point is `height`.
1034
+ */
1035
+ atHeight(height: number): string
1036
+ /** Parse a `<block>:<index>` cursor. Throws `ValidationError` if malformed. */
1037
+ parse(cursor: string): {
1038
+ blockHeight: number
1039
+ eventIndex: number
1040
+ }
1041
+ };
982
1042
  type DecodedEventRow = DecodedFtTransfer | DecodedNftTransfer | DecodedStxTransfer | DecodedStxMint | DecodedStxBurn | DecodedStxLock | DecodedFtMint | DecodedFtBurn | DecodedNftMint | DecodedNftBurn | DecodedPrint;
983
1043
  /**
984
1044
  * Typed client for the Foundation Datasets REST API (`/v1/datasets/*`).
@@ -1103,7 +1163,9 @@ declare class Datasets extends BaseClient {
1103
1163
  summary: DatasetRow
1104
1164
  }>;
1105
1165
  private get;
1106
- private buildParams;
1166
+ /** Map camelCase filter fields to snake_case query keys (dropping pagination
1167
+ * controls) and build the canonical query suffix. */
1168
+ private paramsToQuery;
1107
1169
  private cursorDataset;
1108
1170
  }
1109
1171
  import { SubgraphAgentSchema as SubgraphAgentSchema3, SubgraphSpecFormat as SubgraphSpecFormat2, SubgraphSpecOptions as SubgraphSpecOptions3 } from "@secondlayer/shared/subgraphs/spec";
@@ -1224,4 +1286,4 @@ declare function toJsonSafe(value: unknown): unknown;
1224
1286
  /** Decode a hex-encoded Clarity value to JSON-safe JS (uints as strings,
1225
1287
  * buffers as `0x…` hex, tuples as objects). Returns the input hex on failure. */
1226
1288
  declare function decodeClarityValue(hex: string): unknown;
1227
- export { verifyWebhookSignature, toJsonSafe, isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, getSubgraph, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decodeClarityValue, createStreamsClient, VersionConflictError, ValidationError, UpdateSubscriptionRequest2 as UpdateSubscriptionRequest, Subscriptions, SubscriptionSummary2 as SubscriptionSummary, SubscriptionStatus, SubscriptionRuntime, SubscriptionFormat, SubscriptionDetail2 as SubscriptionDetail, Subgraphs, SubgraphSpecOptions3 as SubgraphSpecOptions, SubgraphSpecFormat2 as SubgraphSpecFormat, SubgraphAgentSchema3 as SubgraphAgentSchema, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, SecondLayerOptions, SecondLayer, RotateSecretResponse2 as RotateSecretResponse, ReplayResult2 as ReplayResult, RateLimitError, Pox4CallsParams, NftTransfersWalkParams, NftTransfersListParams, NftTransfersEnvelope, NftTransferPayload, NftTransferEvent, NftTransfer, IndexTip, IndexEventType, IndexEvent, IndexContractCall, Index, FtTransfersWalkParams, FtTransfersListParams, FtTransfersEnvelope, FtTransferPayload, FtTransferEvent, FtTransfer, FetchLike2 as FetchLike, EventsWalkParams, EventsListParams, EventsEnvelope, DeliveryRow2 as DeliveryRow, 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, DeadRow2 as DeadRow, Datasets, DatasetRow, CursorListParams, CursorEnvelope, CreateSubscriptionResponse2 as CreateSubscriptionResponse, CreateSubscriptionRequest2 as CreateSubscriptionRequest, ContractCallsWalkParams, ContractCallsListParams, ContractCallsEnvelope, CURSOR_SLUGS, AuthError, ApiError };
1289
+ export { verifyWebhookSignature, toJsonSafe, isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, getSubgraph, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decodeClarityValue, createStreamsClient, VersionConflictError, ValidationError, UpdateSubscriptionRequest2 as UpdateSubscriptionRequest, Subscriptions, SubscriptionSummary2 as SubscriptionSummary, SubscriptionStatus, SubscriptionRuntime, SubscriptionFormat, SubscriptionDetail2 as SubscriptionDetail, Subgraphs, SubgraphSpecOptions3 as SubgraphSpecOptions, SubgraphSpecFormat2 as SubgraphSpecFormat, SubgraphAgentSchema3 as SubgraphAgentSchema, StreamsTip, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, SecondLayerOptions, SecondLayer, RotateSecretResponse2 as RotateSecretResponse, ReplayResult2 as ReplayResult, RateLimitError, Pox4CallsParams, NftTransfersWalkParams, NftTransfersListParams, NftTransfersEnvelope, NftTransferPayload, NftTransferEvent, NftTransfer, IndexTip, IndexEventType, IndexEvent, IndexContractCall, Index, FtTransfersWalkParams, FtTransfersListParams, FtTransfersEnvelope, FtTransferPayload, FtTransferEvent, FtTransfer, FetchLike2 as FetchLike, EventsWalkParams, EventsListParams, EventsEnvelope, DeliveryRow2 as DeliveryRow, 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, DeadRow2 as DeadRow, Datasets, DatasetRow, CursorListParams, CursorEnvelope, Cursor, CreateSubscriptionResponse2 as CreateSubscriptionResponse, CreateSubscriptionRequest2 as CreateSubscriptionRequest, ContractCallsWalkParams, ContractCallsListParams, ContractCallsEnvelope, CURSOR_SLUGS, AuthError, ApiError };