@secondlayer/subgraphs 3.20.0 → 3.22.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/dist/src/index.d.ts +188 -16
- package/dist/src/index.js +369 -41
- package/dist/src/index.js.map +12 -11
- package/dist/src/runtime/block-processor.d.ts +100 -13
- package/dist/src/runtime/block-processor.js +208 -22
- package/dist/src/runtime/block-processor.js.map +9 -8
- package/dist/src/runtime/catchup.d.ts +100 -13
- package/dist/src/runtime/catchup.js +208 -22
- package/dist/src/runtime/catchup.js.map +9 -8
- package/dist/src/runtime/context.d.ts +34 -8
- package/dist/src/runtime/context.js +25 -12
- package/dist/src/runtime/context.js.map +4 -4
- package/dist/src/runtime/processor.js +213 -22
- package/dist/src/runtime/processor.js.map +10 -9
- package/dist/src/runtime/reindex.d.ts +100 -13
- package/dist/src/runtime/reindex.js +208 -22
- package/dist/src/runtime/reindex.js.map +9 -8
- package/dist/src/runtime/reorg.d.ts +100 -13
- package/dist/src/runtime/reorg.js +213 -22
- package/dist/src/runtime/reorg.js.map +10 -9
- package/dist/src/runtime/replay.js +29 -6
- package/dist/src/runtime/replay.js.map +3 -3
- package/dist/src/runtime/runner.d.ts +125 -17
- package/dist/src/runtime/runner.js +100 -3
- package/dist/src/runtime/runner.js.map +5 -4
- package/dist/src/runtime/source-matcher.d.ts +98 -11
- package/dist/src/runtime/source-matcher.js +30 -6
- package/dist/src/runtime/source-matcher.js.map +3 -3
- package/dist/src/schema/index.d.ts +100 -13
- package/dist/src/schema/index.js +171 -20
- package/dist/src/schema/index.js.map +5 -5
- package/dist/src/service.js +213 -22
- package/dist/src/service.js.map +10 -9
- package/dist/src/testing/index.d.ts +620 -0
- package/dist/src/testing/index.js +1297 -0
- package/dist/src/testing/index.js.map +16 -0
- package/dist/src/triggers/index.d.ts +87 -9
- package/dist/src/types.d.ts +101 -14
- package/dist/src/validate.d.ts +136 -14
- package/dist/src/validate.js +158 -19
- package/dist/src/validate.js.map +3 -3
- package/package.json +7 -5
|
@@ -3,6 +3,9 @@ var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
|
3
3
|
|
|
4
4
|
// src/runtime/source-matcher.ts
|
|
5
5
|
var patternCache = new Map;
|
|
6
|
+
function matchAny(value, pattern) {
|
|
7
|
+
return Array.isArray(pattern) ? pattern.some((p) => matchPattern(value, p)) : matchPattern(value, pattern);
|
|
8
|
+
}
|
|
6
9
|
function matchPattern(value, pattern) {
|
|
7
10
|
if (!pattern.includes("*"))
|
|
8
11
|
return value === pattern;
|
|
@@ -15,6 +18,22 @@ function matchPattern(value, pattern) {
|
|
|
15
18
|
return re.test(value);
|
|
16
19
|
}
|
|
17
20
|
var EMPTY_SET = new Set;
|
|
21
|
+
function factoryAllows(filter, contractId, factoryContracts) {
|
|
22
|
+
if (!filter.factory)
|
|
23
|
+
return true;
|
|
24
|
+
if (!contractId)
|
|
25
|
+
return false;
|
|
26
|
+
return (factoryContracts.get(filter.factory.from) ?? EMPTY_SET).has(contractId);
|
|
27
|
+
}
|
|
28
|
+
function readPath(payload, path) {
|
|
29
|
+
let cur = payload;
|
|
30
|
+
for (const key of path.split(".")) {
|
|
31
|
+
if (cur === null || typeof cur !== "object")
|
|
32
|
+
return;
|
|
33
|
+
cur = cur[key];
|
|
34
|
+
}
|
|
35
|
+
return cur;
|
|
36
|
+
}
|
|
18
37
|
function traitAllows(filter, contractId, traitContracts) {
|
|
19
38
|
const trait = filter.trait;
|
|
20
39
|
if (!trait)
|
|
@@ -26,7 +45,7 @@ function traitAllows(filter, contractId, traitContracts) {
|
|
|
26
45
|
function assetContract(assetId) {
|
|
27
46
|
return assetId?.split("::")[0];
|
|
28
47
|
}
|
|
29
|
-
function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
48
|
+
function matchFilter(filter, transactions, eventsByTx, traitContracts, factoryContracts = new Map) {
|
|
30
49
|
const results = [];
|
|
31
50
|
switch (filter.type) {
|
|
32
51
|
case "stx_transfer":
|
|
@@ -153,9 +172,11 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
153
172
|
if (tx.type !== "contract_call")
|
|
154
173
|
continue;
|
|
155
174
|
if (filter.contractId) {
|
|
156
|
-
if (!tx.contract_id || !
|
|
175
|
+
if (!tx.contract_id || !matchAny(tx.contract_id, filter.contractId))
|
|
157
176
|
continue;
|
|
158
177
|
}
|
|
178
|
+
if (!factoryAllows(filter, tx.contract_id, factoryContracts))
|
|
179
|
+
continue;
|
|
159
180
|
if (filter.functionName) {
|
|
160
181
|
if (!tx.function_name || !matchPattern(tx.function_name, filter.functionName))
|
|
161
182
|
continue;
|
|
@@ -202,11 +223,13 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
202
223
|
return false;
|
|
203
224
|
const printContractId = data.contract_identifier ?? data.contract_id;
|
|
204
225
|
if (filter.contractId) {
|
|
205
|
-
if (!printContractId || !
|
|
226
|
+
if (!printContractId || !matchAny(printContractId, filter.contractId))
|
|
206
227
|
return false;
|
|
207
228
|
}
|
|
208
229
|
if (!traitAllows(filter, printContractId, traitContracts))
|
|
209
230
|
return false;
|
|
231
|
+
if (!factoryAllows(filter, printContractId, factoryContracts))
|
|
232
|
+
return false;
|
|
210
233
|
return true;
|
|
211
234
|
});
|
|
212
235
|
if (matched.length > 0) {
|
|
@@ -218,7 +241,7 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
218
241
|
}
|
|
219
242
|
return results;
|
|
220
243
|
}
|
|
221
|
-
function matchSources(sources, transactions, events, traitContracts = new Map) {
|
|
244
|
+
function matchSources(sources, transactions, events, traitContracts = new Map, factoryContracts = new Map) {
|
|
222
245
|
const eventsByTx = new Map;
|
|
223
246
|
for (const event of events) {
|
|
224
247
|
const list = eventsByTx.get(event.tx_id) ?? [];
|
|
@@ -228,7 +251,7 @@ function matchSources(sources, transactions, events, traitContracts = new Map) {
|
|
|
228
251
|
const seen = new Set;
|
|
229
252
|
const results = [];
|
|
230
253
|
for (const [sourceName, filter] of Object.entries(sources)) {
|
|
231
|
-
const matches = matchFilter(filter, transactions, eventsByTx, traitContracts);
|
|
254
|
+
const matches = matchFilter(filter, transactions, eventsByTx, traitContracts, factoryContracts);
|
|
232
255
|
for (const match of matches) {
|
|
233
256
|
const dedupeKey = `${match.tx.tx_id}:${sourceName}`;
|
|
234
257
|
if (!seen.has(dedupeKey)) {
|
|
@@ -240,8 +263,9 @@ function matchSources(sources, transactions, events, traitContracts = new Map) {
|
|
|
240
263
|
return results;
|
|
241
264
|
}
|
|
242
265
|
export {
|
|
266
|
+
readPath,
|
|
243
267
|
matchSources
|
|
244
268
|
};
|
|
245
269
|
|
|
246
|
-
//# debugId=
|
|
270
|
+
//# debugId=3A7F11E035AAF9A264756E2164756E21
|
|
247
271
|
//# sourceMappingURL=source-matcher.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/runtime/source-matcher.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { SubgraphFilter } from \"../types.ts\";\n\nexport interface MatchedTx {\n\ttx: TxRecord;\n\tevents: EventRecord[];\n\t/** Source object key — used for handler dispatch */\n\tsourceName: string;\n}\n\ntype TxRecord = {\n\ttx_id: string;\n\ttype: string;\n\tsender: string;\n\tstatus: string;\n\t/** Position within the block — the runner sorts dispatch into chain order. */\n\ttx_index?: number;\n\tcontract_id?: string | null;\n\tfunction_name?: string | null;\n\tfunction_args?: unknown | null;\n\traw_result?: string | null;\n};\n\ntype EventRecord = {\n\tid: string;\n\ttx_id: string;\n\ttype: string;\n\tevent_index: number;\n\tdata: unknown;\n};\n\n// ── Wildcard matching (shared with v1) ──────────────────────────────\n\nconst patternCache = new Map<string, RegExp>();\n\nfunction matchPattern(value: string, pattern: string): boolean {\n\tif (!pattern.includes(\"*\")) return value === pattern;\n\tlet re = patternCache.get(pattern);\n\tif (!re) {\n\t\tconst regex = pattern\n\t\t\t.replace(/[.+^${}()|[\\]\\\\]/g, \"\\\\$&\")\n\t\t\t.replace(/\\*/g, \".*\");\n\t\tre = new RegExp(`^${regex}$`);\n\t\tpatternCache.set(pattern, re);\n\t}\n\treturn re.test(value);\n}\n\n// Trait → set of conforming contract IDs, resolved per block by the caller\n// (block-processor) from the contract registry. Kept as injected data so this\n// module stays pure/sync/DB-less.\nexport type TraitContracts = Map<string, ReadonlySet<string>>;\nconst EMPTY_SET: ReadonlySet<string> = new Set();\n\n/**\n * True when a filter's optional `trait` admits this contract: no trait → always\n * allowed; trait set → the contract must be in the resolved conforming set.\n */\nfunction traitAllows(\n\tfilter: SubgraphFilter,\n\tcontractId: string | undefined | null,\n\ttraitContracts: TraitContracts,\n): boolean {\n\tconst trait = (filter as { trait?: string }).trait;\n\tif (!trait) return true;\n\tif (!contractId) return false;\n\treturn (traitContracts.get(trait) ?? EMPTY_SET).has(contractId);\n}\n\n/** Extract the contract id from an asset identifier (`<contract>::<token>`). */\nfunction assetContract(assetId: string | undefined): string | undefined {\n\treturn assetId?.split(\"::\")[0];\n}\n\n// ── Per-filter-type matchers ────────────────────────────────────────\n\nfunction matchFilter(\n\tfilter: SubgraphFilter,\n\ttransactions: TxRecord[],\n\teventsByTx: Map<string, EventRecord[]>,\n\ttraitContracts: TraitContracts,\n): { tx: TxRecord; events: EventRecord[] }[] {\n\tconst results: { tx: TxRecord; events: EventRecord[] }[] = [];\n\n\tswitch (filter.type) {\n\t\t// ── STX events ──\n\t\tcase \"stx_transfer\":\n\t\tcase \"stx_mint\":\n\t\tcase \"stx_burn\":\n\t\tcase \"stx_lock\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => e.type === eventType);\n\t\t\t\tif (matched.length === 0) continue;\n\n\t\t\t\t// Apply address filters\n\t\t\t\tconst filtered = matched.filter((e) => {\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"lockedAddress\" in filter && filter.lockedAddress) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t!matchPattern(data.locked_address as string, filter.lockedAddress)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Amount filters\n\t\t\t\t\tif (\"minAmount\" in filter && filter.minAmount !== undefined) {\n\t\t\t\t\t\tconst amount = BigInt(\n\t\t\t\t\t\t\t(data.amount ?? data.locked_amount ?? \"0\") as string,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (amount < filter.minAmount) return false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\t\"maxAmount\" in filter &&\n\t\t\t\t\t\t(filter as { maxAmount?: bigint }).maxAmount !== undefined\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst amount = BigInt((data.amount ?? \"0\") as string);\n\t\t\t\t\t\tif (amount > (filter as { maxAmount: bigint }).maxAmount)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (filtered.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: filtered });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── FT events ──\n\t\tcase \"ft_transfer\":\n\t\tcase \"ft_mint\":\n\t\tcase \"ft_burn\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== eventType) return false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\n\t\t\t\t\t// Asset identifier filter\n\t\t\t\t\tif (filter.assetIdentifier) {\n\t\t\t\t\t\tconst assetId = data.asset_identifier as string | undefined;\n\t\t\t\t\t\tif (!assetId || !matchPattern(assetId, filter.assetIdentifier))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Trait scope — the asset's contract must conform.\n\t\t\t\t\tif (\n\t\t\t\t\t\t!traitAllows(\n\t\t\t\t\t\t\tfilter,\n\t\t\t\t\t\t\tassetContract(data.asset_identifier as string | undefined),\n\t\t\t\t\t\t\ttraitContracts,\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t// Address filters\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Amount filter\n\t\t\t\t\tif (filter.minAmount !== undefined) {\n\t\t\t\t\t\tconst amount = BigInt((data.amount ?? \"0\") as string);\n\t\t\t\t\t\tif (amount < filter.minAmount) return false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── NFT events ──\n\t\tcase \"nft_transfer\":\n\t\tcase \"nft_mint\":\n\t\tcase \"nft_burn\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== eventType) return false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\n\t\t\t\t\tif (filter.assetIdentifier) {\n\t\t\t\t\t\tconst assetId = data.asset_identifier as string | undefined;\n\t\t\t\t\t\tif (!assetId || !matchPattern(assetId, filter.assetIdentifier))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\t!traitAllows(\n\t\t\t\t\t\t\tfilter,\n\t\t\t\t\t\t\tassetContract(data.asset_identifier as string | undefined),\n\t\t\t\t\t\t\ttraitContracts,\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Contract call ──\n\t\tcase \"contract_call\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tif (tx.type !== \"contract_call\") continue;\n\n\t\t\t\t// Contract filter\n\t\t\t\tif (filter.contractId) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!tx.contract_id ||\n\t\t\t\t\t\t!matchPattern(tx.contract_id, filter.contractId)\n\t\t\t\t\t)\n\t\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// Function filter\n\t\t\t\tif (filter.functionName) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!tx.function_name ||\n\t\t\t\t\t\t!matchPattern(tx.function_name, filter.functionName)\n\t\t\t\t\t)\n\t\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// Caller filter\n\t\t\t\tif (filter.caller) {\n\t\t\t\t\tif (!matchPattern(tx.sender, filter.caller)) continue;\n\t\t\t\t}\n\t\t\t\t// Trait scope — the called contract must conform.\n\t\t\t\tif (!traitAllows(filter, tx.contract_id, traitContracts)) continue;\n\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tresults.push({ tx, events: txEvents });\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Contract deploy ──\n\t\tcase \"contract_deploy\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tif (tx.type !== \"smart_contract\") continue;\n\n\t\t\t\tif (filter.deployer) {\n\t\t\t\t\tif (!matchPattern(tx.sender, filter.deployer)) continue;\n\t\t\t\t}\n\t\t\t\tif (filter.contractName) {\n\t\t\t\t\tconst name = tx.contract_id?.split(\".\")[1] ?? \"\";\n\t\t\t\t\tif (!matchPattern(name, filter.contractName)) continue;\n\t\t\t\t}\n\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tresults.push({ tx, events: txEvents });\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Print event ──\n\t\tcase \"print_event\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== \"smart_contract_event\" && e.type !== \"contract_event\")\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\t\t\t\t\tif (data.topic !== \"print\") return false;\n\n\t\t\t\t\t// Contract filter — events store the contract under either\n\t\t\t\t\t// `contract_identifier` (legacy smart_contract_event payload)\n\t\t\t\t\t// or `contract_id` (current contract_event payload). Mirror\n\t\t\t\t\t// the streams query which checks both shapes.\n\t\t\t\t\tconst printContractId =\n\t\t\t\t\t\t(data.contract_identifier as string | undefined) ??\n\t\t\t\t\t\t(data.contract_id as string | undefined);\n\t\t\t\t\tif (filter.contractId) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t!printContractId ||\n\t\t\t\t\t\t\t!matchPattern(printContractId, filter.contractId)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (!traitAllows(filter, printContractId, traitContracts))\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t// Topic filter — check the decoded Clarity value's topic field\n\t\t\t\t\t// At this stage data.value is still raw hex; topic filtering happens\n\t\t\t\t\t// after decode in the runner. For now, skip topic filtering here.\n\t\t\t\t\t// The runner will filter by topic after decoding.\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn results;\n}\n\n/**\n * Match named filters against a block's transactions and events.\n * Returns matches with sourceName = the object key from sources.\n */\nexport function matchSources(\n\tsources: Record<string, SubgraphFilter>,\n\ttransactions: TxRecord[],\n\tevents: EventRecord[],\n\ttraitContracts: TraitContracts = new Map(),\n): MatchedTx[] {\n\t// Index events by txId\n\tconst eventsByTx = new Map<string, EventRecord[]>();\n\tfor (const event of events) {\n\t\tconst list = eventsByTx.get(event.tx_id) ?? [];\n\t\tlist.push(event);\n\t\teventsByTx.set(event.tx_id, list);\n\t}\n\n\tconst seen = new Set<string>();\n\tconst results: MatchedTx[] = [];\n\n\tfor (const [sourceName, filter] of Object.entries(sources)) {\n\t\tconst matches = matchFilter(\n\t\t\tfilter,\n\t\t\ttransactions,\n\t\t\teventsByTx,\n\t\t\ttraitContracts,\n\t\t);\n\t\tfor (const match of matches) {\n\t\t\tconst dedupeKey = `${match.tx.tx_id}:${sourceName}`;\n\t\t\tif (!seen.has(dedupeKey)) {\n\t\t\t\tseen.add(dedupeKey);\n\t\t\t\tresults.push({ ...match, sourceName });\n\t\t\t}\n\t\t}\n\t}\n\n\treturn results;\n}\n"
|
|
5
|
+
"import type { SubgraphFilter } from \"../types.ts\";\n\nexport interface MatchedTx {\n\ttx: TxRecord;\n\tevents: EventRecord[];\n\t/** Source object key — used for handler dispatch */\n\tsourceName: string;\n}\n\nexport type TxRecord = {\n\ttx_id: string;\n\ttype: string;\n\tsender: string;\n\tstatus: string;\n\t/** Position within the block — the runner sorts dispatch into chain order. */\n\ttx_index?: number;\n\tcontract_id?: string | null;\n\tfunction_name?: string | null;\n\tfunction_args?: unknown | null;\n\traw_result?: string | null;\n};\n\nexport type EventRecord = {\n\tid: string;\n\ttx_id: string;\n\ttype: string;\n\tevent_index: number;\n\tdata: unknown;\n};\n\n// ── Wildcard matching (shared with v1) ──────────────────────────────\n\nconst patternCache = new Map<string, RegExp>();\n\n/** Match a value against one pattern, or ANY pattern in a set. */\nfunction matchAny(value: string, pattern: string | readonly string[]): boolean {\n\treturn Array.isArray(pattern)\n\t\t? pattern.some((p) => matchPattern(value, p))\n\t\t: matchPattern(value, pattern as string);\n}\n\nfunction matchPattern(value: string, pattern: string): boolean {\n\tif (!pattern.includes(\"*\")) return value === pattern;\n\tlet re = patternCache.get(pattern);\n\tif (!re) {\n\t\tconst regex = pattern\n\t\t\t.replace(/[.+^${}()|[\\]\\\\]/g, \"\\\\$&\")\n\t\t\t.replace(/\\*/g, \".*\");\n\t\tre = new RegExp(`^${regex}$`);\n\t\tpatternCache.set(pattern, re);\n\t}\n\treturn re.test(value);\n}\n\n// Trait → set of conforming contract IDs, resolved per block by the caller\n// (block-processor) from the contract registry. Kept as injected data so this\n// module stays pure/sync/DB-less.\nexport type TraitContracts = Map<string, ReadonlySet<string>>;\nconst EMPTY_SET: ReadonlySet<string> = new Set();\n\n/**\n * True when a filter's optional `trait` admits this contract: no trait → always\n * allowed; trait set → the contract must be in the resolved conforming set.\n */\n/** Addresses discovered per source name (factory sets), resolved by the\n * caller as-of the block being processed. */\nexport type FactoryContracts = ReadonlyMap<string, ReadonlySet<string>>;\n\n/**\n * A factory-scoped source matches only contracts its factory has revealed.\n * Same shape as {@link traitAllows}: the set is injected, so this module\n * stays pure and sync.\n */\nfunction factoryAllows(\n\tfilter: { factory?: { from: string; field: string } },\n\tcontractId: string | undefined | null,\n\tfactoryContracts: FactoryContracts,\n): boolean {\n\tif (!filter.factory) return true;\n\tif (!contractId) return false;\n\treturn (factoryContracts.get(filter.factory.from) ?? EMPTY_SET).has(\n\t\tcontractId,\n\t);\n}\n\n/** Read a dotted path off a decoded payload (`\"data.pool\"`). */\nexport function readPath(payload: unknown, path: string): unknown {\n\tlet cur: unknown = payload;\n\tfor (const key of path.split(\".\")) {\n\t\tif (cur === null || typeof cur !== \"object\") return undefined;\n\t\tcur = (cur as Record<string, unknown>)[key];\n\t}\n\treturn cur;\n}\n\nfunction traitAllows(\n\tfilter: SubgraphFilter,\n\tcontractId: string | undefined | null,\n\ttraitContracts: TraitContracts,\n): boolean {\n\tconst trait = (filter as { trait?: string }).trait;\n\tif (!trait) return true;\n\tif (!contractId) return false;\n\treturn (traitContracts.get(trait) ?? EMPTY_SET).has(contractId);\n}\n\n/** Extract the contract id from an asset identifier (`<contract>::<token>`). */\nfunction assetContract(assetId: string | undefined): string | undefined {\n\treturn assetId?.split(\"::\")[0];\n}\n\n// ── Per-filter-type matchers ────────────────────────────────────────\n\nfunction matchFilter(\n\tfilter: SubgraphFilter,\n\ttransactions: TxRecord[],\n\teventsByTx: Map<string, EventRecord[]>,\n\ttraitContracts: TraitContracts,\n\tfactoryContracts: FactoryContracts = new Map(),\n): { tx: TxRecord; events: EventRecord[] }[] {\n\tconst results: { tx: TxRecord; events: EventRecord[] }[] = [];\n\n\tswitch (filter.type) {\n\t\t// ── STX events ──\n\t\tcase \"stx_transfer\":\n\t\tcase \"stx_mint\":\n\t\tcase \"stx_burn\":\n\t\tcase \"stx_lock\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => e.type === eventType);\n\t\t\t\tif (matched.length === 0) continue;\n\n\t\t\t\t// Apply address filters\n\t\t\t\tconst filtered = matched.filter((e) => {\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"lockedAddress\" in filter && filter.lockedAddress) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t!matchPattern(data.locked_address as string, filter.lockedAddress)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Amount filters\n\t\t\t\t\tif (\"minAmount\" in filter && filter.minAmount !== undefined) {\n\t\t\t\t\t\tconst amount = BigInt(\n\t\t\t\t\t\t\t(data.amount ?? data.locked_amount ?? \"0\") as string,\n\t\t\t\t\t\t);\n\t\t\t\t\t\tif (amount < filter.minAmount) return false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\t\"maxAmount\" in filter &&\n\t\t\t\t\t\t(filter as { maxAmount?: bigint }).maxAmount !== undefined\n\t\t\t\t\t) {\n\t\t\t\t\t\tconst amount = BigInt((data.amount ?? \"0\") as string);\n\t\t\t\t\t\tif (amount > (filter as { maxAmount: bigint }).maxAmount)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (filtered.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: filtered });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── FT events ──\n\t\tcase \"ft_transfer\":\n\t\tcase \"ft_mint\":\n\t\tcase \"ft_burn\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== eventType) return false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\n\t\t\t\t\t// Asset identifier filter\n\t\t\t\t\tif (filter.assetIdentifier) {\n\t\t\t\t\t\tconst assetId = data.asset_identifier as string | undefined;\n\t\t\t\t\t\tif (!assetId || !matchPattern(assetId, filter.assetIdentifier))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Trait scope — the asset's contract must conform.\n\t\t\t\t\tif (\n\t\t\t\t\t\t!traitAllows(\n\t\t\t\t\t\t\tfilter,\n\t\t\t\t\t\t\tassetContract(data.asset_identifier as string | undefined),\n\t\t\t\t\t\t\ttraitContracts,\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t// Address filters\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\t// Amount filter\n\t\t\t\t\tif (filter.minAmount !== undefined) {\n\t\t\t\t\t\tconst amount = BigInt((data.amount ?? \"0\") as string);\n\t\t\t\t\t\tif (amount < filter.minAmount) return false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── NFT events ──\n\t\tcase \"nft_transfer\":\n\t\tcase \"nft_mint\":\n\t\tcase \"nft_burn\": {\n\t\t\tconst eventType = `${filter.type}_event`;\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== eventType) return false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\n\t\t\t\t\tif (filter.assetIdentifier) {\n\t\t\t\t\t\tconst assetId = data.asset_identifier as string | undefined;\n\t\t\t\t\t\tif (!assetId || !matchPattern(assetId, filter.assetIdentifier))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\n\t\t\t\t\t\t!traitAllows(\n\t\t\t\t\t\t\tfilter,\n\t\t\t\t\t\t\tassetContract(data.asset_identifier as string | undefined),\n\t\t\t\t\t\t\ttraitContracts,\n\t\t\t\t\t\t)\n\t\t\t\t\t)\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tif (\"sender\" in filter && filter.sender) {\n\t\t\t\t\t\tif (!matchPattern(data.sender as string, filter.sender))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (\"recipient\" in filter && filter.recipient) {\n\t\t\t\t\t\tif (!matchPattern(data.recipient as string, filter.recipient))\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Contract call ──\n\t\tcase \"contract_call\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tif (tx.type !== \"contract_call\") continue;\n\n\t\t\t\t// Contract filter\n\t\t\t\tif (filter.contractId) {\n\t\t\t\t\tif (!tx.contract_id || !matchAny(tx.contract_id, filter.contractId))\n\t\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// Factory scope: only contracts this source's factory revealed.\n\t\t\t\tif (!factoryAllows(filter, tx.contract_id, factoryContracts)) continue;\n\t\t\t\t// Function filter\n\t\t\t\tif (filter.functionName) {\n\t\t\t\t\tif (\n\t\t\t\t\t\t!tx.function_name ||\n\t\t\t\t\t\t!matchPattern(tx.function_name, filter.functionName)\n\t\t\t\t\t)\n\t\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\t// Caller filter\n\t\t\t\tif (filter.caller) {\n\t\t\t\t\tif (!matchPattern(tx.sender, filter.caller)) continue;\n\t\t\t\t}\n\t\t\t\t// Trait scope — the called contract must conform.\n\t\t\t\tif (!traitAllows(filter, tx.contract_id, traitContracts)) continue;\n\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tresults.push({ tx, events: txEvents });\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Contract deploy ──\n\t\tcase \"contract_deploy\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tif (tx.type !== \"smart_contract\") continue;\n\n\t\t\t\tif (filter.deployer) {\n\t\t\t\t\tif (!matchPattern(tx.sender, filter.deployer)) continue;\n\t\t\t\t}\n\t\t\t\tif (filter.contractName) {\n\t\t\t\t\tconst name = tx.contract_id?.split(\".\")[1] ?? \"\";\n\t\t\t\t\tif (!matchPattern(name, filter.contractName)) continue;\n\t\t\t\t}\n\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tresults.push({ tx, events: txEvents });\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\n\t\t// ── Print event ──\n\t\tcase \"print_event\": {\n\t\t\tfor (const tx of transactions) {\n\t\t\t\tconst txEvents = eventsByTx.get(tx.tx_id) ?? [];\n\t\t\t\tconst matched = txEvents.filter((e) => {\n\t\t\t\t\tif (e.type !== \"smart_contract_event\" && e.type !== \"contract_event\")\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tconst data = e.data as Record<string, unknown> | null;\n\t\t\t\t\tif (!data) return false;\n\t\t\t\t\tif (data.topic !== \"print\") return false;\n\n\t\t\t\t\t// Contract filter — events store the contract under either\n\t\t\t\t\t// `contract_identifier` (legacy smart_contract_event payload)\n\t\t\t\t\t// or `contract_id` (current contract_event payload). Mirror\n\t\t\t\t\t// the streams query which checks both shapes.\n\t\t\t\t\tconst printContractId =\n\t\t\t\t\t\t(data.contract_identifier as string | undefined) ??\n\t\t\t\t\t\t(data.contract_id as string | undefined);\n\t\t\t\t\tif (filter.contractId) {\n\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t!printContractId ||\n\t\t\t\t\t\t\t!matchAny(printContractId, filter.contractId)\n\t\t\t\t\t\t)\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t\tif (!traitAllows(filter, printContractId, traitContracts))\n\t\t\t\t\t\treturn false;\n\t\t\t\t\tif (!factoryAllows(filter, printContractId, factoryContracts))\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t// Topic filter — check the decoded Clarity value's topic field\n\t\t\t\t\t// At this stage data.value is still raw hex; topic filtering happens\n\t\t\t\t\t// after decode in the runner. For now, skip topic filtering here.\n\t\t\t\t\t// The runner will filter by topic after decoding.\n\t\t\t\t\treturn true;\n\t\t\t\t});\n\n\t\t\t\tif (matched.length > 0) {\n\t\t\t\t\tresults.push({ tx, events: matched });\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\n\treturn results;\n}\n\n/**\n * Match named filters against a block's transactions and events.\n * Returns matches with sourceName = the object key from sources.\n */\nexport function matchSources(\n\tsources: Record<string, SubgraphFilter>,\n\ttransactions: TxRecord[],\n\tevents: EventRecord[],\n\ttraitContracts: TraitContracts = new Map(),\n\tfactoryContracts: FactoryContracts = new Map(),\n): MatchedTx[] {\n\t// Index events by txId\n\tconst eventsByTx = new Map<string, EventRecord[]>();\n\tfor (const event of events) {\n\t\tconst list = eventsByTx.get(event.tx_id) ?? [];\n\t\tlist.push(event);\n\t\teventsByTx.set(event.tx_id, list);\n\t}\n\n\tconst seen = new Set<string>();\n\tconst results: MatchedTx[] = [];\n\n\tfor (const [sourceName, filter] of Object.entries(sources)) {\n\t\tconst matches = matchFilter(\n\t\t\tfilter,\n\t\t\ttransactions,\n\t\t\teventsByTx,\n\t\t\ttraitContracts,\n\t\t\tfactoryContracts,\n\t\t);\n\t\tfor (const match of matches) {\n\t\t\tconst dedupeKey = `${match.tx.tx_id}:${sourceName}`;\n\t\t\tif (!seen.has(dedupeKey)) {\n\t\t\t\tseen.add(dedupeKey);\n\t\t\t\tresults.push({ ...match, sourceName });\n\t\t\t}\n\t\t}\n\t}\n\n\treturn results;\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;AAgCA,IAAM,eAAe,IAAI;
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";;;;AAgCA,IAAM,eAAe,IAAI;AAGzB,SAAS,QAAQ,CAAC,OAAe,SAA8C;AAAA,EAC9E,OAAO,MAAM,QAAQ,OAAO,IACzB,QAAQ,KAAK,CAAC,MAAM,aAAa,OAAO,CAAC,CAAC,IAC1C,aAAa,OAAO,OAAiB;AAAA;AAGzC,SAAS,YAAY,CAAC,OAAe,SAA0B;AAAA,EAC9D,IAAI,CAAC,QAAQ,SAAS,GAAG;AAAA,IAAG,OAAO,UAAU;AAAA,EAC7C,IAAI,KAAK,aAAa,IAAI,OAAO;AAAA,EACjC,IA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
8
|
+
"debugId": "3A7F11E035AAF9A264756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -25,15 +25,24 @@ interface SubgraphRelation {
|
|
|
25
25
|
/** Target column(s) the fields point at (a uniqueKeys entry on the target). */
|
|
26
26
|
referencedColumns: string[];
|
|
27
27
|
}
|
|
28
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* Table definition within a subgraph schema.
|
|
30
|
+
*
|
|
31
|
+
* The array fields are `readonly` so a schema literal can be hoisted out of
|
|
32
|
+
* the `defineSubgraph()` call with `as const` (or {@link defineSchema}) and
|
|
33
|
+
* still typecheck — `as const` produces `readonly [readonly ["holder"]]`,
|
|
34
|
+
* which a mutable `string[][]` rejects. Without that, extracting a helper
|
|
35
|
+
* that takes `ctx` meant losing the entire typed surface, which is why the
|
|
36
|
+
* shipped starter reached for `ctx: any`.
|
|
37
|
+
*/
|
|
29
38
|
interface SubgraphTable {
|
|
30
39
|
columns: Record<string, SubgraphColumn>;
|
|
31
40
|
/** Composite indexes (each entry is an array of column names) */
|
|
32
|
-
indexes?: string[][];
|
|
41
|
+
indexes?: readonly (readonly string[])[];
|
|
33
42
|
/** Unique key constraints (each entry is an array of column names). Required for upsert. */
|
|
34
|
-
uniqueKeys?: string[][];
|
|
43
|
+
uniqueKeys?: readonly (readonly string[])[];
|
|
35
44
|
/** Foreign-key relations to other tables (for typed ORM joins). */
|
|
36
|
-
relations?: SubgraphRelation[];
|
|
45
|
+
relations?: readonly SubgraphRelation[];
|
|
37
46
|
}
|
|
38
47
|
/** Subgraph schema — maps table names to table definitions */
|
|
39
48
|
type SubgraphSchema = Record<string, SubgraphTable>;
|
|
@@ -68,6 +77,45 @@ interface StxLockFilter {
|
|
|
68
77
|
type TraitScope = {
|
|
69
78
|
trait?: string
|
|
70
79
|
};
|
|
80
|
+
/**
|
|
81
|
+
* Scope a source to a DYNAMIC address set discovered from another source's
|
|
82
|
+
* events, instead of a fixed contract list.
|
|
83
|
+
*
|
|
84
|
+
* The standard shape it unlocks: a router plus pools created after you
|
|
85
|
+
* deploy. `contractId` arrays cover a set you know at authoring time; a
|
|
86
|
+
* factory covers the set that grows. It is deliberately NOT tied to contract
|
|
87
|
+
* deployment — the address can come from any field of any event, so it also
|
|
88
|
+
* covers launchpad-minted tokens, DAO extension allowlists, and registry
|
|
89
|
+
* entries.
|
|
90
|
+
*
|
|
91
|
+
* ```ts
|
|
92
|
+
* sources: {
|
|
93
|
+
* registry: { type: "print_event", contractId: REGISTRY, topic: "pool-created" },
|
|
94
|
+
* swaps: {
|
|
95
|
+
* type: "print_event",
|
|
96
|
+
* topic: "swap",
|
|
97
|
+
* // every pool the registry has ever announced, including new ones
|
|
98
|
+
* factory: { from: "registry", field: "data.pool" },
|
|
99
|
+
* },
|
|
100
|
+
* }
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* Two guarantees the runtime provides:
|
|
104
|
+
* - **Same-block ordering** — a contract discovered in block N receives its
|
|
105
|
+
* own block-N events. Discovery runs before matching, within the block.
|
|
106
|
+
* - **Reorg-scoped** — the discovered set is stamped with the block that
|
|
107
|
+
* revealed it and rolled back with everything else on a fork, so an address
|
|
108
|
+
* announced on an orphaned chain does not linger forever.
|
|
109
|
+
*/
|
|
110
|
+
interface FactoryScope {
|
|
111
|
+
factory?: {
|
|
112
|
+
/** Source name whose events reveal the addresses. */
|
|
113
|
+
from: string
|
|
114
|
+
/** Dotted path to the address on that source's payload
|
|
115
|
+
* (e.g. `"data.pool"`, `"contractId"`, `"data.token.id"`). */
|
|
116
|
+
field: string
|
|
117
|
+
};
|
|
118
|
+
}
|
|
71
119
|
/** FT event filters */
|
|
72
120
|
interface FtTransferFilter extends TraitScope {
|
|
73
121
|
type: "ft_transfer";
|
|
@@ -106,9 +154,19 @@ interface NftBurnFilter extends TraitScope {
|
|
|
106
154
|
sender?: string;
|
|
107
155
|
}
|
|
108
156
|
/** Contract event filters */
|
|
109
|
-
interface ContractCallFilter extends TraitScope {
|
|
157
|
+
interface ContractCallFilter extends TraitScope, FactoryScope {
|
|
110
158
|
type: "contract_call";
|
|
111
|
-
|
|
159
|
+
/**
|
|
160
|
+
* One contract id, or a SET of them (max 20) — the standard DeFi shape is
|
|
161
|
+
* a router plus N pools, which used to cost N near-identical sources and
|
|
162
|
+
* handlers, or the `"*"` catch-all that erases per-source typing. Payload
|
|
163
|
+
* types are identical per `type`, so one handler serves the set and
|
|
164
|
+
* `event.contractId` disambiguates.
|
|
165
|
+
*
|
|
166
|
+
* Supports `*` wildcards (e.g. `"SP1ABC….pool-*"`). Mutually exclusive
|
|
167
|
+
* with `trait`, which resolves its own set per block.
|
|
168
|
+
*/
|
|
169
|
+
contractId?: string | readonly string[];
|
|
112
170
|
functionName?: string;
|
|
113
171
|
caller?: string;
|
|
114
172
|
/**
|
|
@@ -124,18 +182,47 @@ interface ContractDeployFilter {
|
|
|
124
182
|
deployer?: string;
|
|
125
183
|
contractName?: string;
|
|
126
184
|
}
|
|
127
|
-
|
|
185
|
+
/**
|
|
186
|
+
* One declared field of a print payload.
|
|
187
|
+
*
|
|
188
|
+
* A bare {@link ColumnType} is a required scalar (`amount: "uint"`). The
|
|
189
|
+
* composite forms exist because real print payloads are not flat: BNS-V2
|
|
190
|
+
* emits `name` as a NESTED tuple, and a `prints` vocabulary that could only
|
|
191
|
+
* say `"jsonb"` is why a handler reading flat `data.name` type-checked,
|
|
192
|
+
* deployed, and then decoded to null on every event.
|
|
193
|
+
*
|
|
194
|
+
* - `{ type: F, optional: true }` — the KEY becomes optional on `event.data`
|
|
195
|
+
* (mirrors `always_present: false` in the inferred schema).
|
|
196
|
+
* - `{ tuple: { … } }` — a nested tuple, typed all the way down.
|
|
197
|
+
* - `{ list: F }` — a list of `F`.
|
|
198
|
+
*/
|
|
199
|
+
type PrintField = ColumnType | {
|
|
200
|
+
type: PrintField
|
|
201
|
+
optional: true
|
|
202
|
+
} | {
|
|
203
|
+
tuple: Record<string, PrintField>
|
|
204
|
+
} | {
|
|
205
|
+
list: PrintField
|
|
206
|
+
};
|
|
207
|
+
interface PrintEventFilter extends TraitScope, FactoryScope {
|
|
128
208
|
type: "print_event";
|
|
129
|
-
|
|
209
|
+
/** One contract id, or a set of them (max 20). Supports `*` wildcards.
|
|
210
|
+
* Mutually exclusive with `trait`. */
|
|
211
|
+
contractId?: string | readonly string[];
|
|
130
212
|
topic?: string;
|
|
131
213
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
214
|
+
* Per-topic field schema. When declared, the handler's `event` is a
|
|
215
|
+
* discriminated union keyed by `topic` and `event.data` is typed per topic
|
|
134
216
|
* (e.g. `{ "completed-deposit": { amount: "uint", sender: "principal" } }`).
|
|
135
|
-
*
|
|
136
|
-
*
|
|
217
|
+
* Nested tuples, lists, and optional fields are expressible — see
|
|
218
|
+
* {@link PrintField}.
|
|
219
|
+
*
|
|
220
|
+
* Declaring `prints` also opts the source into RUNTIME validation: an
|
|
221
|
+
* event whose decoded payload does not match is skipped and logged rather
|
|
222
|
+
* than written as nulls (and the deploy-time field lint is promoted from
|
|
223
|
+
* a warning to an error).
|
|
137
224
|
*/
|
|
138
|
-
prints?: Record<string, Record<string,
|
|
225
|
+
prints?: Record<string, Record<string, PrintField>>;
|
|
139
226
|
}
|
|
140
227
|
/** All subgraph filter types — discriminated on `type` */
|
|
141
228
|
type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter;
|
package/dist/src/schema/index.js
CHANGED
|
@@ -14,6 +14,44 @@ var ColumnTypeSchema = z.enum([
|
|
|
14
14
|
"timestamp",
|
|
15
15
|
"jsonb"
|
|
16
16
|
]);
|
|
17
|
+
var PrintFieldSchema = z.lazy(() => z.union([
|
|
18
|
+
ColumnTypeSchema,
|
|
19
|
+
z.object({ type: PrintFieldSchema, optional: z.literal(true) }),
|
|
20
|
+
z.object({ tuple: z.record(z.string(), PrintFieldSchema) }),
|
|
21
|
+
z.object({ list: PrintFieldSchema })
|
|
22
|
+
]));
|
|
23
|
+
var AbiTypeSchema = z.lazy(() => z.union([
|
|
24
|
+
z.string(),
|
|
25
|
+
z.object({ buff: z.object({ length: z.number() }) }),
|
|
26
|
+
z.object({ "string-ascii": z.object({ length: z.number() }) }),
|
|
27
|
+
z.object({ "string-utf8": z.object({ length: z.number() }) }),
|
|
28
|
+
z.object({ optional: AbiTypeSchema }),
|
|
29
|
+
z.object({
|
|
30
|
+
list: z.object({ type: AbiTypeSchema, length: z.number() })
|
|
31
|
+
}),
|
|
32
|
+
z.object({
|
|
33
|
+
tuple: z.array(z.object({ name: z.string(), type: AbiTypeSchema }))
|
|
34
|
+
}),
|
|
35
|
+
z.object({
|
|
36
|
+
response: z.object({ ok: AbiTypeSchema, error: AbiTypeSchema })
|
|
37
|
+
})
|
|
38
|
+
]));
|
|
39
|
+
var AbiContractSchema = z.object({
|
|
40
|
+
functions: z.array(z.object({
|
|
41
|
+
name: z.string(),
|
|
42
|
+
access: z.enum(["public", "read-only", "private"], {
|
|
43
|
+
message: "ABI access must be public | read-only | private — a raw Hiro/Clarinet ABI uses `read_only`. Normalize it first: normalizeAbi() from @secondlayer/stacks/clarity."
|
|
44
|
+
}),
|
|
45
|
+
args: z.array(z.object({ name: z.string(), type: AbiTypeSchema })),
|
|
46
|
+
outputs: AbiTypeSchema
|
|
47
|
+
})),
|
|
48
|
+
maps: z.array(z.unknown()).optional(),
|
|
49
|
+
variables: z.array(z.unknown()).optional(),
|
|
50
|
+
fungible_tokens: z.array(z.unknown()).optional(),
|
|
51
|
+
non_fungible_tokens: z.array(z.unknown()).optional(),
|
|
52
|
+
implemented_traits: z.array(z.unknown()).optional(),
|
|
53
|
+
defined_traits: z.array(z.unknown()).optional()
|
|
54
|
+
});
|
|
17
55
|
var SubgraphColumnSchema = z.object({
|
|
18
56
|
type: ColumnTypeSchema,
|
|
19
57
|
nullable: z.boolean().optional(),
|
|
@@ -48,24 +86,105 @@ var VALID_FILTER_TYPES = [
|
|
|
48
86
|
"contract_deploy",
|
|
49
87
|
"print_event"
|
|
50
88
|
];
|
|
51
|
-
var
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
89
|
+
var contractIdField = z.union([
|
|
90
|
+
z.string(),
|
|
91
|
+
z.array(z.string()).min(1).max(20)
|
|
92
|
+
]);
|
|
93
|
+
var traitScope = { trait: z.string().optional() };
|
|
94
|
+
var factoryScope = {
|
|
95
|
+
factory: z.object({ from: z.string().min(1), field: z.string().min(1) }).strict().optional()
|
|
96
|
+
};
|
|
97
|
+
var amountRange = {
|
|
55
98
|
minAmount: z.bigint().optional(),
|
|
56
|
-
maxAmount: z.bigint().optional()
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
99
|
+
maxAmount: z.bigint().optional()
|
|
100
|
+
};
|
|
101
|
+
var SubgraphFilterUnion = z.discriminatedUnion("type", [
|
|
102
|
+
z.object({
|
|
103
|
+
type: z.literal("stx_transfer"),
|
|
104
|
+
sender: z.string().optional(),
|
|
105
|
+
recipient: z.string().optional(),
|
|
106
|
+
...amountRange
|
|
107
|
+
}).strict(),
|
|
108
|
+
z.object({
|
|
109
|
+
type: z.literal("stx_mint"),
|
|
110
|
+
recipient: z.string().optional(),
|
|
111
|
+
minAmount: z.bigint().optional()
|
|
112
|
+
}).strict(),
|
|
113
|
+
z.object({
|
|
114
|
+
type: z.literal("stx_burn"),
|
|
115
|
+
sender: z.string().optional(),
|
|
116
|
+
minAmount: z.bigint().optional()
|
|
117
|
+
}).strict(),
|
|
118
|
+
z.object({
|
|
119
|
+
type: z.literal("stx_lock"),
|
|
120
|
+
lockedAddress: z.string().optional(),
|
|
121
|
+
minAmount: z.bigint().optional()
|
|
122
|
+
}).strict(),
|
|
123
|
+
z.object({
|
|
124
|
+
type: z.literal("ft_transfer"),
|
|
125
|
+
assetIdentifier: z.string().optional(),
|
|
126
|
+
sender: z.string().optional(),
|
|
127
|
+
recipient: z.string().optional(),
|
|
128
|
+
minAmount: z.bigint().optional(),
|
|
129
|
+
...traitScope
|
|
130
|
+
}).strict(),
|
|
131
|
+
z.object({
|
|
132
|
+
type: z.literal("ft_mint"),
|
|
133
|
+
assetIdentifier: z.string().optional(),
|
|
134
|
+
recipient: z.string().optional(),
|
|
135
|
+
minAmount: z.bigint().optional(),
|
|
136
|
+
...traitScope
|
|
137
|
+
}).strict(),
|
|
138
|
+
z.object({
|
|
139
|
+
type: z.literal("ft_burn"),
|
|
140
|
+
assetIdentifier: z.string().optional(),
|
|
141
|
+
sender: z.string().optional(),
|
|
142
|
+
minAmount: z.bigint().optional(),
|
|
143
|
+
...traitScope
|
|
144
|
+
}).strict(),
|
|
145
|
+
z.object({
|
|
146
|
+
type: z.literal("nft_transfer"),
|
|
147
|
+
assetIdentifier: z.string().optional(),
|
|
148
|
+
sender: z.string().optional(),
|
|
149
|
+
recipient: z.string().optional(),
|
|
150
|
+
...traitScope
|
|
151
|
+
}).strict(),
|
|
152
|
+
z.object({
|
|
153
|
+
type: z.literal("nft_mint"),
|
|
154
|
+
assetIdentifier: z.string().optional(),
|
|
155
|
+
recipient: z.string().optional(),
|
|
156
|
+
...traitScope
|
|
157
|
+
}).strict(),
|
|
158
|
+
z.object({
|
|
159
|
+
type: z.literal("nft_burn"),
|
|
160
|
+
assetIdentifier: z.string().optional(),
|
|
161
|
+
sender: z.string().optional(),
|
|
162
|
+
...traitScope
|
|
163
|
+
}).strict(),
|
|
164
|
+
z.object({
|
|
165
|
+
type: z.literal("contract_call"),
|
|
166
|
+
contractId: contractIdField.optional(),
|
|
167
|
+
functionName: z.string().optional(),
|
|
168
|
+
caller: z.string().optional(),
|
|
169
|
+
abi: AbiContractSchema.optional(),
|
|
170
|
+
...traitScope,
|
|
171
|
+
...factoryScope
|
|
172
|
+
}).strict(),
|
|
173
|
+
z.object({
|
|
174
|
+
type: z.literal("contract_deploy"),
|
|
175
|
+
deployer: z.string().optional(),
|
|
176
|
+
contractName: z.string().optional()
|
|
177
|
+
}).strict(),
|
|
178
|
+
z.object({
|
|
179
|
+
type: z.literal("print_event"),
|
|
180
|
+
contractId: contractIdField.optional(),
|
|
181
|
+
topic: z.string().optional(),
|
|
182
|
+
prints: z.record(z.string(), z.record(z.string(), PrintFieldSchema)).optional(),
|
|
183
|
+
...traitScope,
|
|
184
|
+
...factoryScope
|
|
185
|
+
}).strict()
|
|
186
|
+
]);
|
|
187
|
+
var SubgraphFilterSchema = SubgraphFilterUnion;
|
|
69
188
|
var SubgraphDefinitionSchema = z.object({
|
|
70
189
|
name: SubgraphNameSchema,
|
|
71
190
|
version: z.string().optional(),
|
|
@@ -79,6 +198,23 @@ var SubgraphDefinitionSchema = z.object({
|
|
|
79
198
|
function validateSubgraphDefinition(def) {
|
|
80
199
|
return SubgraphDefinitionSchema.parse(def);
|
|
81
200
|
}
|
|
201
|
+
function filterFieldsByType() {
|
|
202
|
+
const options = SubgraphFilterUnion?._def?.options ?? [];
|
|
203
|
+
const out = [];
|
|
204
|
+
for (const option of options) {
|
|
205
|
+
const shape = option?.shape ?? option?._def?.shape;
|
|
206
|
+
if (!shape)
|
|
207
|
+
continue;
|
|
208
|
+
const literal = shape.type?._def?.values?.[0] ?? shape.type?._def?.value ?? undefined;
|
|
209
|
+
if (typeof literal !== "string")
|
|
210
|
+
continue;
|
|
211
|
+
out.push({
|
|
212
|
+
type: literal,
|
|
213
|
+
fields: Object.keys(shape).filter((k) => k !== "type")
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
return out;
|
|
217
|
+
}
|
|
82
218
|
|
|
83
219
|
// src/schema/generator.ts
|
|
84
220
|
import { createHash } from "node:crypto";
|
|
@@ -174,6 +310,18 @@ function emitJournalDDL(schemaName) {
|
|
|
174
310
|
`CREATE INDEX IF NOT EXISTS idx_${schemaName}_journal_height ON ${schemaName}._journal (block_height)`
|
|
175
311
|
];
|
|
176
312
|
}
|
|
313
|
+
function emitFactoryDDL(schemaName) {
|
|
314
|
+
return [
|
|
315
|
+
`CREATE TABLE IF NOT EXISTS ${schemaName}._factory_addresses (
|
|
316
|
+
source_name TEXT NOT NULL,
|
|
317
|
+
address TEXT NOT NULL,
|
|
318
|
+
block_height BIGINT NOT NULL,
|
|
319
|
+
_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
320
|
+
PRIMARY KEY (source_name, address)
|
|
321
|
+
)`,
|
|
322
|
+
`CREATE INDEX IF NOT EXISTS idx_${schemaName}_factory_height ON ${schemaName}._factory_addresses (block_height)`
|
|
323
|
+
];
|
|
324
|
+
}
|
|
177
325
|
function emitForeignKeyDDL(schemaName, tableName, tableDef) {
|
|
178
326
|
return (tableDef.relations ?? []).map((rel) => {
|
|
179
327
|
const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
|
|
@@ -192,6 +340,9 @@ function generateSubgraphSQL(def, schemaNameOverride) {
|
|
|
192
340
|
statements.push(...emitTableDDL(schemaName, tableName, tableDef));
|
|
193
341
|
}
|
|
194
342
|
statements.push(...emitJournalDDL(schemaName));
|
|
343
|
+
if (Object.values(def.sources ?? {}).some((src) => src.factory !== undefined)) {
|
|
344
|
+
statements.push(...emitFactoryDDL(schemaName));
|
|
345
|
+
}
|
|
195
346
|
for (const [tableName, tableDef] of Object.entries(def.schema)) {
|
|
196
347
|
statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
|
|
197
348
|
}
|
|
@@ -217,8 +368,8 @@ function diffKeyLists(existing, incoming) {
|
|
|
217
368
|
const existingSet = new Set((existing ?? []).map(key));
|
|
218
369
|
const incomingSet = new Set((incoming ?? []).map(key));
|
|
219
370
|
return {
|
|
220
|
-
added: (incoming ?? []).filter((cols) => !existingSet.has(key(cols))),
|
|
221
|
-
removed: (existing ?? []).filter((cols) => !incomingSet.has(key(cols)))
|
|
371
|
+
added: (incoming ?? []).filter((cols) => !existingSet.has(key(cols))).map((cols) => [...cols]),
|
|
372
|
+
removed: (existing ?? []).filter((cols) => !incomingSet.has(key(cols))).map((cols) => [...cols])
|
|
222
373
|
};
|
|
223
374
|
}
|
|
224
375
|
function diffSchema(existing, incoming) {
|
|
@@ -567,5 +718,5 @@ export {
|
|
|
567
718
|
TYPE_MAP
|
|
568
719
|
};
|
|
569
720
|
|
|
570
|
-
//# debugId=
|
|
721
|
+
//# debugId=A19E9CF28F38E66064756E2164756E21
|
|
571
722
|
//# sourceMappingURL=index.js.map
|