@secondlayer/shared 8.0.0 → 9.0.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.
Files changed (34) hide show
  1. package/dist/src/archive/range-digest.d.ts +16 -0
  2. package/dist/src/archive/semantic-digest-builder.d.ts +16 -0
  3. package/dist/src/archive/status.js +11 -3
  4. package/dist/src/archive/status.js.map +3 -3
  5. package/dist/src/coverage/index.d.ts +15 -0
  6. package/dist/src/db/index.d.ts +18 -1
  7. package/dist/src/db/index.js +2 -1
  8. package/dist/src/db/index.js.map +3 -3
  9. package/dist/src/db/queries/chain-reorgs.d.ts +16 -0
  10. package/dist/src/db/queries/chain-reorgs.js +2 -1
  11. package/dist/src/db/queries/chain-reorgs.js.map +3 -3
  12. package/dist/src/db/queries/contracts.d.ts +16 -0
  13. package/dist/src/db/queries/instance.d.ts +16 -0
  14. package/dist/src/db/queries/integrity.d.ts +16 -0
  15. package/dist/src/db/queries/subgraph-gaps.d.ts +16 -0
  16. package/dist/src/db/queries/subgraph-operations.d.ts +16 -0
  17. package/dist/src/db/queries/subgraphs.d.ts +16 -0
  18. package/dist/src/db/queries/subscriptions.d.ts +16 -0
  19. package/dist/src/db/queries/sync-scope.d.ts +1006 -0
  20. package/dist/src/db/queries/sync-scope.js +116 -0
  21. package/dist/src/db/queries/sync-scope.js.map +10 -0
  22. package/dist/src/db/schema.d.ts +17 -1
  23. package/dist/src/index.d.ts +18 -1
  24. package/dist/src/index.js +2 -1
  25. package/dist/src/index.js.map +3 -3
  26. package/dist/src/node/local-client.d.ts +16 -0
  27. package/dist/src/runtime/index.d.ts +137 -18
  28. package/dist/src/runtime/index.js +261 -41
  29. package/dist/src/runtime/index.js.map +9 -7
  30. package/migrations/0120_sync_scopes.ts +42 -0
  31. package/package.json +6 -6
  32. package/dist/src/x402.d.ts +0 -47
  33. package/dist/src/x402.js +0 -79
  34. package/dist/src/x402.js.map +0 -10
@@ -0,0 +1,1006 @@
1
+ import { Kysely } from "kysely";
2
+ /**
3
+ * Coverage-kernel constraints. SQL CHECKs in migration 0116 must stay
4
+ * identical — property tests treat this file as the spec.
5
+ */
6
+ declare const STAGE_KINDS: readonly ["raw", "decode", "subgraph", "queue"];
7
+ type StageKind = (typeof STAGE_KINDS)[number];
8
+ declare const NATIVE_CLOCKS: readonly ["block", "cursor", "queue"];
9
+ type NativeClock = (typeof NATIVE_CLOCKS)[number];
10
+ declare const REPAIR_MODES: readonly ["range_safe", "full_reindex", "archive_replay", "none"];
11
+ type RepairMode = (typeof REPAIR_MODES)[number];
12
+ declare const RUN_STATUSES: readonly ["pending", "running", "complete", "syncing", "lagging", "gap", "stale", "failed", "unverified_import", "unanchored", "source_unavailable", "out_of_scope", "disabled", "halted"];
13
+ type RunStatus = (typeof RUN_STATUSES)[number];
14
+ declare const FAILURE_UNITS: readonly ["block", "range", "cursor", "queue"];
15
+ type FailureUnit = (typeof FAILURE_UNITS)[number];
16
+ declare const FAILURE_CLASSES: readonly ["omission", "version", "digest_mismatch", "crash", "reorg", "source_gap", "handler", "timeout", "unknown"];
17
+ type FailureClass = (typeof FAILURE_CLASSES)[number];
18
+ declare const RETRY_STATES: readonly ["open", "retrying", "halted", "resolved"];
19
+ type RetryState = (typeof RETRY_STATES)[number];
20
+ type BootstrapSource = "archive" | "genesis" | "import";
21
+ type SyncScope = {
22
+ network: string
23
+ start_height: number
24
+ target_height: number | null
25
+ bootstrap: {
26
+ source: BootstrapSource
27
+ manifest_digest: string | null
28
+ genesis_hash: string | null
29
+ }
30
+ };
31
+ import { ColumnType, Generated } from "kysely";
32
+ interface BlocksTable {
33
+ height: number;
34
+ hash: string;
35
+ parent_hash: string;
36
+ burn_block_height: number;
37
+ burn_block_hash: ColumnType<string | null, string | null | undefined, string | null>;
38
+ /** Nakamoto StacksBlockId. Null on rows ingested before it was persisted. */
39
+ index_block_hash: ColumnType<string | null, string | null | undefined, string | null>;
40
+ timestamp: number;
41
+ canonical: Generated<boolean>;
42
+ created_at: Generated<Date>;
43
+ }
44
+ interface TransactionsTable {
45
+ tx_id: string;
46
+ block_height: number;
47
+ tx_index: Generated<number>;
48
+ type: string;
49
+ sender: string;
50
+ status: string;
51
+ contract_id: string | null;
52
+ function_name: string | null;
53
+ function_args: Generated<unknown | null>;
54
+ raw_result: Generated<string | null>;
55
+ raw_tx: string;
56
+ created_at: Generated<Date>;
57
+ }
58
+ interface EventsTable {
59
+ id: Generated<string>;
60
+ tx_id: string;
61
+ block_height: number;
62
+ event_index: number;
63
+ type: string;
64
+ data: unknown;
65
+ created_at: Generated<Date>;
66
+ }
67
+ interface TransactionsArchiveTable {
68
+ archive_id: Generated<string>;
69
+ tx_id: string;
70
+ block_height: number;
71
+ tx_index: number;
72
+ type: string;
73
+ sender: string;
74
+ status: string;
75
+ contract_id: string | null;
76
+ function_name: string | null;
77
+ function_args: unknown | null;
78
+ raw_result: string | null;
79
+ raw_tx: string;
80
+ created_at: Date;
81
+ orphaned_block_hash: string | null;
82
+ archived_at: Generated<Date>;
83
+ }
84
+ interface EventsArchiveTable {
85
+ archive_id: Generated<string>;
86
+ id: string;
87
+ tx_id: string;
88
+ block_height: number;
89
+ event_index: number;
90
+ type: string;
91
+ data: unknown;
92
+ created_at: Date;
93
+ orphaned_block_hash: string | null;
94
+ archived_at: Generated<Date>;
95
+ }
96
+ interface DeadLetterEventsTable {
97
+ id: Generated<string>;
98
+ block_height: number;
99
+ tx_id: string;
100
+ event_index: number;
101
+ event_type: string;
102
+ data: unknown;
103
+ reason: string;
104
+ created_at: Generated<Date>;
105
+ }
106
+ interface MempoolTransactionsTable {
107
+ seq: Generated<string>;
108
+ tx_id: string;
109
+ raw_tx: string;
110
+ type: string;
111
+ sender: string;
112
+ contract_id: string | null;
113
+ function_name: string | null;
114
+ function_args: unknown | null;
115
+ received_at: Generated<Date>;
116
+ }
117
+ interface IndexProgressTable {
118
+ network: string;
119
+ last_indexed_block: Generated<number>;
120
+ last_contiguous_block: Generated<number>;
121
+ highest_seen_block: Generated<number>;
122
+ updated_at: Generated<Date>;
123
+ }
124
+ interface SubgraphsTable {
125
+ id: Generated<string>;
126
+ name: string;
127
+ version: Generated<string>;
128
+ status: Generated<string>;
129
+ definition: Record<string, unknown>;
130
+ schema_hash: string;
131
+ handler_path: string;
132
+ schema_name: string | null;
133
+ start_block: Generated<number>;
134
+ last_processed_block: Generated<number>;
135
+ reindex_from_block: number | null;
136
+ reindex_to_block: number | null;
137
+ last_error: string | null;
138
+ last_error_at: Date | null;
139
+ total_processed: Generated<number>;
140
+ total_errors: Generated<number>;
141
+ account_id: string;
142
+ handler_code: string | null;
143
+ source_code: string | null;
144
+ project_id: string | null;
145
+ visibility: Generated<string>;
146
+ /** Paid (wallet-ghost) deploys expire unless renewed or claimed; NULL = no expiry. */
147
+ expires_at: Date | null;
148
+ /** (event type, contract) probe pairs persisted at deploy for weight classification. */
149
+ sparse_probe_targets: unknown | null;
150
+ database_url_enc: ColumnType<Buffer | null, Buffer | null | undefined, Buffer | null>;
151
+ sandbox_workers: Generated<boolean>;
152
+ created_at: Generated<Date>;
153
+ updated_at: Generated<Date>;
154
+ }
155
+ interface ContractsTable {
156
+ contract_id: string;
157
+ deployer: string;
158
+ block_height: number;
159
+ canonical: Generated<boolean>;
160
+ abi: unknown | null;
161
+ declared_traits: Generated<string[]>;
162
+ inferred_standards: Generated<string[]>;
163
+ abi_status: Generated<string>;
164
+ abi_fetched_at: Date | null;
165
+ created_at: Generated<Date>;
166
+ }
167
+ interface SubgraphGapsTable {
168
+ id: Generated<string>;
169
+ subgraph_id: string;
170
+ subgraph_name: string;
171
+ gap_start: number;
172
+ gap_end: number;
173
+ reason: string;
174
+ detected_at: Generated<Date>;
175
+ resolved_at: Date | null;
176
+ }
177
+ type SubgraphOperationKind = "reindex" | "backfill";
178
+ type SubgraphOperationStatus = "queued" | "running" | "completed" | "failed" | "cancelled";
179
+ interface SubgraphOperationsTable {
180
+ id: Generated<string>;
181
+ subgraph_id: string;
182
+ subgraph_name: string;
183
+ account_id: string | null;
184
+ kind: ColumnType<SubgraphOperationKind, SubgraphOperationKind, SubgraphOperationKind>;
185
+ status: ColumnType<SubgraphOperationStatus, SubgraphOperationStatus | undefined, SubgraphOperationStatus>;
186
+ from_block: number | null;
187
+ to_block: number | null;
188
+ cancel_requested: Generated<boolean>;
189
+ locked_by: string | null;
190
+ locked_until: Date | null;
191
+ started_at: Date | null;
192
+ finished_at: Date | null;
193
+ processed_blocks: number | null;
194
+ error: string | null;
195
+ created_at: Generated<Date>;
196
+ updated_at: Generated<Date>;
197
+ /** 'light' (contract-scoped sparse) | 'heavy' (broad). Claim budgets heavy. */
198
+ weight: Generated<string>;
199
+ /** Candidate-event denominator computed at enqueue (sparse ops only). */
200
+ estimated_events: string | number | bigint | null;
201
+ /** Events processed so far — written by the progress flush. */
202
+ processed_events: string | number | bigint | null;
203
+ /** Backfill ops' own crash checkpoint — advanced conditionally in-tx with
204
+ * each written block; replays skip at/below it. NULL for reindex ops
205
+ * (those checkpoint on subgraphs.last_processed_block). */
206
+ cursor_block: string | number | bigint | null;
207
+ }
208
+ interface ApiKeysTable {
209
+ id: Generated<string>;
210
+ key_hash: string;
211
+ key_prefix: string;
212
+ name: string | null;
213
+ status: Generated<string>;
214
+ rate_limit: Generated<number>;
215
+ ip_address: string;
216
+ account_id: string;
217
+ product: Generated<"account" | "streams" | "index">;
218
+ tier: "free" | "build" | "scale" | "enterprise" | null;
219
+ last_used_at: Date | null;
220
+ revoked_at: Date | null;
221
+ created_at: Generated<Date>;
222
+ }
223
+ /** Singleton self-host / publisher identity. Network is immutable. */
224
+ interface InstancesTable {
225
+ id: Generated<string>;
226
+ network: string;
227
+ created_at: Generated<Date>;
228
+ }
229
+ interface AccountsTable {
230
+ id: Generated<string>;
231
+ /** NULL for unclaimed ghost accounts (anonymous self-serve keys). */
232
+ email: string | null;
233
+ /** True for anonymous self-serve accounts until claimed via magic link. */
234
+ ghost: Generated<boolean>;
235
+ /** Stacks principal owning a wallet-ghost account (x402-paid deploys). */
236
+ wallet_principal: string | null;
237
+ display_name: string | null;
238
+ bio: string | null;
239
+ avatar_url: string | null;
240
+ stripe_customer_id: string | null;
241
+ /** Opt-out toggle for the subgraph reindex-completion email. Defaults true. */
242
+ notify_reindex_complete: Generated<boolean>;
243
+ created_at: Generated<Date>;
244
+ }
245
+ interface SessionsTable {
246
+ id: Generated<string>;
247
+ token_hash: string;
248
+ token_prefix: string;
249
+ account_id: string;
250
+ ip_address: string;
251
+ expires_at: Generated<Date>;
252
+ revoked_at: Date | null;
253
+ last_used_at: Date | null;
254
+ created_at: Generated<Date>;
255
+ }
256
+ interface MagicLinksTable {
257
+ id: Generated<string>;
258
+ email: string;
259
+ token: string;
260
+ code: string | null;
261
+ expires_at: Date;
262
+ used_at: Date | null;
263
+ failed_attempts: Generated<number>;
264
+ created_at: Generated<Date>;
265
+ }
266
+ interface SubgraphProcessingStatsTable {
267
+ id: Generated<string>;
268
+ subgraph_name: string;
269
+ api_key_id: string | null;
270
+ bucket_start: Date | null;
271
+ bucket_end: Date | null;
272
+ blocks_processed: number | null;
273
+ total_time_ms: number | null;
274
+ handler_time_ms: number | null;
275
+ flush_time_ms: number | null;
276
+ max_block_time_ms: number | null;
277
+ max_handler_time_ms: number | null;
278
+ avg_ops_per_block: number | null;
279
+ is_catchup: Generated<boolean>;
280
+ created_at: Generated<Date>;
281
+ }
282
+ interface SubgraphTableSnapshotsTable {
283
+ id: Generated<string>;
284
+ subgraph_name: string;
285
+ api_key_id: string | null;
286
+ table_name: string;
287
+ row_count: number | null;
288
+ created_at: Generated<Date>;
289
+ }
290
+ interface SubgraphHealthSnapshotsTable {
291
+ id: Generated<string>;
292
+ subgraph_id: string;
293
+ total_processed: number;
294
+ total_errors: number;
295
+ last_processed_block: number | null;
296
+ captured_at: Generated<Date>;
297
+ }
298
+ interface ProcessedStripeEventsTable {
299
+ event_id: string;
300
+ event_type: string;
301
+ processed_at: Generated<Date>;
302
+ }
303
+ interface DecodedEventsTable {
304
+ cursor: string;
305
+ block_height: number;
306
+ tx_id: string;
307
+ tx_index: number;
308
+ event_index: number;
309
+ event_type: string;
310
+ microblock_hash: string | null;
311
+ canonical: Generated<boolean>;
312
+ contract_id: string | null;
313
+ sender: string | null;
314
+ recipient: string | null;
315
+ amount: string | null;
316
+ asset_identifier: string | null;
317
+ value: string | null;
318
+ memo: string | null;
319
+ /** Decoded payload for event types that don't fit the flat columns
320
+ * (e.g. `print`: { topic, value, raw_value }). Null for transfer types. */
321
+ payload: unknown | null;
322
+ source_cursor: string;
323
+ created_at: Generated<Date>;
324
+ }
325
+ interface DecoderCheckpointsTable {
326
+ decoder_name: string;
327
+ last_cursor: string | null;
328
+ updated_at: Generated<Date>;
329
+ }
330
+ interface ChainReorgsTable {
331
+ id: Generated<string>;
332
+ detected_at: Generated<Date>;
333
+ fork_point_height: number;
334
+ old_index_block_hash: string | null;
335
+ new_index_block_hash: string | null;
336
+ orphaned_from_height: number;
337
+ orphaned_from_event_index: number;
338
+ orphaned_to_height: number;
339
+ orphaned_to_event_index: number;
340
+ new_canonical_height: number;
341
+ new_canonical_event_index: number;
342
+ created_at: Generated<Date>;
343
+ }
344
+ type Pox4FunctionName = "stack-stx" | "delegate-stx" | "stack-extend" | "stack-increase" | "revoke-delegate-stx" | "delegate-stack-stx" | "delegate-stack-extend" | "delegate-stack-increase" | "stack-aggregation-commit" | "stack-aggregation-commit-indexed" | "stack-aggregation-increase" | "set-signer-key-authorization";
345
+ interface Pox4CallsTable {
346
+ cursor: string;
347
+ block_height: number;
348
+ block_time: Date;
349
+ burn_block_height: number;
350
+ tx_id: string;
351
+ tx_index: number;
352
+ function_name: Pox4FunctionName;
353
+ caller: string;
354
+ stacker: string | null;
355
+ delegate_to: string | null;
356
+ amount_ustx: string | null;
357
+ lock_period: number | null;
358
+ pox_addr_version: number | null;
359
+ pox_addr_hashbytes: string | null;
360
+ pox_addr_btc: string | null;
361
+ start_cycle: number | null;
362
+ end_cycle: number | null;
363
+ signer_key: string | null;
364
+ signer_signature: string | null;
365
+ auth_id: string | null;
366
+ max_amount: string | null;
367
+ reward_cycle: number | null;
368
+ aggregated_amount_ustx: string | null;
369
+ aggregated_signer_index: number | null;
370
+ auth_period: number | null;
371
+ auth_topic: string | null;
372
+ auth_allowed: boolean | null;
373
+ result_ok: boolean;
374
+ result_raw: string;
375
+ canonical: Generated<boolean>;
376
+ source_cursor: string;
377
+ created_at: Generated<Date>;
378
+ }
379
+ interface Pox4CyclesDailyTable {
380
+ date: string;
381
+ reward_cycle: number;
382
+ total_stacked_ustx: Generated<string>;
383
+ solo_stackers: Generated<number>;
384
+ delegated_principals: Generated<number>;
385
+ unique_pools: Generated<number>;
386
+ unique_signers: Generated<number>;
387
+ calls_today: Generated<number>;
388
+ updated_at: Generated<Date>;
389
+ }
390
+ interface Pox4SignersDailyTable {
391
+ date: string;
392
+ reward_cycle: number;
393
+ signer_key: string;
394
+ weight_ustx: Generated<string>;
395
+ stacker_count: Generated<number>;
396
+ aggregation_calls: Generated<number>;
397
+ updated_at: Generated<Date>;
398
+ }
399
+ type Pox5EventTopic = "set-bond-admin" | "set-pause-admin" | "pause-rewards" | "setup-bond" | "add-to-allowlist" | "register-for-bond" | "update-bond-registration" | "register-signer" | "stake" | "stake-update" | "announce-l1-early-exit" | "unstake-sbtc" | "unstake" | "calculate-rewards" | "bond-distribution" | "claim-rewards" | "claim-staker-rewards-for-signer" | "grant-signer-key" | "revoke-signer-grant";
400
+ interface Pox5EventsTable {
401
+ cursor: string;
402
+ block_height: number;
403
+ block_time: Date;
404
+ tx_id: string;
405
+ tx_index: number;
406
+ event_index: number;
407
+ topic: Pox5EventTopic;
408
+ staker: string | null;
409
+ signer: string | null;
410
+ signer_manager: string | null;
411
+ bond_index: number | null;
412
+ amount_ustx: string | null;
413
+ amount_sats: string | null;
414
+ reward_cycle: number | null;
415
+ first_reward_cycle: number | null;
416
+ unlock_cycle: number | null;
417
+ unlock_burn_height: number | null;
418
+ is_l1_lock: boolean | null;
419
+ signer_key: string | null;
420
+ data: unknown;
421
+ canonical: Generated<boolean>;
422
+ source_cursor: string;
423
+ created_at: Generated<Date>;
424
+ }
425
+ interface BurnBlockRewardsTable {
426
+ cursor: string;
427
+ burn_block_height: number;
428
+ burn_block_hash: string;
429
+ reward_index: number;
430
+ recipient_btc: string;
431
+ amount_sats: string;
432
+ burn_amount: Generated<string>;
433
+ created_at: Generated<Date>;
434
+ }
435
+ interface BurnBlockRewardSlotsTable {
436
+ cursor: string;
437
+ burn_block_height: number;
438
+ burn_block_hash: string;
439
+ slot_index: number;
440
+ holder_btc: string;
441
+ created_at: Generated<Date>;
442
+ }
443
+ type SbtcEventTopic = "completed-deposit" | "withdrawal-create" | "withdrawal-accept" | "withdrawal-reject" | "key-rotation" | "update-protocol-contract";
444
+ interface SbtcEventsTable {
445
+ cursor: string;
446
+ block_height: number;
447
+ block_time: Date;
448
+ tx_id: string;
449
+ tx_index: number;
450
+ event_index: number;
451
+ topic: SbtcEventTopic;
452
+ request_id: number | null;
453
+ amount: string | null;
454
+ sender: string | null;
455
+ recipient_btc_version: number | null;
456
+ recipient_btc_hashbytes: string | null;
457
+ bitcoin_txid: string | null;
458
+ output_index: number | null;
459
+ sweep_txid: string | null;
460
+ burn_hash: string | null;
461
+ burn_height: number | null;
462
+ signer_bitmap: string | null;
463
+ max_fee: string | null;
464
+ fee: string | null;
465
+ block_height_at_request: number | null;
466
+ governance_contract_type: number | null;
467
+ governance_new_contract: string | null;
468
+ signer_aggregate_pubkey: string | null;
469
+ signer_threshold: number | null;
470
+ signer_address: string | null;
471
+ signer_keys_count: number | null;
472
+ canonical: Generated<boolean>;
473
+ source_cursor: string;
474
+ created_at: Generated<Date>;
475
+ }
476
+ type SbtcTokenEventType = "transfer" | "mint" | "burn";
477
+ interface SbtcTokenEventsTable {
478
+ cursor: string;
479
+ block_height: number;
480
+ block_time: Date;
481
+ tx_id: string;
482
+ tx_index: number;
483
+ event_index: number;
484
+ event_type: SbtcTokenEventType;
485
+ sender: string | null;
486
+ recipient: string | null;
487
+ amount: string;
488
+ memo: string | null;
489
+ canonical: Generated<boolean>;
490
+ source_cursor: string;
491
+ created_at: Generated<Date>;
492
+ }
493
+ /**
494
+ * BTC L1 settlement status for sBTC withdrawal sweeps. Filled by the indexer's
495
+ * settlement-confirmer worker, which polls our own bitcoind for confirmations of
496
+ * each `withdrawal-accept`'s `sweep_txid` and surfaces real settlement on the
497
+ * withdrawal lifecycle (the API's `settlement.{btc_confirmations,
498
+ * settlement_confirmed}` placeholder). Keyed on `sweep_txid` (not `request_id`):
499
+ * a request can produce more than one sweep over its life (RBF/rebroadcast), so
500
+ * each broadcast keeps its own confirmation history.
501
+ */
502
+ interface SbtcSettlementsTable {
503
+ sweep_txid: string;
504
+ request_id: number;
505
+ btc_confirmations: Generated<number>;
506
+ settlement_confirmed: Generated<boolean>;
507
+ /** Confirming Bitcoin block — null while the sweep is unconfirmed. */
508
+ block_hash: string | null;
509
+ block_height: number | null;
510
+ first_seen_at: Generated<Date>;
511
+ last_checked_at: Generated<Date>;
512
+ /** Set once, when `settlement_confirmed` first flips true. */
513
+ confirmed_at: Date | null;
514
+ updated_at: Generated<Date>;
515
+ }
516
+ interface SbtcSupplySnapshotsTable {
517
+ date: string;
518
+ total_supply: Generated<string>;
519
+ mints_today: Generated<string>;
520
+ burns_today: Generated<string>;
521
+ deposit_count: Generated<number>;
522
+ withdrawal_create_count: Generated<number>;
523
+ withdrawal_accept_count: Generated<number>;
524
+ withdrawal_reject_count: Generated<number>;
525
+ updated_at: Generated<Date>;
526
+ }
527
+ type BnsNameEventTopic = "new-name" | "transfer-name" | "renew-name" | "burn-name" | "new-airdrop";
528
+ interface BnsNameEventsTable {
529
+ cursor: string;
530
+ block_height: number;
531
+ block_time: Date;
532
+ tx_id: string;
533
+ tx_index: number;
534
+ event_index: number;
535
+ topic: BnsNameEventTopic;
536
+ namespace: string;
537
+ name: string;
538
+ fqn: string;
539
+ owner: string | null;
540
+ bns_id: string;
541
+ registered_at: number | null;
542
+ imported_at: number | null;
543
+ renewal_height: number | null;
544
+ stx_burn: string | null;
545
+ preordered_by: string | null;
546
+ hashed_salted_fqn_preorder: string | null;
547
+ canonical: Generated<boolean>;
548
+ source_cursor: string;
549
+ created_at: Generated<Date>;
550
+ }
551
+ type BnsNamespaceEventStatus = "launch" | "transfer-manager" | "freeze-manager" | "update-price-manager" | "freeze-price-manager" | "turn-off-manager-transfers";
552
+ interface BnsNamespaceEventsTable {
553
+ cursor: string;
554
+ block_height: number;
555
+ block_time: Date;
556
+ tx_id: string;
557
+ tx_index: number;
558
+ event_index: number;
559
+ status: BnsNamespaceEventStatus;
560
+ namespace: string;
561
+ manager: string | null;
562
+ manager_frozen: boolean | null;
563
+ manager_transfers_disabled: boolean | null;
564
+ price_function: string | null;
565
+ price_frozen: boolean | null;
566
+ lifetime: number | null;
567
+ revealed_at: number | null;
568
+ launched_at: number | null;
569
+ canonical: Generated<boolean>;
570
+ source_cursor: string;
571
+ created_at: Generated<Date>;
572
+ }
573
+ type BnsMarketplaceAction = "list-in-ustx" | "unlist-in-ustx" | "buy-in-ustx";
574
+ interface BnsMarketplaceEventsTable {
575
+ cursor: string;
576
+ block_height: number;
577
+ block_time: Date;
578
+ tx_id: string;
579
+ tx_index: number;
580
+ event_index: number;
581
+ action: BnsMarketplaceAction;
582
+ bns_id: string;
583
+ price_ustx: string | null;
584
+ commission: string | null;
585
+ canonical: Generated<boolean>;
586
+ source_cursor: string;
587
+ created_at: Generated<Date>;
588
+ }
589
+ interface BnsNamesTable {
590
+ fqn: string;
591
+ namespace: string;
592
+ name: string;
593
+ owner: string;
594
+ bns_id: string;
595
+ registered_at: number | null;
596
+ renewal_height: number | null;
597
+ last_event_cursor: string;
598
+ last_event_at: Date;
599
+ updated_at: Generated<Date>;
600
+ }
601
+ interface BnsNamespacesTable {
602
+ namespace: string;
603
+ manager: string | null;
604
+ manager_frozen: Generated<boolean>;
605
+ price_frozen: Generated<boolean>;
606
+ lifetime: number | null;
607
+ launched_at: number | null;
608
+ last_event_cursor: string;
609
+ last_event_at: Date;
610
+ name_count: Generated<number>;
611
+ updated_at: Generated<Date>;
612
+ }
613
+ /**
614
+ * Memoized `ctx.client.readOnly()` results. Keyed on `index_block_hash` so a
615
+ * reorg-replaced block never inherits the orphaned fork's answer; a NULL
616
+ * `index_block_hash` marks a caller-declared contract-constant value, cached
617
+ * once per contract+args instead of once per block.
618
+ */
619
+ interface ChainReadCacheTable {
620
+ id: Generated<number>;
621
+ contract_id: string;
622
+ function_name: string;
623
+ args_hash: string;
624
+ index_block_hash: string | null;
625
+ block_height: ColumnType<number | null, number | null | undefined, number | null>;
626
+ result_hex: string;
627
+ created_at: Generated<Date>;
628
+ }
629
+ /**
630
+ * A block that arrived for a height we already hold, staged until a later
631
+ * block's `parent_hash` says which of the two the chain kept. See migration
632
+ * 0112 — adopting on first sight picked the losing fork twice.
633
+ */
634
+ interface PendingForkBlocksTable {
635
+ height: number;
636
+ block_hash: string;
637
+ parent_hash: string;
638
+ /** The canonical hash we held at this height when the contender arrived. */
639
+ incumbent_hash: string;
640
+ /** The raw observer payload, replayed verbatim if this fork wins. */
641
+ payload: ColumnType<unknown, unknown, unknown>;
642
+ received_at: Generated<Date>;
643
+ }
644
+ type ObserverJournalStatus = "received" | "processed" | "failed";
645
+ /**
646
+ * Exact observer callback receipt. `raw_body` is immutable evidence; the
647
+ * processing columns are updated only after the current derived-state write
648
+ * succeeds or fails.
649
+ */
650
+ interface ObserverJournalTable {
651
+ sequence: Generated<string>;
652
+ network: string;
653
+ path: string;
654
+ source: string | null;
655
+ received_at: Generated<Date>;
656
+ raw_body: ColumnType<Buffer, Buffer, Buffer>;
657
+ raw_body_sha256: string;
658
+ status: ColumnType<ObserverJournalStatus, ObserverJournalStatus | undefined, ObserverJournalStatus>;
659
+ semantic_sha256: string | null;
660
+ block_height: number | null;
661
+ block_hash: string | null;
662
+ burn_block_height: number | null;
663
+ burn_block_hash: string | null;
664
+ result: unknown | null;
665
+ error: string | null;
666
+ processed_at: Date | null;
667
+ }
668
+ /** Declared coverage stage. Identity is the `id` string (`raw`, `decode:stx`, …). */
669
+ interface StageRegistryTable {
670
+ id: string;
671
+ kind: StageKind;
672
+ depends_on: string | null;
673
+ native_clock: NativeClock;
674
+ producer_version: string;
675
+ repair_mode: RepairMode;
676
+ enabled: Generated<boolean>;
677
+ created_at: Generated<Date>;
678
+ }
679
+ /** One versioned sweep of a stage. */
680
+ interface StageRunsTable {
681
+ id: Generated<string>;
682
+ stage_id: string;
683
+ code_hash: string;
684
+ config_hash: string;
685
+ handler_hash: string | null;
686
+ target_height: number | null;
687
+ target_cursor: string | null;
688
+ status: RunStatus;
689
+ complete_through: number | null;
690
+ started_at: Generated<Date>;
691
+ finished_at: Date | null;
692
+ }
693
+ /** Per-block acknowledgement. Unique on (stage, height, hash). */
694
+ interface StageBlockReceiptsTable {
695
+ id: Generated<string>;
696
+ stage_id: string;
697
+ run_id: string | null;
698
+ block_height: number;
699
+ block_hash: string;
700
+ input_count: number;
701
+ input_digest: string;
702
+ effect_digest: string;
703
+ finalized: Generated<boolean>;
704
+ compacted_at: Date | null;
705
+ created_at: Generated<Date>;
706
+ }
707
+ /** Compacted finalized range. Only sealed after receipts in the range finalize. */
708
+ interface CoverageSegmentsTable {
709
+ id: Generated<string>;
710
+ stage_id: string;
711
+ from_height: number;
712
+ to_height: number;
713
+ chain_digest: string;
714
+ input_digest: string;
715
+ output_digest: string;
716
+ sealed_at: Generated<Date>;
717
+ }
718
+ /** Exact unit or range that failed, plus retry/retention state. */
719
+ interface StageFailuresTable {
720
+ id: Generated<string>;
721
+ stage_id: string;
722
+ run_id: string | null;
723
+ unit_kind: FailureUnit;
724
+ from_height: number | null;
725
+ to_height: number | null;
726
+ class: FailureClass;
727
+ retry_state: RetryState;
728
+ retry_count: Generated<number>;
729
+ last_error: string | null;
730
+ created_at: Generated<Date>;
731
+ resolved_at: Date | null;
732
+ retain_until: Generated<Date>;
733
+ }
734
+ /**
735
+ * The instance's declared scope — one row per network. `start_height` is what
736
+ * makes missing history below it deliberate rather than a gap.
737
+ */
738
+ interface SyncScopesTable {
739
+ network: string;
740
+ start_height: number;
741
+ target_height: number | null;
742
+ bootstrap_source: BootstrapSource;
743
+ bootstrap_manifest_digest: string | null;
744
+ genesis_hash: string | null;
745
+ created_at: Generated<Date>;
746
+ updated_at: Generated<Date>;
747
+ }
748
+ interface Database {
749
+ blocks: BlocksTable;
750
+ transactions: TransactionsTable;
751
+ events: EventsTable;
752
+ transactions_archive: TransactionsArchiveTable;
753
+ events_archive: EventsArchiveTable;
754
+ dead_letter_events: DeadLetterEventsTable;
755
+ mempool_transactions: MempoolTransactionsTable;
756
+ index_progress: IndexProgressTable;
757
+ contracts: ContractsTable;
758
+ subgraphs: SubgraphsTable;
759
+ api_keys: ApiKeysTable;
760
+ instances: InstancesTable;
761
+ accounts: AccountsTable;
762
+ sessions: SessionsTable;
763
+ magic_links: MagicLinksTable;
764
+ subgraph_health_snapshots: SubgraphHealthSnapshotsTable;
765
+ subgraph_processing_stats: SubgraphProcessingStatsTable;
766
+ subgraph_table_snapshots: SubgraphTableSnapshotsTable;
767
+ subgraph_gaps: SubgraphGapsTable;
768
+ subgraph_operations: SubgraphOperationsTable;
769
+ processed_stripe_events: ProcessedStripeEventsTable;
770
+ account_spend_caps: AccountSpendCapsTable;
771
+ account_credits: AccountCreditsTable;
772
+ subscriptions: SubscriptionsTable;
773
+ subscription_outbox: SubscriptionOutboxTable;
774
+ subscription_deliveries: SubscriptionDeliveriesTable;
775
+ trigger_evaluator_state: TriggerEvaluatorStateTable;
776
+ decoded_events: DecodedEventsTable;
777
+ decoder_checkpoints: DecoderCheckpointsTable;
778
+ chain_reorgs: ChainReorgsTable;
779
+ pox4_calls: Pox4CallsTable;
780
+ pox4_cycles_daily: Pox4CyclesDailyTable;
781
+ pox4_signers_daily: Pox4SignersDailyTable;
782
+ pox5_events: Pox5EventsTable;
783
+ burn_block_rewards: BurnBlockRewardsTable;
784
+ burn_block_reward_slots: BurnBlockRewardSlotsTable;
785
+ sbtc_events: SbtcEventsTable;
786
+ sbtc_token_events: SbtcTokenEventsTable;
787
+ sbtc_settlements: SbtcSettlementsTable;
788
+ sbtc_supply_snapshots: SbtcSupplySnapshotsTable;
789
+ bns_name_events: BnsNameEventsTable;
790
+ bns_namespace_events: BnsNamespaceEventsTable;
791
+ bns_marketplace_events: BnsMarketplaceEventsTable;
792
+ bns_names: BnsNamesTable;
793
+ bns_namespaces: BnsNamespacesTable;
794
+ service_heartbeats: ServiceHeartbeatsTable;
795
+ x402_payments: X402PaymentsTable;
796
+ x402_balances: X402BalancesTable;
797
+ chain_read_cache: ChainReadCacheTable;
798
+ pending_fork_blocks: PendingForkBlocksTable;
799
+ observer_journal: ObserverJournalTable;
800
+ sync_scopes: SyncScopesTable;
801
+ stage_registry: StageRegistryTable;
802
+ stage_runs: StageRunsTable;
803
+ stage_block_receipts: StageBlockReceiptsTable;
804
+ coverage_segments: CoverageSegmentsTable;
805
+ stage_failures: StageFailuresTable;
806
+ }
807
+ /** Prepaid x402 credit — one running USD-micros balance per payer principal. */
808
+ interface X402BalancesTable {
809
+ principal: string;
810
+ balance_usd_micros: Generated<string | number | bigint>;
811
+ /** Month bucket ("YYYY-MM") the spend counter applies to. */
812
+ spent_month: string | null;
813
+ spent_month_usd_micros: Generated<string | number | bigint>;
814
+ updated_at: Generated<Date>;
815
+ }
816
+ interface ServiceHeartbeatsTable {
817
+ name: string;
818
+ updated_at: Generated<Date>;
819
+ }
820
+ /** x402 pay-per-request ledger (control plane). One row per settled payment,
821
+ * keyed by challenge nonce + settled txid. `state` tracks confirmed-tier
822
+ * settlement and post-serve reorg reversal. */
823
+ interface X402PaymentsTable {
824
+ id: Generated<string>;
825
+ nonce: string;
826
+ txid: string;
827
+ asset: string;
828
+ amount: string;
829
+ payer: string;
830
+ surface: string;
831
+ state: Generated<"pending" | "confirmed" | "reverted">;
832
+ created_at: Generated<Date>;
833
+ updated_at: Generated<Date>;
834
+ /** "payment" = per-call settle; "deposit" = prepaid balance top-up. */
835
+ kind: Generated<string>;
836
+ /** Linked claimed account once the paying wallet is attached (continuity). */
837
+ account_id: string | null;
838
+ /** USD-micros to credit on confirmation, for deposit rows the reconciler
839
+ * settles asynchronously. NULL for per-call settles (credit nothing). */
840
+ credit_usd_micros: string | null;
841
+ /** Idempotency key: stamped exactly once when this row's credit is applied.
842
+ * NULL means never credited (or not a credit-bearing row). Pre-migration
843
+ * non-pending deposit rows are backfilled to a non-null sentinel so the
844
+ * reconciler's heal path can never double-credit them. */
845
+ credited_at: Generated<Date> | null;
846
+ }
847
+ interface AccountSpendCapsTable {
848
+ account_id: string;
849
+ monthly_cap_cents: number | null;
850
+ compute_cap_cents: number | null;
851
+ storage_cap_cents: number | null;
852
+ alert_threshold_pct: Generated<number>;
853
+ alert_sent_at: Date | null;
854
+ frozen_at: Date | null;
855
+ updated_at: Generated<Date>;
856
+ }
857
+ /** Prepaid dev credits — card-funded balance per account (peer to x402_balances). */
858
+ interface AccountCreditsTable {
859
+ account_id: string;
860
+ balance_usd_micros: Generated<string | number | bigint>;
861
+ /** Month bucket ("YYYY-MM") the spend counter applies to. */
862
+ spent_month: string | null;
863
+ spent_month_usd_micros: Generated<string | number | bigint>;
864
+ /** Null = auto-refill off. */
865
+ refill_below_usd_micros: string | number | bigint | null;
866
+ refill_pack_usd: number | null;
867
+ refill_last_at: Date | null;
868
+ updated_at: Generated<Date>;
869
+ }
870
+ type SubscriptionStatus = "active" | "paused" | "error";
871
+ /** Polymorphic subscription mode: `subgraph` reacts to processed table rows;
872
+ * `chain` reacts to raw chain events matched directly off the Index/Streams
873
+ * clock (no subgraph). See migration 0088. */
874
+ type SubscriptionKind = "subgraph" | "chain";
875
+ type SubscriptionFormat = "standard-webhooks" | "inngest" | "trigger" | "cloudflare" | "cloudevents" | "raw";
876
+ type SubscriptionRuntime = "inngest" | "trigger" | "cloudflare" | "node";
877
+ interface SubscriptionsTable {
878
+ id: Generated<string>;
879
+ account_id: string;
880
+ project_id: string | null;
881
+ name: string;
882
+ status: ColumnType<SubscriptionStatus, SubscriptionStatus | undefined, SubscriptionStatus>;
883
+ kind: ColumnType<SubscriptionKind, SubscriptionKind | undefined, SubscriptionKind>;
884
+ /** Null for chain subscriptions (CHECK subscriptions_kind_shape). */
885
+ subgraph_name: string | null;
886
+ /** Null for chain subscriptions (CHECK subscriptions_kind_shape). */
887
+ table_name: string | null;
888
+ /** Chain-trigger filter array (the `SubgraphFilter` shape, JSON). Null for
889
+ * subgraph subscriptions. Typed loosely here to avoid a shared→subgraphs
890
+ * import cycle; the Zod schema in schemas/subscriptions.ts owns the shape. */
891
+ triggers: unknown | null;
892
+ filter: Generated<unknown>;
893
+ format: ColumnType<SubscriptionFormat, SubscriptionFormat | undefined, SubscriptionFormat>;
894
+ runtime: SubscriptionRuntime | null;
895
+ url: string;
896
+ signing_secret_enc: Buffer;
897
+ auth_config: Generated<unknown>;
898
+ max_retries: Generated<number>;
899
+ timeout_ms: Generated<number>;
900
+ concurrency: Generated<number>;
901
+ circuit_failures: Generated<number>;
902
+ circuit_opened_at: Date | null;
903
+ last_delivery_at: Date | null;
904
+ last_success_at: Date | null;
905
+ last_error: string | null;
906
+ created_at: Generated<Date>;
907
+ updated_at: Generated<Date>;
908
+ }
909
+ type OutboxStatus = "pending" | "delivered" | "dead";
910
+ interface SubscriptionOutboxTable {
911
+ id: Generated<string>;
912
+ subscription_id: string;
913
+ kind: ColumnType<SubscriptionKind, SubscriptionKind | undefined, SubscriptionKind>;
914
+ /** Null for chain-subscription rows. */
915
+ subgraph_name: string | null;
916
+ /** Null for chain-subscription rows. */
917
+ table_name: string | null;
918
+ block_height: number | bigint;
919
+ tx_id: string | null;
920
+ row_pk: unknown;
921
+ event_type: string;
922
+ payload: unknown;
923
+ dedup_key: string;
924
+ attempt: Generated<number>;
925
+ next_attempt_at: Generated<Date>;
926
+ status: ColumnType<OutboxStatus, OutboxStatus | undefined, OutboxStatus>;
927
+ is_replay: Generated<boolean>;
928
+ delivered_at: Date | null;
929
+ failed_at: Date | null;
930
+ locked_by: string | null;
931
+ locked_until: Date | null;
932
+ created_at: Generated<Date>;
933
+ }
934
+ interface SubscriptionDeliveriesTable {
935
+ id: Generated<string>;
936
+ /** Nullable after migration 0077 — outbox row may be cleaned up while
937
+ * delivery telemetry is retained. */
938
+ outbox_id: string | null;
939
+ subscription_id: string;
940
+ attempt: number;
941
+ status_code: number | null;
942
+ response_headers: unknown | null;
943
+ response_body: string | null;
944
+ error_message: string | null;
945
+ duration_ms: number | null;
946
+ dispatched_at: Generated<Date>;
947
+ }
948
+ /** Single-row (id always TRUE) high-water mark for the chain-trigger evaluator.
949
+ * One loop serves all chain subscriptions, so the cursor is global. */
950
+ interface TriggerEvaluatorStateTable {
951
+ id: Generated<boolean>;
952
+ last_processed_block: ColumnType<bigint, bigint | number | undefined, bigint | number>;
953
+ /** Watermark for the async sBTC settlement-confirmed webhook scan; null until
954
+ * the evaluator fast-forwards it on first run. */
955
+ last_settlement_scan_at: ColumnType<Date | null, Date | null | undefined, Date | null>;
956
+ updated_at: Generated<Date>;
957
+ }
958
+ /**
959
+ * The instance's declared sync scope.
960
+ *
961
+ * An instance that starts at height N is not an instance missing the first N
962
+ * blocks — but nothing in the schema said so, so every consumer had to guess
963
+ * from `MIN(height)`. `sync_scopes` records the declaration; this module is the
964
+ * only place that reads it, and it always answers in the `SyncScope` shape the
965
+ * coverage evaluator consumes.
966
+ *
967
+ * `resolveSyncScope` never fails on an instance that predates the table: with
968
+ * no row it derives a scope from what the database already shows, so an
969
+ * existing install keeps working without a backfill dance.
970
+ */
971
+ /**
972
+ * Declares a forward-only instance: history below this height is deliberately
973
+ * absent, not lost. Read only when no `sync_scopes` row exists — a recorded
974
+ * scope always wins, because it is the one an operator committed to.
975
+ */
976
+ declare const SYNC_START_HEIGHT_ENV = "SECONDLAYER_SYNC_START_HEIGHT";
977
+ declare class InvalidSyncStartHeightError extends Error {
978
+ readonly value: string;
979
+ readonly name = "InvalidSyncStartHeightError";
980
+ constructor(value: string);
981
+ }
982
+ /**
983
+ * Throws rather than ignoring a malformed value: a typo'd start height that
984
+ * silently falls back to genesis would report a real forward-only instance as
985
+ * an 8M-block gap, which is the exact confusion this table exists to end.
986
+ */
987
+ declare function syncStartHeightFromEnv(env?: NodeJS.ProcessEnv): number | null;
988
+ declare function getSyncScope(db: Kysely<Database>, network: string): Promise<SyncScope | null>;
989
+ /**
990
+ * Record (or restate) the scope for a network. Bootstrap and re-bootstrap are
991
+ * the writers; the upsert is idempotent so re-running a restore over the same
992
+ * archive does not fork the declaration.
993
+ */
994
+ declare function upsertSyncScope(db: Kysely<Database>, scope: SyncScope): Promise<SyncScope>;
995
+ /**
996
+ * The persisted scope, or one derived from the database when none was ever
997
+ * recorded.
998
+ *
999
+ * The derivation is deliberately conservative: it claims only what the data
1000
+ * already shows (the lowest canonical block, the genesis hash if genesis is
1001
+ * present) and never invents provenance — an instance whose history starts
1002
+ * above genesis with no recorded bootstrap is an `import`, not an archive
1003
+ * restore we can vouch for.
1004
+ */
1005
+ declare function resolveSyncScope(db: Kysely<Database>, network: string): Promise<SyncScope>;
1006
+ export { upsertSyncScope, syncStartHeightFromEnv, resolveSyncScope, getSyncScope, SYNC_START_HEIGHT_ENV, InvalidSyncStartHeightError };