@secondlayer/sdk 5.9.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.
@@ -1,7 +1,81 @@
1
1
  declare const STREAMS_EVENT_TYPES: readonly ["stx_transfer", "stx_mint", "stx_burn", "stx_lock", "ft_transfer", "ft_mint", "ft_burn", "nft_transfer", "nft_mint", "nft_burn", "print"];
2
2
  type StreamsEventType = (typeof STREAMS_EVENT_TYPES)[number];
3
- type StreamsEventPayload = Record<string, unknown>;
4
- type StreamsEvent = {
3
+ /** A Clarity value as Streams serves it: the canonical hex string, a typed
4
+ * object carrying that hex (`{ hex }`), or a decoded Clarity-JSON object.
5
+ * Decode helpers (`decodeNftTransfer`, etc.) resolve it to a concrete value. */
6
+ type StreamsClarityValue = string | {
7
+ hex: string
8
+ } | Record<string, unknown>;
9
+ type StxTransferPayload = {
10
+ sender: string
11
+ recipient: string
12
+ amount: string
13
+ memo?: string
14
+ };
15
+ type StxMintPayload = {
16
+ recipient: string
17
+ amount: string
18
+ };
19
+ type StxBurnPayload = {
20
+ sender: string
21
+ amount: string
22
+ };
23
+ type StxLockPayload = {
24
+ locked_address: string
25
+ locked_amount: string
26
+ unlock_height: string
27
+ };
28
+ type FtTransferPayload = {
29
+ asset_identifier: string
30
+ sender: string
31
+ recipient: string
32
+ amount: string
33
+ };
34
+ type FtMintPayload = {
35
+ asset_identifier: string
36
+ recipient: string
37
+ amount: string
38
+ };
39
+ type FtBurnPayload = {
40
+ asset_identifier: string
41
+ sender: string
42
+ amount: string
43
+ };
44
+ type NftTransferPayload = {
45
+ asset_identifier: string
46
+ sender: string
47
+ recipient: string
48
+ value: StreamsClarityValue
49
+ /** Canonical serialized hex of `value`, when the stream carries it. */
50
+ raw_value?: string
51
+ };
52
+ type NftMintPayload = {
53
+ asset_identifier: string
54
+ recipient: string
55
+ value: StreamsClarityValue
56
+ raw_value?: string
57
+ };
58
+ type NftBurnPayload = {
59
+ asset_identifier: string
60
+ sender: string
61
+ value: StreamsClarityValue
62
+ raw_value?: string
63
+ };
64
+ type PrintPayload = {
65
+ contract_id?: string | null
66
+ topic?: string
67
+ value?: unknown
68
+ raw_value?: string
69
+ };
70
+ /** Union of every Streams payload shape, discriminated by `event_type` on the
71
+ * parent `StreamsEvent`. */
72
+ type StreamsEventPayload = StxTransferPayload | StxMintPayload | StxBurnPayload | StxLockPayload | FtTransferPayload | FtMintPayload | FtBurnPayload | NftTransferPayload | NftMintPayload | NftBurnPayload | PrintPayload;
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
+ */
5
79
  cursor: string
6
80
  block_height: number
7
81
  block_hash: string
@@ -9,9 +83,7 @@ type StreamsEvent = {
9
83
  tx_id: string
10
84
  tx_index: number
11
85
  event_index: number
12
- event_type: StreamsEventType
13
86
  contract_id: string | null
14
- payload: StreamsEventPayload
15
87
  ts: string
16
88
  /**
17
89
  * True when this event's block is past the finality boundary (immutable).
@@ -19,6 +91,16 @@ type StreamsEvent = {
19
91
  */
20
92
  finalized?: boolean
21
93
  };
94
+ type StreamsEventOf<
95
+ T extends StreamsEventType,
96
+ P
97
+ > = StreamsEventBase & {
98
+ event_type: T
99
+ payload: P
100
+ };
101
+ /** A raw Streams event. Discriminated on `event_type`, so `event.payload`
102
+ * narrows to the matching payload shape once the type is checked. */
103
+ type StreamsEvent = StreamsEventOf<"stx_transfer", StxTransferPayload> | StreamsEventOf<"stx_mint", StxMintPayload> | StreamsEventOf<"stx_burn", StxBurnPayload> | StreamsEventOf<"stx_lock", StxLockPayload> | StreamsEventOf<"ft_transfer", FtTransferPayload> | StreamsEventOf<"ft_mint", FtMintPayload> | StreamsEventOf<"ft_burn", FtBurnPayload> | StreamsEventOf<"nft_transfer", NftTransferPayload> | StreamsEventOf<"nft_mint", NftMintPayload> | StreamsEventOf<"nft_burn", NftBurnPayload> | StreamsEventOf<"print", PrintPayload>;
22
104
  type StreamsTip = {
23
105
  block_height: number
24
106
  block_hash: string
@@ -61,23 +143,28 @@ type StreamsReorgsListEnvelope = {
61
143
  reorgs: StreamsReorg[]
62
144
  next_since: string | null
63
145
  };
146
+ /** A filter that matches a single value or any value in a list. */
147
+ type StreamsFilterValue = string | readonly string[];
64
148
  type StreamsEventsListParams = {
65
149
  cursor?: string | null
66
150
  fromHeight?: number
67
151
  toHeight?: number
68
152
  types?: readonly StreamsEventType[]
69
- contractId?: string
70
- sender?: string
71
- recipient?: string
153
+ /** Event types to exclude (applied after `types`). */
154
+ notTypes?: readonly StreamsEventType[]
155
+ contractId?: StreamsFilterValue
156
+ sender?: StreamsFilterValue
157
+ recipient?: StreamsFilterValue
72
158
  assetIdentifier?: string
73
159
  limit?: number
74
160
  };
75
161
  type StreamsEventsStreamParams = {
76
162
  fromCursor?: string | null
77
163
  types?: readonly StreamsEventType[]
78
- contractId?: string
79
- sender?: string
80
- recipient?: string
164
+ notTypes?: readonly StreamsEventType[]
165
+ contractId?: StreamsFilterValue
166
+ sender?: StreamsFilterValue
167
+ recipient?: StreamsFilterValue
81
168
  assetIdentifier?: string
82
169
  batchSize?: number
83
170
  emptyBackoffMs?: number
@@ -85,16 +172,53 @@ type StreamsEventsStreamParams = {
85
172
  maxEmptyPolls?: number
86
173
  signal?: AbortSignal
87
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
+ };
88
191
  type StreamsEventsConsumeParams = {
89
192
  fromCursor?: string | null
90
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
91
201
  types?: readonly StreamsEventType[]
92
- contractId?: string
93
- sender?: string
94
- recipient?: string
202
+ notTypes?: readonly StreamsEventType[]
203
+ contractId?: StreamsFilterValue
204
+ sender?: StreamsFilterValue
205
+ recipient?: StreamsFilterValue
95
206
  assetIdentifier?: string
96
207
  batchSize?: number
97
- 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
98
222
  emptyBackoffMs?: number
99
223
  maxPages?: number
100
224
  maxEmptyPolls?: number
@@ -247,16 +371,9 @@ declare class StreamsServerError extends Error {
247
371
  declare class StreamsSignatureError extends Error {
248
372
  constructor(message?: string);
249
373
  }
250
- type FtTransferPayload = {
251
- asset_identifier: string
252
- sender: string
253
- recipient: string
254
- amount: string
255
- };
256
- type FtTransferEvent = StreamsEvent & {
374
+ type FtTransferEvent = Extract<StreamsEvent, {
257
375
  event_type: "ft_transfer"
258
- payload: FtTransferPayload
259
- };
376
+ }>;
260
377
  type DecodedFtTransferPayload = {
261
378
  asset_identifier: string
262
379
  contract_id: string
@@ -277,18 +394,9 @@ type DecodedFtTransfer = {
277
394
  };
278
395
  declare function isFtTransfer(event: StreamsEvent): event is FtTransferEvent;
279
396
  declare function decodeFtTransfer(event: StreamsEvent): DecodedFtTransfer;
280
- type NftTransferPayload = {
281
- asset_identifier: string
282
- sender: string
283
- recipient: string
284
- value: string | {
285
- hex: string
286
- }
287
- };
288
- type NftTransferEvent = StreamsEvent & {
397
+ type NftTransferEvent = Extract<StreamsEvent, {
289
398
  event_type: "nft_transfer"
290
- payload: NftTransferPayload
291
- };
399
+ }>;
292
400
  type DecodedNftTransferPayload = {
293
401
  asset_identifier: string
294
402
  contract_id: string
@@ -512,5 +620,24 @@ type DecodedEventColumns = {
512
620
  /** JSONB overflow for non-flat types (e.g. print's decoded value). */
513
621
  payload?: unknown
514
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
+ };
515
642
  type DecodedEventRow = DecodedFtTransfer | DecodedNftTransfer | DecodedStxTransfer | DecodedStxMint | DecodedStxBurn | DecodedStxLock | DecodedFtMint | DecodedFtBurn | DecodedNftMint | DecodedNftBurn | DecodedPrint;
516
- 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 };