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