@secondlayer/subgraphs 7.7.4 → 8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/subgraphs",
3
- "version": "7.7.4",
3
+ "version": "8.0.0",
4
4
  "description": "Describe the Stacks data you want as one TypeScript file; it fills itself and survives reorgs.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,10 +17,6 @@
17
17
  "types": "./dist/index.d.ts",
18
18
  "import": "./dist/index.js"
19
19
  },
20
- "./types": {
21
- "types": "./dist/types.d.ts",
22
- "import": "./dist/types.js"
23
- },
24
20
  "./schema": {
25
21
  "types": "./dist/schema/index.d.ts",
26
22
  "import": "./dist/schema/index.js"
package/dist/types.d.ts DELETED
@@ -1,424 +0,0 @@
1
- import { AbiContract as AbiContract2 } from "@secondlayer/stacks/clarity";
2
- import { AssetIdentifier } from "@secondlayer/stacks/filters";
3
- /**
4
- * How long a read stays valid.
5
- *
6
- * - `per-block` (default): pinned to the block being processed, cached under
7
- * its `index_block_hash`. Sound for any read-only function — reindexing the
8
- * same block always returns the same value, and a reorg-replaced block has a
9
- * different id so it can't inherit the orphaned answer.
10
- * - `contract-constant`: the caller asserts the value cannot change for this
11
- * contract and these args (SIP-010 `get-decimals`/`get-symbol`), so it is
12
- * resolved ONCE instead of once per block. This is the mode that makes a
13
- * backfill affordable — under `per-block` every block is a distinct key and
14
- * therefore a distinct RPC. Declaring it on a value that does change pins the
15
- * first answer forever, so it is opt-in and never inferred.
16
- */
17
- type ChainReadCacheMode = "per-block" | "contract-constant";
18
- /** Options for a contract read, such as cache mode. */
19
- type ChainReadOptions = {
20
- cache?: ChainReadCacheMode;
21
- };
22
- /**
23
- * `ChainReadClient` with the ABI generic erased. Structurally cheap, so the
24
- * base `SubgraphContext` can carry it; `TypedSubgraphContext` uses the real
25
- * one and handlers get full inference.
26
- */
27
- interface ErasedChainReadClient {
28
- /**
29
- * Reads a contract's read-only functions at the block being processed.
30
- *
31
- * Results are untyped here. Call it through `readContractAt` to get typed
32
- * results from the contract's ABI.
33
- */
34
- contract(contractId: string, abi: unknown, options?: ChainReadOptions): {
35
- read: Record<string, (args?: Record<string, unknown>) => Promise<unknown>>;
36
- };
37
- }
38
- /** Supported column types for subgraph schemas */
39
- type ColumnType = "text" | "uint" | "int" | "principal" | "boolean" | "timestamp" | "jsonb";
40
- /** Column definition in a subgraph table */
41
- interface SubgraphColumn {
42
- type: ColumnType;
43
- nullable?: boolean;
44
- indexed?: boolean;
45
- search?: boolean;
46
- default?: string | number | boolean;
47
- }
48
- /**
49
- * Table definition within a subgraph schema.
50
- *
51
- * The array fields are `readonly` so a schema literal can be hoisted out of
52
- * the `defineSubgraph()` call with `as const` (or {@link defineSchema}) and
53
- * still typecheck — `as const` produces `readonly [readonly ["holder"]]`,
54
- * which a mutable `string[][]` rejects. Without that, extracting a helper
55
- * that takes `ctx` meant losing the entire typed surface, which is why the
56
- * shipped starter reached for `ctx: any`.
57
- */
58
- interface SubgraphTable {
59
- columns: Record<string, SubgraphColumn>;
60
- /** Composite indexes (each entry is an array of column names) */
61
- indexes?: readonly (readonly string[])[];
62
- /** Unique key constraints (each entry is an array of column names). Required for upsert. */
63
- uniqueKeys?: readonly (readonly string[])[];
64
- }
65
- /** Maps table names to table definitions. */
66
- type SubgraphSchema = Record<string, SubgraphTable>;
67
- /** STX event filters */
68
- interface StxTransferFilter {
69
- type: "stx_transfer";
70
- sender?: string;
71
- recipient?: string;
72
- minAmount?: bigint;
73
- maxAmount?: bigint;
74
- }
75
- interface StxMintFilter {
76
- type: "stx_mint";
77
- recipient?: string;
78
- minAmount?: bigint;
79
- }
80
- interface StxBurnFilter {
81
- type: "stx_burn";
82
- sender?: string;
83
- minAmount?: bigint;
84
- }
85
- interface StxLockFilter {
86
- type: "stx_lock";
87
- lockedAddress?: string;
88
- minAmount?: bigint;
89
- }
90
- /**
91
- * Restrict a source to contracts conforming to a trait/standard (e.g. "sip-010")
92
- * instead of a fixed contract — resolved from the contract registry at match time,
93
- * as-of each processed block. Lets a source index "all SIP-010 tokens" etc.
94
- */
95
- type TraitScope = {
96
- trait?: string;
97
- };
98
- /**
99
- * Scope a source to a DYNAMIC address set discovered from another source's
100
- * events, instead of a fixed contract list.
101
- *
102
- * The standard shape it unlocks: a router plus pools created after you
103
- * deploy. `contractId` arrays cover a set you know at authoring time; a
104
- * factory covers the set that grows. It is deliberately NOT tied to contract
105
- * deployment — the address can come from any field of any event, so it also
106
- * covers launchpad-minted tokens, DAO extension allowlists, and registry
107
- * entries.
108
- *
109
- * ```ts
110
- * sources: {
111
- * registry: {
112
- * type: "print_event",
113
- * contractId: REGISTRY,
114
- * topic: "pool-created",
115
- * prints: { "pool-created": { pool: "principal" } },
116
- * },
117
- * swaps: {
118
- * type: "print_event",
119
- * topic: "swap",
120
- * // every pool the registry has ever announced, including new ones
121
- * factory: { from: "registry", field: "data.pool" },
122
- * },
123
- * }
124
- * ```
125
- *
126
- * Two guarantees the runtime provides:
127
- * - **Same-block ordering** — a contract discovered in block N receives its
128
- * own block-N events. Discovery runs before matching, within the block.
129
- * - **Reorg-scoped** — the discovered set is stamped with the block that
130
- * revealed it and rolled back with everything else on a fork, so an address
131
- * announced on an orphaned chain does not linger forever.
132
- */
133
- interface FactoryScope {
134
- factory?: {
135
- /** Source name whose events reveal the addresses. */
136
- from: string;
137
- /** Dotted path to the address on that source's payload
138
- * (e.g. `"data.pool"`, `"contractId"`, `"data.token.id"`). */
139
- field: string;
140
- };
141
- }
142
- /** FT event filters */
143
- interface FtTransferFilter extends TraitScope {
144
- type: "ft_transfer";
145
- assetIdentifier?: AssetIdentifier;
146
- sender?: string;
147
- recipient?: string;
148
- minAmount?: bigint;
149
- }
150
- interface FtMintFilter extends TraitScope {
151
- type: "ft_mint";
152
- assetIdentifier?: AssetIdentifier;
153
- recipient?: string;
154
- minAmount?: bigint;
155
- }
156
- interface FtBurnFilter extends TraitScope {
157
- type: "ft_burn";
158
- assetIdentifier?: AssetIdentifier;
159
- sender?: string;
160
- minAmount?: bigint;
161
- }
162
- /** NFT event filters */
163
- interface NftTransferFilter extends TraitScope {
164
- type: "nft_transfer";
165
- assetIdentifier?: AssetIdentifier;
166
- sender?: string;
167
- recipient?: string;
168
- }
169
- interface NftMintFilter extends TraitScope {
170
- type: "nft_mint";
171
- assetIdentifier?: AssetIdentifier;
172
- recipient?: string;
173
- }
174
- interface NftBurnFilter extends TraitScope {
175
- type: "nft_burn";
176
- assetIdentifier?: AssetIdentifier;
177
- sender?: string;
178
- }
179
- /**
180
- * Event shape passed to contract_call handlers.
181
- *
182
- * `args` is a **positional array** of decoded Clarity values matching the
183
- * contract function's parameter list in declaration order. Use the ABI
184
- * (or Clarity contract source) to map positions to names:
185
- *
186
- * ```ts
187
- * // pox-4 stack-stx args: (amount-ustx uint) (pox-addr tuple) (start-burn-ht uint) (lock-period uint)
188
- * const [amountUstx, , , lockPeriod] = event.args;
189
- * ```
190
- *
191
- * Bigints come out as `bigint`. Buffers as `Uint8Array`. Principals as
192
- * strings (`"SP..."`). Tuples as `Record<string, unknown>`.
193
- *
194
- * Historical transactions indexed before the function_args column was added
195
- * will have `args = []` — always guard with `args.length > 0` when reading
196
- * args from pre-Nakamoto history.
197
- */
198
- interface ContractCallEvent {
199
- type: "contract_call";
200
- /** Transaction sender (the principal who signed the tx). Always non-null. */
201
- sender: string;
202
- contractId: string;
203
- functionName: string;
204
- /** Positional decoded Clarity values — order matches the ABI parameter list. */
205
- args: unknown[];
206
- /** Decoded return value from the contract function, or null. */
207
- result: unknown;
208
- /** Raw hex-encoded result value. */
209
- resultHex: string | null;
210
- /** Transaction metadata. */
211
- tx: {
212
- txId: string;
213
- sender: string;
214
- type: string;
215
- status: string;
216
- contractId: string | null;
217
- functionName: string | null;
218
- };
219
- }
220
- /** Contract event filters */
221
- interface ContractCallFilter extends TraitScope, FactoryScope {
222
- type: "contract_call";
223
- /**
224
- * One contract id, or a SET of them (max 20) — the standard DeFi shape is
225
- * a router plus N pools, which used to cost N near-identical sources and
226
- * handlers. Payload types are identical per `type`, so one handler serves
227
- * the set and `event.contractId` disambiguates.
228
- *
229
- * Supports `*` wildcards (e.g. `"SP1ABC….pool-*"`). Mutually exclusive
230
- * with `trait`, which resolves its own set per block.
231
- */
232
- contractId?: string | readonly string[];
233
- functionName?: string;
234
- caller?: string;
235
- /**
236
- * Contract ABI (pass it `as const`) used to type `event.input` — the named,
237
- * decoded function arguments. Dev-provided; serialized into the deployed
238
- * definition and used at runtime to decode args by name. Omit to keep
239
- * `event.args` as a positional `unknown[]`.
240
- *
241
- * Required whenever `functionName` is set. `secondlayer subgraphs deploy`
242
- * fills it in from the deployed contract for a single concrete
243
- * `contractId`; pass it yourself for wildcard, multi-contract or trait
244
- * sources, or a local deploy (`normalizeAbi()` from
245
- * `@secondlayer/stacks/clarity`).
246
- */
247
- abi?: AbiContract2;
248
- }
249
- interface ContractDeployFilter {
250
- type: "contract_deploy";
251
- deployer?: string;
252
- contractName?: string;
253
- }
254
- interface NestedContractCallFilter extends TraitScope, FactoryScope {
255
- type: "nested_contract_call";
256
- contractId?: string | readonly string[];
257
- functionName?: string;
258
- caller?: string;
259
- sender?: string;
260
- }
261
- interface VarSetFilter extends TraitScope, FactoryScope {
262
- type: "var_set";
263
- contractId?: string | readonly string[];
264
- varName?: string;
265
- /**
266
- * Contract ABI. With a `varName` the abi defines, `event.value` is typed
267
- * and decoded from the variable's type. Pass it yourself
268
- * (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
269
- */
270
- abi?: AbiContract2;
271
- }
272
- interface MapWriteFilter extends TraitScope, FactoryScope {
273
- type: "map_set" | "map_insert" | "map_delete";
274
- contractId?: string | readonly string[];
275
- map?: string;
276
- /**
277
- * Contract ABI. With a `map` the abi defines, `event.key` and
278
- * `event.value` are typed and decoded from the map's types. Pass it
279
- * yourself (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
280
- */
281
- abi?: AbiContract2;
282
- }
283
- /**
284
- * One declared field of a print payload.
285
- *
286
- * A bare {@link ColumnType} is a required scalar (`amount: "uint"`). The
287
- * composite forms exist because real print payloads are not flat: BNS-V2
288
- * emits `name` as a NESTED tuple, and a `prints` vocabulary that could only
289
- * say `"jsonb"` is why a handler reading flat `data.name` type-checked,
290
- * deployed, and then decoded to null on every event.
291
- *
292
- * - `{ type: F, optional: true }` — the KEY becomes optional on `event.data`
293
- * (mirrors `always_present: false` in the inferred schema).
294
- * - `{ tuple: { … } }` — a nested tuple, typed all the way down.
295
- * - `{ list: F }` — a list of `F`.
296
- */
297
- type PrintField = ColumnType | {
298
- type: PrintField;
299
- optional: true;
300
- } | {
301
- tuple: Record<string, PrintField>;
302
- } | {
303
- list: PrintField;
304
- };
305
- /**
306
- * Per-topic field schema for a print_event source. When declared, the handler's
307
- * `event` is a discriminated union keyed by `topic` and `event.data` is typed
308
- * per topic. Nested tuples, lists, and optional fields — see {@link PrintField}.
309
- *
310
- * Also opts the source into RUNTIME validation: a payload that does not match
311
- * is skipped and logged rather than written as nulls.
312
- */
313
- type PrintEventPrints = Record<string, Record<string, PrintField>>;
314
- type PrintEventFields = FactoryScope & {
315
- type: "print_event";
316
- topic?: string;
317
- /** Equality on top-level keys of the decoded print tuple, spelled as the
318
- * contract prints them (`signer-manager`). A value matches the field's
319
- * JSON form: principals `SP…`, uints as decimal strings, bools
320
- * `"true"`/`"false"`, buffers `0x…`. An array is any-of; keys AND.
321
- * Nested tuples and lists never match. */
322
- where?: Record<string, string | readonly string[]>;
323
- };
324
- /**
325
- * Pinned to one or more contract ids with no trait — `prints` is required so
326
- * `event.data` is typed and empty-table handlers cannot typecheck.
327
- */
328
- type PinnedPrintEventFilter = PrintEventFields & {
329
- contractId: string | readonly string[];
330
- trait?: undefined;
331
- prints: PrintEventPrints;
332
- };
333
- /**
334
- * Trait-scoped print source (optionally narrowed by contractId). No single
335
- * schema — `prints` stays optional.
336
- */
337
- type TraitPrintEventFilter = PrintEventFields & {
338
- contractId?: string | readonly string[];
339
- trait: string;
340
- prints?: PrintEventPrints;
341
- };
342
- /**
343
- * Unpinned / factory-only print source (no contractId). `prints` optional.
344
- */
345
- type UnpinnedPrintEventFilter = PrintEventFields & {
346
- contractId?: undefined;
347
- trait?: string;
348
- prints?: PrintEventPrints;
349
- };
350
- /**
351
- * Print-event filter. Pinned sources (contractId set, no trait) require
352
- * `prints`; trait and unpinned sources may omit it.
353
- */
354
- type PrintEventFilter = PinnedPrintEventFilter | TraitPrintEventFilter | UnpinnedPrintEventFilter;
355
- /** A subgraph filter, discriminated on `type`. */
356
- type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter | NestedContractCallFilter | VarSetFilter | MapWriteFilter;
357
- /** Transaction metadata available in handlers */
358
- interface TxMeta {
359
- txId: string;
360
- sender: string;
361
- type: string;
362
- status: string;
363
- contractId?: string | null;
364
- functionName?: string | null;
365
- }
366
- /** Scalar types that can appear as row values */
367
- type RowValue = string | number | bigint | boolean | null | undefined | Record<string, unknown> | unknown[];
368
- /** Context passed to subgraph handlers during event processing */
369
- interface SubgraphContext {
370
- block: {
371
- height: number;
372
- hash: string;
373
- timestamp: number;
374
- burnBlockHeight: number;
375
- /** Nakamoto StacksBlockId, when the block row carries one. */
376
- indexBlockHash?: string | null;
377
- };
378
- tx: TxMeta;
379
- /**
380
- * Read-only contract calls pinned to this block. Cached, so the same
381
- * (contract, function, args) is fetched once per block rather than once
382
- * per event.
383
- *
384
- * Erased here on purpose: this interface is compared structurally wherever
385
- * a subgraph definition is passed around, and carrying an ABI-generic
386
- * method pushes those comparisons past TS's instantiation depth. Handlers
387
- * receive `TypedSubgraphContext`, which exposes the fully typed version.
388
- */
389
- client: ErasedChainReadClient;
390
- insert(table: string, row: Record<string, unknown>): void;
391
- update(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
392
- upsert(table: string, key: Record<string, unknown>, row: Record<string, unknown>): void;
393
- delete(table: string, where: Record<string, unknown>): void;
394
- /**
395
- * Atomic counter update — the blessed accumulator primitive. Applies
396
- * `col = COALESCE(col, 0) + delta` per column (insert-or-add); deltas may
397
- * be negative. Requires a uniqueKeys constraint matching `key`. Prefer
398
- * this over read-modify-write handlers for running totals.
399
- */
400
- increment(table: string, key: Record<string, unknown>, deltas: Record<string, bigint | number>): void;
401
- findOne(table: string, where: Record<string, unknown>): Promise<Record<string, unknown> | null>;
402
- findMany(table: string, where: Record<string, unknown>): Promise<Record<string, unknown>[]>;
403
- }
404
- /** Handler function that processes events and writes to the subgraph */
405
- type SubgraphHandler = (event: Record<string, unknown>, ctx: SubgraphContext) => Promise<void> | void;
406
- /** Complete subgraph definition */
407
- interface SubgraphDefinition {
408
- /** Unique subgraph name (lowercase, alphanumeric + hyphens) */
409
- name: string;
410
- /** Human description */
411
- description?: string;
412
- /** Block height to start indexing from (default: 1) */
413
- startBlock?: number;
414
- /** 'concurrent' = tip-first: live at tip now, history backfills behind.
415
- * Requires order-tolerant handlers. Default 'blocking'. */
416
- backfillMode?: "blocking" | "concurrent";
417
- /** Named source filters — keys become handler keys */
418
- sources: Record<string, SubgraphFilter>;
419
- /** Tables in this subgraph */
420
- schema: SubgraphSchema;
421
- /** Handler functions, one per source, keyed by source name */
422
- handlers: Record<string, SubgraphHandler>;
423
- }
424
- export { ColumnType, ContractCallEvent, ContractCallFilter, ContractDeployFilter, FactoryScope, FtBurnFilter, FtMintFilter, FtTransferFilter, MapWriteFilter, NestedContractCallFilter, NftBurnFilter, NftMintFilter, NftTransferFilter, PinnedPrintEventFilter, PrintEventFilter, PrintEventPrints, PrintField, RowValue, StxBurnFilter, StxLockFilter, StxMintFilter, StxTransferFilter, SubgraphColumn, SubgraphContext, SubgraphDefinition, SubgraphFilter, SubgraphHandler, SubgraphSchema, SubgraphTable, TraitPrintEventFilter, TxMeta, UnpinnedPrintEventFilter, VarSetFilter };
package/dist/types.js DELETED
@@ -1,3 +0,0 @@
1
-
2
- //# debugId=9FCB033A2878230664756E2164756E21
3
- //# sourceMappingURL=types.js.map
package/dist/types.js.map DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "version": 3,
3
- "sources": [],
4
- "sourcesContent": [
5
- ],
6
- "mappings": "",
7
- "debugId": "9FCB033A2878230664756E2164756E21",
8
- "names": []
9
- }