@secondlayer/subgraphs 3.21.0 → 3.23.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 +238 -17
- package/dist/src/index.js +389 -52
- package/dist/src/index.js.map +12 -11
- package/dist/src/runtime/block-processor.d.ts +111 -10
- package/dist/src/runtime/block-processor.js +263 -31
- package/dist/src/runtime/block-processor.js.map +9 -8
- package/dist/src/runtime/catchup.d.ts +111 -10
- package/dist/src/runtime/catchup.js +263 -31
- package/dist/src/runtime/catchup.js.map +9 -8
- package/dist/src/runtime/context.d.ts +111 -11
- package/dist/src/runtime/context.js +166 -12
- package/dist/src/runtime/context.js.map +7 -6
- package/dist/src/runtime/processor.js +272 -35
- package/dist/src/runtime/processor.js.map +10 -9
- package/dist/src/runtime/reindex.d.ts +111 -10
- package/dist/src/runtime/reindex.js +263 -31
- package/dist/src/runtime/reindex.js.map +9 -8
- package/dist/src/runtime/reorg.d.ts +111 -10
- package/dist/src/runtime/reorg.js +268 -31
- package/dist/src/runtime/reorg.js.map +10 -9
- package/dist/src/runtime/replay.d.ts +3 -3
- package/dist/src/runtime/replay.js +30 -6
- package/dist/src/runtime/replay.js.map +4 -4
- package/dist/src/runtime/runner.d.ts +191 -17
- package/dist/src/runtime/source-matcher.d.ts +68 -8
- package/dist/src/runtime/source-matcher.js +30 -6
- package/dist/src/runtime/source-matcher.js.map +3 -3
- package/dist/src/runtime/stats.d.ts +3 -3
- package/dist/src/schema/index.d.ts +114 -13
- package/dist/src/schema/index.js +133 -20
- package/dist/src/schema/index.js.map +5 -5
- package/dist/src/service.js +272 -35
- package/dist/src/service.js.map +10 -9
- package/dist/src/testing/index.d.ts +667 -0
- package/dist/src/testing/index.js +1463 -0
- package/dist/src/testing/index.js.map +17 -0
- package/dist/src/triggers/index.d.ts +57 -6
- package/dist/src/types.d.ts +112 -11
- package/dist/src/validate.d.ts +129 -11
- package/dist/src/validate.js +117 -18
- package/dist/src/validate.js.map +3 -3
- package/package.json +9 -3
|
@@ -1,4 +1,32 @@
|
|
|
1
|
-
import { AbiContract } from "@secondlayer/stacks/clarity";
|
|
1
|
+
import { AbiContract as AbiContract2 } from "@secondlayer/stacks/clarity";
|
|
2
|
+
/**
|
|
3
|
+
* How long a read stays valid.
|
|
4
|
+
*
|
|
5
|
+
* - `per-block` (default): pinned to the block being processed, cached under
|
|
6
|
+
* its `index_block_hash`. Sound for any read-only function — reindexing the
|
|
7
|
+
* same block always returns the same value, and a reorg-replaced block has a
|
|
8
|
+
* different id so it can't inherit the orphaned answer.
|
|
9
|
+
* - `contract-constant`: the caller asserts the value cannot change for this
|
|
10
|
+
* contract and these args (SIP-010 `get-decimals`/`get-symbol`), so it is
|
|
11
|
+
* resolved ONCE instead of once per block. This is the mode that makes a
|
|
12
|
+
* backfill affordable — under `per-block` every block is a distinct key and
|
|
13
|
+
* therefore a distinct RPC. Declaring it on a value that does change pins the
|
|
14
|
+
* first answer forever, so it is opt-in and never inferred.
|
|
15
|
+
*/
|
|
16
|
+
type ChainReadCacheMode = "per-block" | "contract-constant";
|
|
17
|
+
type ChainReadOptions = {
|
|
18
|
+
cache?: ChainReadCacheMode
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* `ChainReadClient` with the ABI generic erased. Structurally cheap, so the
|
|
22
|
+
* base `SubgraphContext` can carry it; `TypedSubgraphContext` uses the real
|
|
23
|
+
* one and handlers get full inference.
|
|
24
|
+
*/
|
|
25
|
+
interface ErasedChainReadClient {
|
|
26
|
+
contract(contractId: string, abi: unknown, options?: ChainReadOptions): {
|
|
27
|
+
read: Record<string, (args?: Record<string, unknown>) => Promise<unknown>>
|
|
28
|
+
};
|
|
29
|
+
}
|
|
2
30
|
/** Supported column types for subgraph schemas */
|
|
3
31
|
type ColumnType = "text" | "uint" | "int" | "principal" | "boolean" | "timestamp" | "jsonb";
|
|
4
32
|
/** Column definition in a subgraph table */
|
|
@@ -25,15 +53,24 @@ interface SubgraphRelation {
|
|
|
25
53
|
/** Target column(s) the fields point at (a uniqueKeys entry on the target). */
|
|
26
54
|
referencedColumns: string[];
|
|
27
55
|
}
|
|
28
|
-
/**
|
|
56
|
+
/**
|
|
57
|
+
* Table definition within a subgraph schema.
|
|
58
|
+
*
|
|
59
|
+
* The array fields are `readonly` so a schema literal can be hoisted out of
|
|
60
|
+
* the `defineSubgraph()` call with `as const` (or {@link defineSchema}) and
|
|
61
|
+
* still typecheck — `as const` produces `readonly [readonly ["holder"]]`,
|
|
62
|
+
* which a mutable `string[][]` rejects. Without that, extracting a helper
|
|
63
|
+
* that takes `ctx` meant losing the entire typed surface, which is why the
|
|
64
|
+
* shipped starter reached for `ctx: any`.
|
|
65
|
+
*/
|
|
29
66
|
interface SubgraphTable {
|
|
30
67
|
columns: Record<string, SubgraphColumn>;
|
|
31
68
|
/** Composite indexes (each entry is an array of column names) */
|
|
32
|
-
indexes?: string[][];
|
|
69
|
+
indexes?: readonly (readonly string[])[];
|
|
33
70
|
/** Unique key constraints (each entry is an array of column names). Required for upsert. */
|
|
34
|
-
uniqueKeys?: string[][];
|
|
71
|
+
uniqueKeys?: readonly (readonly string[])[];
|
|
35
72
|
/** Foreign-key relations to other tables (for typed ORM joins). */
|
|
36
|
-
relations?: SubgraphRelation[];
|
|
73
|
+
relations?: readonly SubgraphRelation[];
|
|
37
74
|
}
|
|
38
75
|
/** Subgraph schema — maps table names to table definitions */
|
|
39
76
|
type SubgraphSchema = Record<string, SubgraphTable>;
|
|
@@ -68,6 +105,45 @@ interface StxLockFilter {
|
|
|
68
105
|
type TraitScope = {
|
|
69
106
|
trait?: string
|
|
70
107
|
};
|
|
108
|
+
/**
|
|
109
|
+
* Scope a source to a DYNAMIC address set discovered from another source's
|
|
110
|
+
* events, instead of a fixed contract list.
|
|
111
|
+
*
|
|
112
|
+
* The standard shape it unlocks: a router plus pools created after you
|
|
113
|
+
* deploy. `contractId` arrays cover a set you know at authoring time; a
|
|
114
|
+
* factory covers the set that grows. It is deliberately NOT tied to contract
|
|
115
|
+
* deployment — the address can come from any field of any event, so it also
|
|
116
|
+
* covers launchpad-minted tokens, DAO extension allowlists, and registry
|
|
117
|
+
* entries.
|
|
118
|
+
*
|
|
119
|
+
* ```ts
|
|
120
|
+
* sources: {
|
|
121
|
+
* registry: { type: "print_event", contractId: REGISTRY, topic: "pool-created" },
|
|
122
|
+
* swaps: {
|
|
123
|
+
* type: "print_event",
|
|
124
|
+
* topic: "swap",
|
|
125
|
+
* // every pool the registry has ever announced, including new ones
|
|
126
|
+
* factory: { from: "registry", field: "data.pool" },
|
|
127
|
+
* },
|
|
128
|
+
* }
|
|
129
|
+
* ```
|
|
130
|
+
*
|
|
131
|
+
* Two guarantees the runtime provides:
|
|
132
|
+
* - **Same-block ordering** — a contract discovered in block N receives its
|
|
133
|
+
* own block-N events. Discovery runs before matching, within the block.
|
|
134
|
+
* - **Reorg-scoped** — the discovered set is stamped with the block that
|
|
135
|
+
* revealed it and rolled back with everything else on a fork, so an address
|
|
136
|
+
* announced on an orphaned chain does not linger forever.
|
|
137
|
+
*/
|
|
138
|
+
interface FactoryScope {
|
|
139
|
+
factory?: {
|
|
140
|
+
/** Source name whose events reveal the addresses. */
|
|
141
|
+
from: string
|
|
142
|
+
/** Dotted path to the address on that source's payload
|
|
143
|
+
* (e.g. `"data.pool"`, `"contractId"`, `"data.token.id"`). */
|
|
144
|
+
field: string
|
|
145
|
+
};
|
|
146
|
+
}
|
|
71
147
|
/** FT event filters */
|
|
72
148
|
interface FtTransferFilter extends TraitScope {
|
|
73
149
|
type: "ft_transfer";
|
|
@@ -106,9 +182,19 @@ interface NftBurnFilter extends TraitScope {
|
|
|
106
182
|
sender?: string;
|
|
107
183
|
}
|
|
108
184
|
/** Contract event filters */
|
|
109
|
-
interface ContractCallFilter extends TraitScope {
|
|
185
|
+
interface ContractCallFilter extends TraitScope, FactoryScope {
|
|
110
186
|
type: "contract_call";
|
|
111
|
-
|
|
187
|
+
/**
|
|
188
|
+
* One contract id, or a SET of them (max 20) — the standard DeFi shape is
|
|
189
|
+
* a router plus N pools, which used to cost N near-identical sources and
|
|
190
|
+
* handlers, or the `"*"` catch-all that erases per-source typing. Payload
|
|
191
|
+
* types are identical per `type`, so one handler serves the set and
|
|
192
|
+
* `event.contractId` disambiguates.
|
|
193
|
+
*
|
|
194
|
+
* Supports `*` wildcards (e.g. `"SP1ABC….pool-*"`). Mutually exclusive
|
|
195
|
+
* with `trait`, which resolves its own set per block.
|
|
196
|
+
*/
|
|
197
|
+
contractId?: string | readonly string[];
|
|
112
198
|
functionName?: string;
|
|
113
199
|
caller?: string;
|
|
114
200
|
/**
|
|
@@ -117,7 +203,7 @@ interface ContractCallFilter extends TraitScope {
|
|
|
117
203
|
* definition and used at runtime to decode args by name. Omit to keep
|
|
118
204
|
* `event.args` as a positional `unknown[]`.
|
|
119
205
|
*/
|
|
120
|
-
abi?:
|
|
206
|
+
abi?: AbiContract2;
|
|
121
207
|
}
|
|
122
208
|
interface ContractDeployFilter {
|
|
123
209
|
type: "contract_deploy";
|
|
@@ -146,9 +232,11 @@ type PrintField = ColumnType | {
|
|
|
146
232
|
} | {
|
|
147
233
|
list: PrintField
|
|
148
234
|
};
|
|
149
|
-
interface PrintEventFilter extends TraitScope {
|
|
235
|
+
interface PrintEventFilter extends TraitScope, FactoryScope {
|
|
150
236
|
type: "print_event";
|
|
151
|
-
|
|
237
|
+
/** One contract id, or a set of them (max 20). Supports `*` wildcards.
|
|
238
|
+
* Mutually exclusive with `trait`. */
|
|
239
|
+
contractId?: string | readonly string[];
|
|
152
240
|
topic?: string;
|
|
153
241
|
/**
|
|
154
242
|
* Per-topic field schema. When declared, the handler's `event` is a
|
|
@@ -186,8 +274,21 @@ interface SubgraphContext {
|
|
|
186
274
|
hash: string
|
|
187
275
|
timestamp: number
|
|
188
276
|
burnBlockHeight: number
|
|
277
|
+
/** Nakamoto StacksBlockId, when the block row carries one. */
|
|
278
|
+
indexBlockHash?: string | null
|
|
189
279
|
};
|
|
190
280
|
tx: TxMeta;
|
|
281
|
+
/**
|
|
282
|
+
* Read-only contract calls pinned to this block. Cached, so the same
|
|
283
|
+
* (contract, function, args) is fetched once per block rather than once
|
|
284
|
+
* per event.
|
|
285
|
+
*
|
|
286
|
+
* Erased here on purpose: this interface is compared structurally wherever
|
|
287
|
+
* a subgraph definition is passed around, and carrying an ABI-generic
|
|
288
|
+
* method pushes those comparisons past TS's instantiation depth. Handlers
|
|
289
|
+
* receive `TypedSubgraphContext`, which exposes the fully typed version.
|
|
290
|
+
*/
|
|
291
|
+
client: ErasedChainReadClient;
|
|
191
292
|
insert(table: string, row: Record<string, unknown>): void;
|
|
192
293
|
update(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
|
|
193
294
|
upsert(table: string, key: Record<string, unknown>, row: Record<string, unknown>): void;
|
|
@@ -100,6 +100,18 @@ function emitJournalDDL(schemaName) {
|
|
|
100
100
|
`CREATE INDEX IF NOT EXISTS idx_${schemaName}_journal_height ON ${schemaName}._journal (block_height)`
|
|
101
101
|
];
|
|
102
102
|
}
|
|
103
|
+
function emitFactoryDDL(schemaName) {
|
|
104
|
+
return [
|
|
105
|
+
`CREATE TABLE IF NOT EXISTS ${schemaName}._factory_addresses (
|
|
106
|
+
source_name TEXT NOT NULL,
|
|
107
|
+
address TEXT NOT NULL,
|
|
108
|
+
block_height BIGINT NOT NULL,
|
|
109
|
+
_created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
|
110
|
+
PRIMARY KEY (source_name, address)
|
|
111
|
+
)`,
|
|
112
|
+
`CREATE INDEX IF NOT EXISTS idx_${schemaName}_factory_height ON ${schemaName}._factory_addresses (block_height)`
|
|
113
|
+
];
|
|
114
|
+
}
|
|
103
115
|
function emitForeignKeyDDL(schemaName, tableName, tableDef) {
|
|
104
116
|
return (tableDef.relations ?? []).map((rel) => {
|
|
105
117
|
const constraintName = `fk_${schemaName}_${tableName}_${rel.name}`;
|
|
@@ -118,6 +130,9 @@ function generateSubgraphSQL(def, schemaNameOverride) {
|
|
|
118
130
|
statements.push(...emitTableDDL(schemaName, tableName, tableDef));
|
|
119
131
|
}
|
|
120
132
|
statements.push(...emitJournalDDL(schemaName));
|
|
133
|
+
if (Object.values(def.sources ?? {}).some((src) => src.factory !== undefined)) {
|
|
134
|
+
statements.push(...emitFactoryDDL(schemaName));
|
|
135
|
+
}
|
|
121
136
|
for (const [tableName, tableDef] of Object.entries(def.schema)) {
|
|
122
137
|
statements.push(...emitForeignKeyDDL(schemaName, tableName, tableDef));
|
|
123
138
|
}
|
|
@@ -130,6 +145,139 @@ function generateSubgraphSQL(def, schemaNameOverride) {
|
|
|
130
145
|
return { statements, hash };
|
|
131
146
|
}
|
|
132
147
|
|
|
148
|
+
// src/runtime/chain-read.ts
|
|
149
|
+
import { getSourceDb } from "@secondlayer/shared/db";
|
|
150
|
+
import { http, createPublicClient } from "@secondlayer/stacks";
|
|
151
|
+
import {
|
|
152
|
+
ContractResponseError,
|
|
153
|
+
buildFunctionArgs,
|
|
154
|
+
isResponseOutput,
|
|
155
|
+
readContract
|
|
156
|
+
} from "@secondlayer/stacks/actions";
|
|
157
|
+
import {
|
|
158
|
+
clarityValueToJSUntyped,
|
|
159
|
+
deserializeCV,
|
|
160
|
+
normalizeAbi,
|
|
161
|
+
serializeCV,
|
|
162
|
+
toCamelCase
|
|
163
|
+
} from "@secondlayer/stacks/clarity";
|
|
164
|
+
function readContractAt(ctx, contractId, abi, options) {
|
|
165
|
+
return ctx.client.contract(contractId, abi, options);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
class ChainReadError extends Error {
|
|
169
|
+
name = "ChainReadError";
|
|
170
|
+
}
|
|
171
|
+
function nodeClient(rpcUrl) {
|
|
172
|
+
return createPublicClient({
|
|
173
|
+
transport: http(rpcUrl, { retryCount: 0, timeout: 1e4 })
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
class ConcurrencyGate {
|
|
178
|
+
limit;
|
|
179
|
+
active = 0;
|
|
180
|
+
queue = [];
|
|
181
|
+
constructor(limit) {
|
|
182
|
+
this.limit = limit;
|
|
183
|
+
}
|
|
184
|
+
async run(fn) {
|
|
185
|
+
if (this.active >= this.limit) {
|
|
186
|
+
await new Promise((resolve) => this.queue.push(resolve));
|
|
187
|
+
}
|
|
188
|
+
this.active++;
|
|
189
|
+
try {
|
|
190
|
+
return await fn();
|
|
191
|
+
} finally {
|
|
192
|
+
this.active--;
|
|
193
|
+
this.queue.shift()?.();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
var DEFAULT_MAX_CONCURRENT_READS = 4;
|
|
198
|
+
function maxConcurrentReads() {
|
|
199
|
+
const raw = process.env.SUBGRAPH_CHAIN_READ_CONCURRENCY;
|
|
200
|
+
const parsed = raw ? Number.parseInt(raw, 10) : Number.NaN;
|
|
201
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : DEFAULT_MAX_CONCURRENT_READS;
|
|
202
|
+
}
|
|
203
|
+
var gate = new ConcurrencyGate(maxConcurrentReads());
|
|
204
|
+
function hashArgs(args) {
|
|
205
|
+
const serialized = args.map((arg) => serializeCV(arg)).join(",");
|
|
206
|
+
return Bun.hash(serialized).toString(16);
|
|
207
|
+
}
|
|
208
|
+
function findFunction(abi, name) {
|
|
209
|
+
const fn = abi.functions.find((f) => f.name === name);
|
|
210
|
+
if (!fn) {
|
|
211
|
+
throw new ChainReadError(`No function "${name}" in the supplied ABI.`);
|
|
212
|
+
}
|
|
213
|
+
if (fn.access !== "read-only") {
|
|
214
|
+
throw new ChainReadError(`"${name}" is ${fn.access}, not read-only — a handler cannot call it.`);
|
|
215
|
+
}
|
|
216
|
+
return fn;
|
|
217
|
+
}
|
|
218
|
+
function createChainReadClient(ctx) {
|
|
219
|
+
const readValue = async (contractId, abi, functionName, args, options) => {
|
|
220
|
+
const rpcUrl = ctx.rpcUrl ?? process.env.STACKS_NODE_RPC_URL;
|
|
221
|
+
if (!rpcUrl) {
|
|
222
|
+
throw new ChainReadError("ctx.client needs a Stacks node: set STACKS_NODE_RPC_URL on the subgraph processor.");
|
|
223
|
+
}
|
|
224
|
+
const constant = options.cache === "contract-constant";
|
|
225
|
+
if (!constant && !ctx.indexBlockHash) {
|
|
226
|
+
throw new ChainReadError(`Block ${ctx.blockHeight} has no index_block_hash, so a read cannot be pinned to it. Reading at the node's tip would make this handler non-deterministic (the same reindex would produce different rows). Re-ingest the block, or pass cache: "contract-constant" if the value genuinely cannot change.`);
|
|
227
|
+
}
|
|
228
|
+
const fn = findFunction(abi, functionName);
|
|
229
|
+
const clarityArgs = buildFunctionArgs(fn, args);
|
|
230
|
+
const db = ctx.db ?? getSourceDb();
|
|
231
|
+
const argsHash = hashArgs(clarityArgs);
|
|
232
|
+
const pinnedTo = constant ? null : ctx.indexBlockHash;
|
|
233
|
+
const cached = await db.selectFrom("chain_read_cache").select("result_hex").where("contract_id", "=", contractId).where("function_name", "=", fn.name).where("args_hash", "=", argsHash).where((eb) => pinnedTo === null ? eb("index_block_hash", "is", null) : eb("index_block_hash", "=", pinnedTo)).executeTakeFirst();
|
|
234
|
+
const resultHex = cached?.result_hex ?? await gate.run(async () => {
|
|
235
|
+
const value2 = await readContract(nodeClient(rpcUrl), {
|
|
236
|
+
contract: contractId,
|
|
237
|
+
functionName: fn.name,
|
|
238
|
+
args: clarityArgs,
|
|
239
|
+
tip: ctx.indexBlockHash ?? undefined
|
|
240
|
+
});
|
|
241
|
+
return serializeCV(value2);
|
|
242
|
+
});
|
|
243
|
+
if (!cached) {
|
|
244
|
+
await db.insertInto("chain_read_cache").values({
|
|
245
|
+
contract_id: contractId,
|
|
246
|
+
function_name: fn.name,
|
|
247
|
+
args_hash: argsHash,
|
|
248
|
+
index_block_hash: pinnedTo,
|
|
249
|
+
block_height: constant ? null : ctx.blockHeight,
|
|
250
|
+
result_hex: resultHex
|
|
251
|
+
}).onConflict((oc) => oc.doNothing()).execute();
|
|
252
|
+
}
|
|
253
|
+
const value = deserializeCV(resultHex);
|
|
254
|
+
const js = clarityValueToJSUntyped(fn.outputs, value);
|
|
255
|
+
if (isResponseOutput(fn.outputs)) {
|
|
256
|
+
const response = js;
|
|
257
|
+
if ("ok" in response)
|
|
258
|
+
return response.ok;
|
|
259
|
+
throw new ContractResponseError(`${contractId}::${fn.name} returned an err response.`, response.err);
|
|
260
|
+
}
|
|
261
|
+
return js;
|
|
262
|
+
};
|
|
263
|
+
return {
|
|
264
|
+
contract(contractId, rawAbi, options = {}) {
|
|
265
|
+
const abi = normalizeAbi(rawAbi);
|
|
266
|
+
const camelToKebab = new Map;
|
|
267
|
+
for (const fn of abi.functions) {
|
|
268
|
+
camelToKebab.set(toCamelCase(fn.name), fn.name);
|
|
269
|
+
}
|
|
270
|
+
const read = new Proxy({}, {
|
|
271
|
+
get(_target, prop) {
|
|
272
|
+
const fnName = camelToKebab.get(prop) ?? prop;
|
|
273
|
+
return (args = {}) => readValue(contractId, abi, fnName, args, options);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
return { read };
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
|
|
133
281
|
// src/runtime/context.ts
|
|
134
282
|
var JOURNAL_RETENTION_BLOCKS = 300;
|
|
135
283
|
var journalEnsured = new Set;
|
|
@@ -148,6 +296,7 @@ class SubgraphContext {
|
|
|
148
296
|
ops = [];
|
|
149
297
|
byo;
|
|
150
298
|
journal;
|
|
299
|
+
_client;
|
|
151
300
|
constructor(db, pgSchemaName2, subgraphSchema, block, tx, byo = false, journal = false) {
|
|
152
301
|
this.db = db;
|
|
153
302
|
this.pgSchemaName = pgSchemaName2;
|
|
@@ -157,6 +306,13 @@ class SubgraphContext {
|
|
|
157
306
|
this.byo = byo;
|
|
158
307
|
this.journal = journal;
|
|
159
308
|
}
|
|
309
|
+
get client() {
|
|
310
|
+
this._client ??= createChainReadClient({
|
|
311
|
+
blockHeight: this.block.height,
|
|
312
|
+
indexBlockHash: this.block.indexBlockHash
|
|
313
|
+
});
|
|
314
|
+
return this._client;
|
|
315
|
+
}
|
|
160
316
|
get tx() {
|
|
161
317
|
return this._tx;
|
|
162
318
|
}
|
|
@@ -262,23 +418,21 @@ class SubgraphContext {
|
|
|
262
418
|
formatUnits(value, decimals) {
|
|
263
419
|
return formatUnits(value, decimals);
|
|
264
420
|
}
|
|
265
|
-
async
|
|
266
|
-
this.validateTable(table);
|
|
421
|
+
async readRows(table, where, limit) {
|
|
267
422
|
const qualifiedTable = `"${this.pgSchemaName}"."${table}"`;
|
|
268
423
|
const { clause } = buildWhereClause(where);
|
|
269
|
-
const query = `SELECT * FROM ${qualifiedTable} WHERE ${clause} LIMIT
|
|
424
|
+
const query = `SELECT * FROM ${qualifiedTable} WHERE ${clause}${limit !== undefined ? ` LIMIT ${limit}` : ""}`;
|
|
270
425
|
const { rows } = await sql.raw(query).execute(this.db);
|
|
271
|
-
|
|
272
|
-
|
|
426
|
+
return rows.map((r) => this.coerceRow(table, r));
|
|
427
|
+
}
|
|
428
|
+
async findOne(table, where) {
|
|
429
|
+
this.validateTable(table);
|
|
430
|
+
const row = (await this.readRows(table, where, 1))[0] ?? null;
|
|
431
|
+
return this.overlayOne(table, where, row);
|
|
273
432
|
}
|
|
274
433
|
async findMany(table, where) {
|
|
275
434
|
this.validateTable(table);
|
|
276
|
-
|
|
277
|
-
const { clause } = buildWhereClause(where);
|
|
278
|
-
const query = `SELECT * FROM ${qualifiedTable} WHERE ${clause}`;
|
|
279
|
-
const { rows } = await sql.raw(query).execute(this.db);
|
|
280
|
-
const dbRows = rows.map((r) => this.coerceRow(table, r));
|
|
281
|
-
return this.overlayMany(table, where, dbRows);
|
|
435
|
+
return this.overlayMany(table, where, await this.readRows(table, where));
|
|
282
436
|
}
|
|
283
437
|
overlayOne(table, where, dbRow) {
|
|
284
438
|
if (this.ops.length === 0)
|
|
@@ -720,11 +874,11 @@ function buildWhereClause(where) {
|
|
|
720
874
|
}
|
|
721
875
|
|
|
722
876
|
// src/runtime/clarity.ts
|
|
723
|
-
import { cvToValue, deserializeCV } from "@secondlayer/stacks/clarity";
|
|
877
|
+
import { cvToValue, deserializeCV as deserializeCV2 } from "@secondlayer/stacks/clarity";
|
|
724
878
|
function decodeClarityValue(hex) {
|
|
725
879
|
try {
|
|
726
880
|
const cleanHex = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
727
|
-
const cv =
|
|
881
|
+
const cv = deserializeCV2(cleanHex);
|
|
728
882
|
return cvToValue(cv);
|
|
729
883
|
} catch {
|
|
730
884
|
return hex;
|
|
@@ -755,8 +909,8 @@ import { getErrorMessage } from "@secondlayer/shared";
|
|
|
755
909
|
import { logger as logger2 } from "@secondlayer/shared/logger";
|
|
756
910
|
import {
|
|
757
911
|
clarityValueToJS,
|
|
758
|
-
deserializeCV as
|
|
759
|
-
toCamelCase
|
|
912
|
+
deserializeCV as deserializeCV3,
|
|
913
|
+
toCamelCase as toCamelCase2
|
|
760
914
|
} from "@secondlayer/stacks/clarity";
|
|
761
915
|
|
|
762
916
|
// src/runtime/print-validate.ts
|
|
@@ -919,7 +1073,7 @@ function buildContractCallInput(filter, tx) {
|
|
|
919
1073
|
return;
|
|
920
1074
|
try {
|
|
921
1075
|
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
922
|
-
input[
|
|
1076
|
+
input[toCamelCase2(arg.name)] = decodeArg(arg.type, deserializeCV3(clean));
|
|
923
1077
|
} catch {}
|
|
924
1078
|
});
|
|
925
1079
|
return input;
|
|
@@ -1198,6 +1352,9 @@ async function runHandlers(subgraph, matched, ctx, opts) {
|
|
|
1198
1352
|
|
|
1199
1353
|
// src/runtime/source-matcher.ts
|
|
1200
1354
|
var patternCache = new Map;
|
|
1355
|
+
function matchAny(value, pattern) {
|
|
1356
|
+
return Array.isArray(pattern) ? pattern.some((p) => matchPattern(value, p)) : matchPattern(value, pattern);
|
|
1357
|
+
}
|
|
1201
1358
|
function matchPattern(value, pattern) {
|
|
1202
1359
|
if (!pattern.includes("*"))
|
|
1203
1360
|
return value === pattern;
|
|
@@ -1210,6 +1367,22 @@ function matchPattern(value, pattern) {
|
|
|
1210
1367
|
return re.test(value);
|
|
1211
1368
|
}
|
|
1212
1369
|
var EMPTY_SET = new Set;
|
|
1370
|
+
function factoryAllows(filter, contractId, factoryContracts) {
|
|
1371
|
+
if (!filter.factory)
|
|
1372
|
+
return true;
|
|
1373
|
+
if (!contractId)
|
|
1374
|
+
return false;
|
|
1375
|
+
return (factoryContracts.get(filter.factory.from) ?? EMPTY_SET).has(contractId);
|
|
1376
|
+
}
|
|
1377
|
+
function readPath(payload, path) {
|
|
1378
|
+
let cur = payload;
|
|
1379
|
+
for (const key of path.split(".")) {
|
|
1380
|
+
if (cur === null || typeof cur !== "object")
|
|
1381
|
+
return;
|
|
1382
|
+
cur = cur[key];
|
|
1383
|
+
}
|
|
1384
|
+
return cur;
|
|
1385
|
+
}
|
|
1213
1386
|
function traitAllows(filter, contractId, traitContracts) {
|
|
1214
1387
|
const trait = filter.trait;
|
|
1215
1388
|
if (!trait)
|
|
@@ -1221,7 +1394,7 @@ function traitAllows(filter, contractId, traitContracts) {
|
|
|
1221
1394
|
function assetContract(assetId) {
|
|
1222
1395
|
return assetId?.split("::")[0];
|
|
1223
1396
|
}
|
|
1224
|
-
function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
1397
|
+
function matchFilter(filter, transactions, eventsByTx, traitContracts, factoryContracts = new Map) {
|
|
1225
1398
|
const results = [];
|
|
1226
1399
|
switch (filter.type) {
|
|
1227
1400
|
case "stx_transfer":
|
|
@@ -1348,9 +1521,11 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
1348
1521
|
if (tx.type !== "contract_call")
|
|
1349
1522
|
continue;
|
|
1350
1523
|
if (filter.contractId) {
|
|
1351
|
-
if (!tx.contract_id || !
|
|
1524
|
+
if (!tx.contract_id || !matchAny(tx.contract_id, filter.contractId))
|
|
1352
1525
|
continue;
|
|
1353
1526
|
}
|
|
1527
|
+
if (!factoryAllows(filter, tx.contract_id, factoryContracts))
|
|
1528
|
+
continue;
|
|
1354
1529
|
if (filter.functionName) {
|
|
1355
1530
|
if (!tx.function_name || !matchPattern(tx.function_name, filter.functionName))
|
|
1356
1531
|
continue;
|
|
@@ -1397,11 +1572,13 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
1397
1572
|
return false;
|
|
1398
1573
|
const printContractId = data.contract_identifier ?? data.contract_id;
|
|
1399
1574
|
if (filter.contractId) {
|
|
1400
|
-
if (!printContractId || !
|
|
1575
|
+
if (!printContractId || !matchAny(printContractId, filter.contractId))
|
|
1401
1576
|
return false;
|
|
1402
1577
|
}
|
|
1403
1578
|
if (!traitAllows(filter, printContractId, traitContracts))
|
|
1404
1579
|
return false;
|
|
1580
|
+
if (!factoryAllows(filter, printContractId, factoryContracts))
|
|
1581
|
+
return false;
|
|
1405
1582
|
return true;
|
|
1406
1583
|
});
|
|
1407
1584
|
if (matched.length > 0) {
|
|
@@ -1413,7 +1590,7 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts) {
|
|
|
1413
1590
|
}
|
|
1414
1591
|
return results;
|
|
1415
1592
|
}
|
|
1416
|
-
function matchSources(sources, transactions, events, traitContracts = new Map) {
|
|
1593
|
+
function matchSources(sources, transactions, events, traitContracts = new Map, factoryContracts = new Map) {
|
|
1417
1594
|
const eventsByTx = new Map;
|
|
1418
1595
|
for (const event of events) {
|
|
1419
1596
|
const list = eventsByTx.get(event.tx_id) ?? [];
|
|
@@ -1423,7 +1600,7 @@ function matchSources(sources, transactions, events, traitContracts = new Map) {
|
|
|
1423
1600
|
const seen = new Set;
|
|
1424
1601
|
const results = [];
|
|
1425
1602
|
for (const [sourceName, filter] of Object.entries(sources)) {
|
|
1426
|
-
const matches = matchFilter(filter, transactions, eventsByTx, traitContracts);
|
|
1603
|
+
const matches = matchFilter(filter, transactions, eventsByTx, traitContracts, factoryContracts);
|
|
1427
1604
|
for (const match of matches) {
|
|
1428
1605
|
const dedupeKey = `${match.tx.tx_id}:${sourceName}`;
|
|
1429
1606
|
if (!seen.has(dedupeKey)) {
|
|
@@ -1450,7 +1627,7 @@ import { logger as logger5 } from "@secondlayer/shared/logger";
|
|
|
1450
1627
|
import { sql as sql3 } from "kysely";
|
|
1451
1628
|
|
|
1452
1629
|
// src/runtime/block-source.ts
|
|
1453
|
-
import { getSourceDb } from "@secondlayer/shared/db";
|
|
1630
|
+
import { getSourceDb as getSourceDb2 } from "@secondlayer/shared/db";
|
|
1454
1631
|
import {
|
|
1455
1632
|
IndexHttpClient
|
|
1456
1633
|
} from "@secondlayer/shared/index-http";
|
|
@@ -1511,6 +1688,7 @@ function reconstructBlock(b) {
|
|
|
1511
1688
|
parent_hash: b.parent_hash,
|
|
1512
1689
|
burn_block_height: b.burn_block_height,
|
|
1513
1690
|
burn_block_hash: b.burn_block_hash,
|
|
1691
|
+
index_block_hash: b.index_block_hash ?? null,
|
|
1514
1692
|
timestamp: isoToUnixSeconds(b.block_time),
|
|
1515
1693
|
canonical: true,
|
|
1516
1694
|
created_at: new Date(0)
|
|
@@ -1645,11 +1823,11 @@ function sparseProbeTargets(subgraph) {
|
|
|
1645
1823
|
|
|
1646
1824
|
class PostgresBlockSource {
|
|
1647
1825
|
async getTip() {
|
|
1648
|
-
const progress = await
|
|
1826
|
+
const progress = await getSourceDb2().selectFrom("index_progress").selectAll().where("network", "=", process.env.NETWORK ?? "mainnet").executeTakeFirst();
|
|
1649
1827
|
return progress ? Number(progress.highest_seen_block) : 0;
|
|
1650
1828
|
}
|
|
1651
1829
|
loadBlockRange(fromHeight, toHeight) {
|
|
1652
|
-
return loadBlockRange(
|
|
1830
|
+
return loadBlockRange(getSourceDb2(), fromHeight, toHeight);
|
|
1653
1831
|
}
|
|
1654
1832
|
}
|
|
1655
1833
|
var EVENT_FILTER_TO_INDEX_TYPE = {
|
|
@@ -1718,8 +1896,8 @@ class PublicApiBlockSource {
|
|
|
1718
1896
|
eventTypes;
|
|
1719
1897
|
probeTargets;
|
|
1720
1898
|
needsTransactions;
|
|
1721
|
-
constructor(
|
|
1722
|
-
this.http =
|
|
1899
|
+
constructor(http2, eventTypes, probeTargets, needsTransactions = true) {
|
|
1900
|
+
this.http = http2;
|
|
1723
1901
|
this.eventTypes = eventTypes;
|
|
1724
1902
|
this.probeTargets = probeTargets;
|
|
1725
1903
|
this.needsTransactions = needsTransactions;
|
|
@@ -2078,6 +2256,57 @@ async function resolveRoute(subgraphName, targetDb) {
|
|
|
2078
2256
|
function invalidateSubgraphRoute(subgraphName) {
|
|
2079
2257
|
routeCache.delete(subgraphName);
|
|
2080
2258
|
}
|
|
2259
|
+
async function resolveFactoryContracts(subgraph, blockHeight, schemaName, db, txs, evts) {
|
|
2260
|
+
const factories = new Map;
|
|
2261
|
+
for (const source of Object.values(subgraph.sources)) {
|
|
2262
|
+
const factory = source.factory;
|
|
2263
|
+
if (factory)
|
|
2264
|
+
factories.set(factory.from, factory);
|
|
2265
|
+
}
|
|
2266
|
+
const resolved = new Map;
|
|
2267
|
+
const discovered = [];
|
|
2268
|
+
if (factories.size === 0)
|
|
2269
|
+
return { resolved, discovered };
|
|
2270
|
+
for (const [discoveringSource, factory] of factories) {
|
|
2271
|
+
const known = new Set;
|
|
2272
|
+
try {
|
|
2273
|
+
const rows = await sql3`
|
|
2274
|
+
SELECT address FROM ${sql3.raw(`"${schemaName}"."_factory_addresses"`)}
|
|
2275
|
+
WHERE source_name = ${discoveringSource} AND block_height <= ${blockHeight}
|
|
2276
|
+
`.execute(db);
|
|
2277
|
+
for (const row of rows.rows)
|
|
2278
|
+
known.add(row.address);
|
|
2279
|
+
} catch {}
|
|
2280
|
+
const discovering = subgraph.sources[factory.from];
|
|
2281
|
+
if (discovering) {
|
|
2282
|
+
const matches = matchSources({ [factory.from]: discovering }, txs, evts, new Map, new Map);
|
|
2283
|
+
for (const match of matches) {
|
|
2284
|
+
for (const event of match.events ?? []) {
|
|
2285
|
+
const payload = buildEventPayload(discovering, match.tx, event);
|
|
2286
|
+
const value = readPath(payload, factory.field);
|
|
2287
|
+
if (typeof value === "string" && value.length > 0 && !known.has(value)) {
|
|
2288
|
+
known.add(value);
|
|
2289
|
+
discovered.push({ sourceName: discoveringSource, address: value });
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
}
|
|
2294
|
+
resolved.set(discoveringSource, known);
|
|
2295
|
+
}
|
|
2296
|
+
return { resolved, discovered };
|
|
2297
|
+
}
|
|
2298
|
+
async function persistFactoryDiscoveries(schemaName, db, blockHeight, discovered) {
|
|
2299
|
+
if (discovered.length === 0)
|
|
2300
|
+
return;
|
|
2301
|
+
for (const { sourceName, address } of discovered) {
|
|
2302
|
+
await sql3`
|
|
2303
|
+
INSERT INTO ${sql3.raw(`"${schemaName}"."_factory_addresses"`)}
|
|
2304
|
+
(source_name, address, block_height)
|
|
2305
|
+
VALUES (${sourceName}, ${address}, ${blockHeight})
|
|
2306
|
+
ON CONFLICT (source_name, address) DO NOTHING
|
|
2307
|
+
`.execute(db);
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2081
2310
|
async function resolveTraitContracts(subgraph, blockHeight, db) {
|
|
2082
2311
|
const traits = new Set;
|
|
2083
2312
|
for (const source of Object.values(subgraph.sources)) {
|
|
@@ -2164,8 +2393,11 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
2164
2393
|
txs = data.txs;
|
|
2165
2394
|
evts = data.events;
|
|
2166
2395
|
}
|
|
2396
|
+
const route = await resolveRoute(subgraphName, targetDb);
|
|
2397
|
+
const schemaName = route.schemaName;
|
|
2167
2398
|
const traitContracts = await resolveTraitContracts(subgraph, blockHeight, targetDb);
|
|
2168
|
-
const
|
|
2399
|
+
const { resolved: factoryContracts, discovered } = await resolveFactoryContracts(subgraph, blockHeight, schemaName, targetDb, txs, evts);
|
|
2400
|
+
const matched = matchSources(subgraph.sources, txs, evts, traitContracts, factoryContracts);
|
|
2169
2401
|
result.matched = matched.length;
|
|
2170
2402
|
if (matched.length === 0) {
|
|
2171
2403
|
if (!opts?.skipProgressUpdate) {
|
|
@@ -2173,13 +2405,12 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
2173
2405
|
}
|
|
2174
2406
|
return result;
|
|
2175
2407
|
}
|
|
2176
|
-
const route = await resolveRoute(subgraphName, targetDb);
|
|
2177
|
-
const schemaName = route.schemaName;
|
|
2178
2408
|
const blockMeta = {
|
|
2179
2409
|
height: block.height,
|
|
2180
2410
|
hash: block.hash,
|
|
2181
2411
|
timestamp: block.timestamp,
|
|
2182
|
-
burnBlockHeight: block.burn_block_height
|
|
2412
|
+
burnBlockHeight: block.burn_block_height,
|
|
2413
|
+
indexBlockHash: block.index_block_hash
|
|
2183
2414
|
};
|
|
2184
2415
|
const initialTx = {
|
|
2185
2416
|
txId: "",
|
|
@@ -2232,6 +2463,7 @@ async function processBlock(subgraph, subgraphName, blockHeight, opts) {
|
|
|
2232
2463
|
});
|
|
2233
2464
|
result.processed = runResult.processed;
|
|
2234
2465
|
result.errors = runResult.errors;
|
|
2466
|
+
await persistFactoryDiscoveries(schemaName, route.dataDb, blockHeight, discovered);
|
|
2235
2467
|
await targetDb.transaction().execute(async (tx) => {
|
|
2236
2468
|
if (manifest && manifest.count > 0) {
|
|
2237
2469
|
await emitSubscriptionOutbox(tx, subgraphName, manifest, matcher, block.height);
|
|
@@ -2630,5 +2862,5 @@ export {
|
|
|
2630
2862
|
bumpReorgEpoch
|
|
2631
2863
|
};
|
|
2632
2864
|
|
|
2633
|
-
//# debugId=
|
|
2865
|
+
//# debugId=ADCDCD9F1956197464756E2164756E21
|
|
2634
2866
|
//# sourceMappingURL=catchup.js.map
|