@secondlayer/subgraphs 0.8.1 → 0.9.1

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 (42) hide show
  1. package/dist/src/index.d.ts +113 -32
  2. package/dist/src/index.js +486 -99
  3. package/dist/src/index.js.map +9 -10
  4. package/dist/src/runtime/block-processor.d.ts +110 -20
  5. package/dist/src/runtime/block-processor.js +452 -90
  6. package/dist/src/runtime/block-processor.js.map +7 -8
  7. package/dist/src/runtime/catchup.d.ts +110 -20
  8. package/dist/src/runtime/catchup.js +457 -94
  9. package/dist/src/runtime/catchup.js.map +8 -9
  10. package/dist/src/runtime/clarity.js +3 -3
  11. package/dist/src/runtime/clarity.js.map +3 -3
  12. package/dist/src/runtime/context.d.ts +23 -5
  13. package/dist/src/runtime/context.js +57 -1
  14. package/dist/src/runtime/context.js.map +3 -3
  15. package/dist/src/runtime/processor.js +457 -94
  16. package/dist/src/runtime/processor.js.map +8 -9
  17. package/dist/src/runtime/reindex.d.ts +110 -20
  18. package/dist/src/runtime/reindex.js +452 -90
  19. package/dist/src/runtime/reindex.js.map +7 -8
  20. package/dist/src/runtime/reorg.d.ts +110 -20
  21. package/dist/src/runtime/reorg.js +452 -90
  22. package/dist/src/runtime/reorg.js.map +7 -8
  23. package/dist/src/runtime/runner.d.ts +152 -42
  24. package/dist/src/runtime/runner.js +226 -30
  25. package/dist/src/runtime/runner.js.map +4 -4
  26. package/dist/src/runtime/source-matcher.d.ts +88 -31
  27. package/dist/src/runtime/source-matcher.js +171 -61
  28. package/dist/src/runtime/source-matcher.js.map +4 -5
  29. package/dist/src/schema/index.d.ts +110 -20
  30. package/dist/src/schema/index.js +34 -9
  31. package/dist/src/schema/index.js.map +3 -3
  32. package/dist/src/service.js +457 -94
  33. package/dist/src/service.js.map +8 -9
  34. package/dist/src/templates.js +52 -51
  35. package/dist/src/templates.js.map +3 -3
  36. package/dist/src/types.d.ts +111 -29
  37. package/dist/src/types.js +1 -20
  38. package/dist/src/types.js.map +3 -4
  39. package/dist/src/validate.d.ts +112 -22
  40. package/dist/src/validate.js +35 -10
  41. package/dist/src/validate.js.map +3 -3
  42. package/package.json +2 -2
@@ -18,19 +18,98 @@ interface SubgraphTable {
18
18
  }
19
19
  /** Subgraph schema — maps table names to table definitions */
20
20
  type SubgraphSchema = Record<string, SubgraphTable>;
21
- /** Source filter for what blockchain data this subgraph processes */
22
- interface SubgraphSource {
23
- /** Contract principal (e.g., SP000...::contract-name). Supports * wildcards. */
24
- contract?: string;
25
- /** Event name/topic to filter on */
26
- event?: string;
27
- /** Function name to filter on */
28
- function?: string;
29
- /** Transaction type filter (e.g., "stx_transfer", "contract_call") */
30
- type?: string;
31
- /** Minimum amount filter (for stx_transfer sources) */
21
+ /** STX event filters */
22
+ interface StxTransferFilter {
23
+ type: "stx_transfer";
24
+ sender?: string;
25
+ recipient?: string;
32
26
  minAmount?: bigint;
27
+ maxAmount?: bigint;
33
28
  }
29
+ interface StxMintFilter {
30
+ type: "stx_mint";
31
+ recipient?: string;
32
+ minAmount?: bigint;
33
+ }
34
+ interface StxBurnFilter {
35
+ type: "stx_burn";
36
+ sender?: string;
37
+ minAmount?: bigint;
38
+ }
39
+ interface StxLockFilter {
40
+ type: "stx_lock";
41
+ lockedAddress?: string;
42
+ minAmount?: bigint;
43
+ }
44
+ /** FT event filters */
45
+ interface FtTransferFilter {
46
+ type: "ft_transfer";
47
+ assetIdentifier?: string;
48
+ sender?: string;
49
+ recipient?: string;
50
+ minAmount?: bigint;
51
+ }
52
+ interface FtMintFilter {
53
+ type: "ft_mint";
54
+ assetIdentifier?: string;
55
+ recipient?: string;
56
+ minAmount?: bigint;
57
+ }
58
+ interface FtBurnFilter {
59
+ type: "ft_burn";
60
+ assetIdentifier?: string;
61
+ sender?: string;
62
+ minAmount?: bigint;
63
+ }
64
+ /** NFT event filters */
65
+ interface NftTransferFilter {
66
+ type: "nft_transfer";
67
+ assetIdentifier?: string;
68
+ sender?: string;
69
+ recipient?: string;
70
+ }
71
+ interface NftMintFilter {
72
+ type: "nft_mint";
73
+ assetIdentifier?: string;
74
+ recipient?: string;
75
+ }
76
+ interface NftBurnFilter {
77
+ type: "nft_burn";
78
+ assetIdentifier?: string;
79
+ sender?: string;
80
+ }
81
+ /** Contract event filters */
82
+ interface ContractCallFilter {
83
+ type: "contract_call";
84
+ contractId?: string;
85
+ functionName?: string;
86
+ caller?: string;
87
+ /** ABI for typed event.args. If omitted, auto-fetched at deploy time. */
88
+ abi?: Record<string, unknown>;
89
+ }
90
+ interface ContractDeployFilter {
91
+ type: "contract_deploy";
92
+ deployer?: string;
93
+ contractName?: string;
94
+ }
95
+ interface PrintEventFilter {
96
+ type: "print_event";
97
+ contractId?: string;
98
+ topic?: string;
99
+ }
100
+ /** All subgraph filter types — discriminated on `type` */
101
+ type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter;
102
+ /** Transaction metadata available in handlers */
103
+ interface TxMeta {
104
+ txId: string;
105
+ sender: string;
106
+ type: string;
107
+ status: string;
108
+ contractId?: string | null;
109
+ functionName?: string | null;
110
+ }
111
+ /** Value or computed function that receives existing row */
112
+ type ComputedValue<T = unknown> = T | ((existing: Record<string, unknown> | null) => T);
34
113
  /** Context passed to subgraph handlers during event processing */
35
114
  interface SubgraphContext {
36
115
  block: {
@@ -39,18 +118,29 @@ interface SubgraphContext {
39
118
  timestamp: number
40
119
  burnBlockHeight: number
41
120
  };
42
- tx: {
43
- txId: string
44
- sender: string
45
- type: string
46
- status: string
47
- };
121
+ tx: TxMeta;
48
122
  insert(table: string, row: Record<string, unknown>): void;
49
123
  update(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
50
124
  upsert(table: string, key: Record<string, unknown>, row: Record<string, unknown>): void;
51
125
  delete(table: string, where: Record<string, unknown>): void;
126
+ /** Partial update — sets only specified fields, preserves others */
127
+ patch(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
128
+ /** Find-then-merge-or-insert. Values can be functions: (existing) => newValue */
129
+ patchOrInsert(table: string, key: Record<string, unknown>, row: Record<string, ComputedValue>): Promise<void>;
52
130
  findOne(table: string, where: Record<string, unknown>): Promise<Record<string, unknown> | null>;
53
131
  findMany(table: string, where: Record<string, unknown>): Promise<Record<string, unknown>[]>;
132
+ /** Format a bigint amount with decimal places */
133
+ formatUnits(value: bigint, decimals: number): string;
134
+ /** Count rows matching filter */
135
+ count(table: string, where?: Record<string, unknown>): Promise<number>;
136
+ /** Sum a numeric column */
137
+ sum(table: string, column: string, where?: Record<string, unknown>): Promise<bigint>;
138
+ /** Min of a numeric column */
139
+ min(table: string, column: string, where?: Record<string, unknown>): Promise<bigint | null>;
140
+ /** Max of a numeric column */
141
+ max(table: string, column: string, where?: Record<string, unknown>): Promise<bigint | null>;
142
+ /** Count distinct values in a column */
143
+ countDistinct(table: string, column: string, where?: Record<string, unknown>): Promise<number>;
54
144
  }
55
145
  /** Handler function that processes events and writes to the subgraph */
56
146
  type SubgraphHandler = (event: Record<string, unknown>, ctx: SubgraphContext) => Promise<void> | void;
@@ -64,11 +154,11 @@ interface SubgraphDefinition {
64
154
  description?: string;
65
155
  /** Block height to start indexing from (default: 1) */
66
156
  startBlock?: number;
67
- /** What blockchain data to process one or more source filters */
68
- sources: SubgraphSource[];
157
+ /** Named source filterskeys become handler keys */
158
+ sources: Record<string, SubgraphFilter>;
69
159
  /** Tables in this subgraph */
70
160
  schema: SubgraphSchema;
71
- /** Keyed handler functions — keys match sourceKey() output, "*" is catch-all */
161
+ /** Handler functions — keys must match source names (or "*" for catch-all) */
72
162
  handlers: Record<string, SubgraphHandler>;
73
163
  }
74
164
  import { Transaction } from "kysely";