@secondlayer/shared 6.15.0 → 6.17.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/db/index.d.ts +28 -5
- package/dist/src/db/queries/chain-reorgs.d.ts +26 -4
- package/dist/src/db/queries/contracts.d.ts +26 -4
- package/dist/src/db/queries/integrity.d.ts +26 -4
- package/dist/src/db/queries/subgraph-gaps.d.ts +26 -4
- package/dist/src/db/queries/subgraph-operations.d.ts +26 -4
- package/dist/src/db/queries/subgraphs.d.ts +26 -4
- package/dist/src/db/queries/subscriptions.d.ts +41 -7
- package/dist/src/db/queries/subscriptions.js +10 -3
- package/dist/src/db/queries/subscriptions.js.map +3 -3
- package/dist/src/db/schema.d.ts +28 -5
- package/dist/src/index.d.ts +110 -10
- package/dist/src/index.js +177 -4
- package/dist/src/index.js.map +3 -3
- package/dist/src/node/local-client.d.ts +26 -4
- package/dist/src/schemas/index.d.ts +82 -5
- package/dist/src/schemas/index.js +177 -4
- package/dist/src/schemas/index.js.map +3 -3
- package/dist/src/schemas/subscriptions.d.ts +117 -6
- package/dist/src/schemas/subscriptions.js +182 -5
- package/dist/src/schemas/subscriptions.js.map +3 -3
- package/migrations/0088_chain_subscriptions.ts +86 -0
- package/package.json +1 -1
|
@@ -63,18 +63,137 @@ var SubscriptionFilterClauseSchema = z.union([
|
|
|
63
63
|
SubscriptionFilterOperatorSchema
|
|
64
64
|
]);
|
|
65
65
|
var SubscriptionFilterSchema = z.record(z.string().min(1), SubscriptionFilterClauseSchema);
|
|
66
|
+
var CHAIN_TRIGGER_TYPES = [
|
|
67
|
+
"stx_transfer",
|
|
68
|
+
"stx_mint",
|
|
69
|
+
"stx_burn",
|
|
70
|
+
"stx_lock",
|
|
71
|
+
"ft_transfer",
|
|
72
|
+
"ft_mint",
|
|
73
|
+
"ft_burn",
|
|
74
|
+
"nft_transfer",
|
|
75
|
+
"nft_mint",
|
|
76
|
+
"nft_burn",
|
|
77
|
+
"contract_call",
|
|
78
|
+
"contract_deploy",
|
|
79
|
+
"print_event"
|
|
80
|
+
];
|
|
81
|
+
var triggerAmount = z.union([
|
|
82
|
+
z.string().trim().regex(/^\d+$/, "must be a non-negative integer string"),
|
|
83
|
+
z.number().int().nonnegative()
|
|
84
|
+
]);
|
|
85
|
+
var triggerPattern = z.string().trim().min(1);
|
|
86
|
+
var trait = z.string().trim().min(1);
|
|
87
|
+
var ChainTriggerSchema = z.discriminatedUnion("type", [
|
|
88
|
+
z.object({
|
|
89
|
+
type: z.literal("stx_transfer"),
|
|
90
|
+
sender: triggerPattern.optional(),
|
|
91
|
+
recipient: triggerPattern.optional(),
|
|
92
|
+
minAmount: triggerAmount.optional(),
|
|
93
|
+
maxAmount: triggerAmount.optional()
|
|
94
|
+
}).strict(),
|
|
95
|
+
z.object({
|
|
96
|
+
type: z.literal("stx_mint"),
|
|
97
|
+
recipient: triggerPattern.optional(),
|
|
98
|
+
minAmount: triggerAmount.optional()
|
|
99
|
+
}).strict(),
|
|
100
|
+
z.object({
|
|
101
|
+
type: z.literal("stx_burn"),
|
|
102
|
+
sender: triggerPattern.optional(),
|
|
103
|
+
minAmount: triggerAmount.optional()
|
|
104
|
+
}).strict(),
|
|
105
|
+
z.object({
|
|
106
|
+
type: z.literal("stx_lock"),
|
|
107
|
+
lockedAddress: triggerPattern.optional(),
|
|
108
|
+
minAmount: triggerAmount.optional()
|
|
109
|
+
}).strict(),
|
|
110
|
+
z.object({
|
|
111
|
+
type: z.literal("ft_transfer"),
|
|
112
|
+
assetIdentifier: triggerPattern.optional(),
|
|
113
|
+
sender: triggerPattern.optional(),
|
|
114
|
+
recipient: triggerPattern.optional(),
|
|
115
|
+
minAmount: triggerAmount.optional(),
|
|
116
|
+
trait: trait.optional()
|
|
117
|
+
}).strict(),
|
|
118
|
+
z.object({
|
|
119
|
+
type: z.literal("ft_mint"),
|
|
120
|
+
assetIdentifier: triggerPattern.optional(),
|
|
121
|
+
recipient: triggerPattern.optional(),
|
|
122
|
+
minAmount: triggerAmount.optional(),
|
|
123
|
+
trait: trait.optional()
|
|
124
|
+
}).strict(),
|
|
125
|
+
z.object({
|
|
126
|
+
type: z.literal("ft_burn"),
|
|
127
|
+
assetIdentifier: triggerPattern.optional(),
|
|
128
|
+
sender: triggerPattern.optional(),
|
|
129
|
+
minAmount: triggerAmount.optional(),
|
|
130
|
+
trait: trait.optional()
|
|
131
|
+
}).strict(),
|
|
132
|
+
z.object({
|
|
133
|
+
type: z.literal("nft_transfer"),
|
|
134
|
+
assetIdentifier: triggerPattern.optional(),
|
|
135
|
+
sender: triggerPattern.optional(),
|
|
136
|
+
recipient: triggerPattern.optional(),
|
|
137
|
+
trait: trait.optional()
|
|
138
|
+
}).strict(),
|
|
139
|
+
z.object({
|
|
140
|
+
type: z.literal("nft_mint"),
|
|
141
|
+
assetIdentifier: triggerPattern.optional(),
|
|
142
|
+
recipient: triggerPattern.optional(),
|
|
143
|
+
trait: trait.optional()
|
|
144
|
+
}).strict(),
|
|
145
|
+
z.object({
|
|
146
|
+
type: z.literal("nft_burn"),
|
|
147
|
+
assetIdentifier: triggerPattern.optional(),
|
|
148
|
+
sender: triggerPattern.optional(),
|
|
149
|
+
trait: trait.optional()
|
|
150
|
+
}).strict(),
|
|
151
|
+
z.object({
|
|
152
|
+
type: z.literal("contract_call"),
|
|
153
|
+
contractId: triggerPattern.optional(),
|
|
154
|
+
functionName: triggerPattern.optional(),
|
|
155
|
+
caller: triggerPattern.optional(),
|
|
156
|
+
trait: trait.optional()
|
|
157
|
+
}).strict(),
|
|
158
|
+
z.object({
|
|
159
|
+
type: z.literal("contract_deploy"),
|
|
160
|
+
deployer: triggerPattern.optional(),
|
|
161
|
+
contractName: triggerPattern.optional()
|
|
162
|
+
}).strict(),
|
|
163
|
+
z.object({
|
|
164
|
+
type: z.literal("print_event"),
|
|
165
|
+
contractId: triggerPattern.optional(),
|
|
166
|
+
topic: triggerPattern.optional(),
|
|
167
|
+
trait: trait.optional()
|
|
168
|
+
}).strict()
|
|
169
|
+
]);
|
|
170
|
+
var ChainTriggersSchema = z.array(ChainTriggerSchema).min(1).max(50);
|
|
66
171
|
var CreateSubscriptionRequestSchema = z.object({
|
|
67
172
|
name,
|
|
68
|
-
subgraphName: resourceName,
|
|
69
|
-
tableName: resourceName,
|
|
70
|
-
url: webhookUrl,
|
|
173
|
+
subgraphName: resourceName.optional(),
|
|
174
|
+
tableName: resourceName.optional(),
|
|
71
175
|
filter: SubscriptionFilterSchema.optional(),
|
|
176
|
+
triggers: ChainTriggersSchema.optional(),
|
|
177
|
+
url: webhookUrl,
|
|
72
178
|
format: SubscriptionFormatSchema.default("standard-webhooks"),
|
|
73
179
|
runtime: SubscriptionRuntimeSchema.nullable().optional(),
|
|
74
180
|
authConfig: z.record(z.string(), z.unknown()).optional(),
|
|
75
181
|
maxRetries: z.number().int().min(0).max(100).optional(),
|
|
76
182
|
timeoutMs: z.number().int().min(100).max(300000).optional(),
|
|
77
183
|
concurrency: z.number().int().min(1).max(100).optional()
|
|
184
|
+
}).refine((v) => {
|
|
185
|
+
const subgraphMode = v.subgraphName !== undefined || v.tableName !== undefined;
|
|
186
|
+
const chainMode = v.triggers !== undefined;
|
|
187
|
+
if (chainMode && subgraphMode)
|
|
188
|
+
return false;
|
|
189
|
+
if (chainMode)
|
|
190
|
+
return true;
|
|
191
|
+
return v.subgraphName !== undefined && v.tableName !== undefined;
|
|
192
|
+
}, {
|
|
193
|
+
message: "provide either { subgraphName, tableName } for a subgraph subscription OR { triggers } for a chain subscription — not both"
|
|
194
|
+
}).refine((v) => v.filter === undefined || v.triggers === undefined, {
|
|
195
|
+
message: "`filter` applies to subgraph subscriptions; chain subscriptions use `triggers`",
|
|
196
|
+
path: ["filter"]
|
|
78
197
|
});
|
|
79
198
|
var UpdateSubscriptionRequestSchema = z.object({
|
|
80
199
|
name: name.optional(),
|
|
@@ -97,6 +216,60 @@ var ReplaySubscriptionRequestSchema = z.object({
|
|
|
97
216
|
message: "fromBlock must be less than or equal to toBlock",
|
|
98
217
|
path: ["toBlock"]
|
|
99
218
|
});
|
|
219
|
+
var trigger = {
|
|
220
|
+
stxTransfer: (f = {}) => ({
|
|
221
|
+
type: "stx_transfer",
|
|
222
|
+
...f
|
|
223
|
+
}),
|
|
224
|
+
stxMint: (f = {}) => ({
|
|
225
|
+
type: "stx_mint",
|
|
226
|
+
...f
|
|
227
|
+
}),
|
|
228
|
+
stxBurn: (f = {}) => ({
|
|
229
|
+
type: "stx_burn",
|
|
230
|
+
...f
|
|
231
|
+
}),
|
|
232
|
+
stxLock: (f = {}) => ({
|
|
233
|
+
type: "stx_lock",
|
|
234
|
+
...f
|
|
235
|
+
}),
|
|
236
|
+
ftTransfer: (f = {}) => ({
|
|
237
|
+
type: "ft_transfer",
|
|
238
|
+
...f
|
|
239
|
+
}),
|
|
240
|
+
ftMint: (f = {}) => ({
|
|
241
|
+
type: "ft_mint",
|
|
242
|
+
...f
|
|
243
|
+
}),
|
|
244
|
+
ftBurn: (f = {}) => ({
|
|
245
|
+
type: "ft_burn",
|
|
246
|
+
...f
|
|
247
|
+
}),
|
|
248
|
+
nftTransfer: (f = {}) => ({
|
|
249
|
+
type: "nft_transfer",
|
|
250
|
+
...f
|
|
251
|
+
}),
|
|
252
|
+
nftMint: (f = {}) => ({
|
|
253
|
+
type: "nft_mint",
|
|
254
|
+
...f
|
|
255
|
+
}),
|
|
256
|
+
nftBurn: (f = {}) => ({
|
|
257
|
+
type: "nft_burn",
|
|
258
|
+
...f
|
|
259
|
+
}),
|
|
260
|
+
contractCall: (f = {}) => ({
|
|
261
|
+
type: "contract_call",
|
|
262
|
+
...f
|
|
263
|
+
}),
|
|
264
|
+
contractDeploy: (f = {}) => ({
|
|
265
|
+
type: "contract_deploy",
|
|
266
|
+
...f
|
|
267
|
+
}),
|
|
268
|
+
printEvent: (f = {}) => ({
|
|
269
|
+
type: "print_event",
|
|
270
|
+
...f
|
|
271
|
+
})
|
|
272
|
+
};
|
|
100
273
|
var SCALAR_COLUMN_TYPES = new Set([
|
|
101
274
|
"text",
|
|
102
275
|
"uint",
|
|
@@ -152,6 +325,7 @@ function validateSubscriptionFilterForTable(input) {
|
|
|
152
325
|
}
|
|
153
326
|
export {
|
|
154
327
|
validateSubscriptionFilterForTable,
|
|
328
|
+
trigger,
|
|
155
329
|
formatSubscriptionSchemaErrors,
|
|
156
330
|
UpdateSubscriptionRequestSchema,
|
|
157
331
|
SubscriptionStatusSchema,
|
|
@@ -166,8 +340,11 @@ export {
|
|
|
166
340
|
SUBSCRIPTION_FORMATS,
|
|
167
341
|
SUBSCRIPTION_FILTER_OPERATORS,
|
|
168
342
|
ReplaySubscriptionRequestSchema,
|
|
169
|
-
CreateSubscriptionRequestSchema
|
|
343
|
+
CreateSubscriptionRequestSchema,
|
|
344
|
+
ChainTriggersSchema,
|
|
345
|
+
ChainTriggerSchema,
|
|
346
|
+
CHAIN_TRIGGER_TYPES
|
|
170
347
|
};
|
|
171
348
|
|
|
172
|
-
//# debugId=
|
|
349
|
+
//# debugId=420686B508CCE30264756E2164756E21
|
|
173
350
|
//# sourceMappingURL=subscriptions.js.map
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/schemas/subscriptions.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import { z } from \"zod/v4\";\n\nexport const SUBSCRIPTION_FORMATS = [\n\t\"standard-webhooks\",\n\t\"inngest\",\n\t\"trigger\",\n\t\"cloudflare\",\n\t\"cloudevents\",\n\t\"raw\",\n] as const;\n\nexport const SUBSCRIPTION_RUNTIMES = [\n\t\"inngest\",\n\t\"trigger\",\n\t\"cloudflare\",\n\t\"node\",\n] as const;\n\nexport const SUBSCRIPTION_STATUSES = [\"active\", \"paused\", \"error\"] as const;\n\nexport const SUBSCRIPTION_FILTER_OPERATORS = [\n\t\"eq\",\n\t\"neq\",\n\t\"gt\",\n\t\"gte\",\n\t\"lt\",\n\t\"lte\",\n\t\"in\",\n] as const;\n\nconst webhookUrl = z\n\t.string()\n\t.trim()\n\t.min(1)\n\t.refine(\n\t\t(value) => value.startsWith(\"http://\") || value.startsWith(\"https://\"),\n\t\t\"must be an http(s) URL\",\n\t);\n\nconst name = z.string().trim().min(1).max(128);\nconst resourceName = z.string().trim().min(1).max(128);\n\nexport const SubscriptionStatusSchema: z.ZodType<SubscriptionStatus> = z.enum(\n\tSUBSCRIPTION_STATUSES,\n);\nexport const SubscriptionFormatSchema: z.ZodType<SubscriptionFormat> =\n\tz.enum(SUBSCRIPTION_FORMATS);\nexport const SubscriptionRuntimeSchema: z.ZodType<SubscriptionRuntime> = z.enum(\n\tSUBSCRIPTION_RUNTIMES,\n);\n\nexport const SubscriptionFilterPrimitiveSchema: z.ZodType<SubscriptionFilterPrimitive> =\n\tz.union([z.string(), z.number().finite(), z.boolean()]);\n\nexport const SubscriptionFilterOperatorSchema: z.ZodType<SubscriptionFilterOperator> =\n\tz.union([\n\t\tz.object({ eq: SubscriptionFilterPrimitiveSchema }).strict(),\n\t\tz.object({ neq: SubscriptionFilterPrimitiveSchema }).strict(),\n\t\tz.object({ gt: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ gte: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ lt: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ lte: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\tin: z.array(SubscriptionFilterPrimitiveSchema).min(1),\n\t\t\t})\n\t\t\t.strict(),\n\t]);\n\nexport const SubscriptionFilterClauseSchema: z.ZodType<SubscriptionFilterClause> =\n\tz.union([\n\t\tSubscriptionFilterPrimitiveSchema,\n\t\tSubscriptionFilterOperatorSchema,\n\t]);\n\nexport const SubscriptionFilterSchema: z.ZodType<SubscriptionFilter> = z.record(\n\tz.string().min(1),\n\tSubscriptionFilterClauseSchema,\n);\n\nexport const CreateSubscriptionRequestSchema: z.ZodType<ParsedCreateSubscriptionRequest> =\n\tz.object({\n\t\tname,\n\t\tsubgraphName: resourceName,\n\t\ttableName: resourceName,\n\t\turl: webhookUrl,\n\t\tfilter: SubscriptionFilterSchema.optional(),\n\t\tformat: SubscriptionFormatSchema.default(\"standard-webhooks\"),\n\t\truntime: SubscriptionRuntimeSchema.nullable().optional(),\n\t\tauthConfig: z.record(z.string(), z.unknown()).optional(),\n\t\tmaxRetries: z.number().int().min(0).max(100).optional(),\n\t\ttimeoutMs: z.number().int().min(100).max(300_000).optional(),\n\t\tconcurrency: z.number().int().min(1).max(100).optional(),\n\t});\n\nexport const UpdateSubscriptionRequestSchema: z.ZodType<UpdateSubscriptionRequest> =\n\tz\n\t\t.object({\n\t\t\tname: name.optional(),\n\t\t\turl: webhookUrl.optional(),\n\t\t\tfilter: SubscriptionFilterSchema.optional(),\n\t\t\tformat: SubscriptionFormatSchema.optional(),\n\t\t\truntime: SubscriptionRuntimeSchema.nullable().optional(),\n\t\t\tauthConfig: z.record(z.string(), z.unknown()).optional(),\n\t\t\tmaxRetries: z.number().int().min(0).max(100).optional(),\n\t\t\ttimeoutMs: z.number().int().min(100).max(300_000).optional(),\n\t\t\tconcurrency: z.number().int().min(1).max(100).optional(),\n\t\t})\n\t\t.refine((value) => Object.keys(value).length > 0, {\n\t\t\tmessage: \"At least one field must be provided\",\n\t\t});\n\nexport const ReplaySubscriptionRequestSchema: z.ZodType<ReplaySubscriptionRequest> =\n\tz\n\t\t.object({\n\t\t\tfromBlock: z.number().int().nonnegative(),\n\t\t\ttoBlock: z.number().int().nonnegative(),\n\t\t\tforce: z.string().trim().min(1).max(64).optional(),\n\t\t})\n\t\t.refine((value) => value.fromBlock <= value.toBlock, {\n\t\t\tmessage: \"fromBlock must be less than or equal to toBlock\",\n\t\t\tpath: [\"toBlock\"],\n\t\t});\n\nexport type SubscriptionStatus = (typeof SUBSCRIPTION_STATUSES)[number];\nexport type SubscriptionFormat = (typeof SUBSCRIPTION_FORMATS)[number];\nexport type SubscriptionRuntime = (typeof SUBSCRIPTION_RUNTIMES)[number];\nexport type SubscriptionFilterPrimitive = string | number | boolean;\nexport type SubscriptionFilterOperator =\n\t| { eq: SubscriptionFilterPrimitive }\n\t| { neq: SubscriptionFilterPrimitive }\n\t| { gt: string | number }\n\t| { gte: string | number }\n\t| { lt: string | number }\n\t| { lte: string | number }\n\t| { in: SubscriptionFilterPrimitive[] };\nexport type SubscriptionFilterClause =\n\t| SubscriptionFilterPrimitive\n\t| SubscriptionFilterOperator;\nexport type SubscriptionFilter = Record<string, SubscriptionFilterClause>;\n\nexport interface CreateSubscriptionRequest {\n\tname: string;\n\tsubgraphName: string;\n\ttableName: string;\n\turl: string;\n\tfilter?: SubscriptionFilter;\n\tformat?: SubscriptionFormat;\n\truntime?: SubscriptionRuntime | null;\n\tauthConfig?: Record<string, unknown>;\n\tmaxRetries?: number;\n\ttimeoutMs?: number;\n\tconcurrency?: number;\n}\n\nexport interface ParsedCreateSubscriptionRequest\n\textends Omit<CreateSubscriptionRequest, \"format\"> {\n\tformat: SubscriptionFormat;\n}\n\nexport interface UpdateSubscriptionRequest {\n\tname?: string;\n\turl?: string;\n\tfilter?: SubscriptionFilter;\n\tformat?: SubscriptionFormat;\n\truntime?: SubscriptionRuntime | null;\n\tauthConfig?: Record<string, unknown>;\n\tmaxRetries?: number;\n\ttimeoutMs?: number;\n\tconcurrency?: number;\n}\n\nexport type ParsedUpdateSubscriptionRequest = UpdateSubscriptionRequest;\n\nexport interface ReplaySubscriptionRequest {\n\tfromBlock: number;\n\ttoBlock: number;\n\tforce?: string;\n}\n\nexport type ParsedReplaySubscriptionRequest = ReplaySubscriptionRequest;\n\nexport interface SubscriptionSummary {\n\tid: string;\n\tname: string;\n\tstatus: SubscriptionStatus;\n\tsubgraphName: string;\n\ttableName: string;\n\tformat: SubscriptionFormat;\n\truntime: SubscriptionRuntime | null;\n\turl: string;\n\tlastDeliveryAt: string | null;\n\tlastSuccessAt: string | null;\n\tcreatedAt: string;\n\tupdatedAt: string;\n}\n\nexport interface SubscriptionDetail extends SubscriptionSummary {\n\tfilter: Record<string, unknown>;\n\tauthConfig: Record<string, unknown>;\n\tmaxRetries: number;\n\ttimeoutMs: number;\n\tconcurrency: number;\n\tcircuitFailures: number;\n\tcircuitOpenedAt: string | null;\n\tlastError: string | null;\n}\n\nexport interface CreateSubscriptionResponse {\n\tsubscription: SubscriptionDetail;\n\t/** Plaintext signing secret — surfaced ONCE. Store it server-side. */\n\tsigningSecret: string;\n}\n\nexport interface RotateSecretResponse {\n\tsubscription: SubscriptionDetail;\n\tsigningSecret: string;\n}\n\nexport interface DeliveryRow {\n\tid: string;\n\tattempt: number;\n\tstatusCode: number | null;\n\terrorMessage: string | null;\n\tdurationMs: number | null;\n\tresponseBody: string | null;\n\tdispatchedAt: string;\n}\n\nexport interface ReplayResult {\n\treplayId: string;\n\tenqueuedCount: number;\n\tscannedCount: number;\n}\n\nexport interface DeadRow {\n\tid: string;\n\teventType: string;\n\tattempt: number;\n\tblockHeight: number;\n\ttxId: string | null;\n\tpayload: Record<string, unknown>;\n\tfailedAt: string | null;\n\tcreatedAt: string;\n}\n\nexport interface SubscriptionSchemaColumn {\n\ttype?: unknown;\n}\n\nexport interface SubscriptionSchemaTable {\n\tcolumns: Record<string, SubscriptionSchemaColumn>;\n}\n\nexport type SubscriptionSchemaTables = Record<string, SubscriptionSchemaTable>;\n\nconst SCALAR_COLUMN_TYPES = new Set([\n\t\"text\",\n\t\"uint\",\n\t\"int\",\n\t\"principal\",\n\t\"boolean\",\n\t\"timestamp\",\n]);\n\nconst COMPARISON_COLUMN_TYPES = new Set([\"uint\", \"int\", \"timestamp\"]);\n\nfunction formatIssuePath(path: PropertyKey[]): string {\n\treturn path.length > 0 ? `${path.map(String).join(\".\")}: ` : \"\";\n}\n\nexport function formatSubscriptionSchemaErrors(error: z.ZodError): string[] {\n\treturn error.issues.map(\n\t\t(issue) => `${formatIssuePath(issue.path)}${issue.message}`,\n\t);\n}\n\nfunction operatorForClause(clause: SubscriptionFilterClause): string {\n\tif (clause === null || typeof clause !== \"object\" || Array.isArray(clause)) {\n\t\treturn \"eq\";\n\t}\n\treturn Object.keys(clause)[0] ?? \"eq\";\n}\n\nexport function validateSubscriptionFilterForTable(input: {\n\tsubgraphName?: string;\n\ttableName: string;\n\tfilter?: unknown;\n\ttables: SubscriptionSchemaTables;\n}): string[] {\n\tconst errors: string[] = [];\n\tconst table = input.tables[input.tableName];\n\tif (!table) {\n\t\tconst names = Object.keys(input.tables);\n\t\terrors.push(\n\t\t\t`Unknown table \"${input.tableName}\"${\n\t\t\t\tinput.subgraphName ? ` in subgraph \"${input.subgraphName}\"` : \"\"\n\t\t\t}.${names.length > 0 ? ` Available tables: ${names.join(\", \")}.` : \"\"}`,\n\t\t);\n\t\treturn errors;\n\t}\n\n\tif (input.filter === undefined) return errors;\n\n\tconst parsed = SubscriptionFilterSchema.safeParse(input.filter);\n\tif (!parsed.success) {\n\t\treturn formatSubscriptionSchemaErrors(parsed.error);\n\t}\n\n\tfor (const [field, clause] of Object.entries(parsed.data)) {\n\t\tconst column = table.columns[field];\n\t\tif (!column) {\n\t\t\terrors.push(\n\t\t\t\t`Unknown filter field \"${field}\" on table \"${input.tableName}\".`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst columnType =\n\t\t\ttypeof column.type === \"string\" ? column.type.toLowerCase() : \"\";\n\t\tif (!SCALAR_COLUMN_TYPES.has(columnType)) {\n\t\t\terrors.push(\n\t\t\t\t`Filter field \"${field}\" has unsupported type \"${columnType || \"unknown\"}\"; subscription filters require scalar columns.`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst operator = operatorForClause(clause);\n\t\tif (\n\t\t\t(operator === \"gt\" ||\n\t\t\t\toperator === \"gte\" ||\n\t\t\t\toperator === \"lt\" ||\n\t\t\t\toperator === \"lte\") &&\n\t\t\t!COMPARISON_COLUMN_TYPES.has(columnType)\n\t\t) {\n\t\t\terrors.push(\n\t\t\t\t`Operator \"${operator}\" is not supported for ${columnType} field \"${field}\".`,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn errors;\n}\n"
|
|
5
|
+
"import { z } from \"zod/v4\";\n\nexport const SUBSCRIPTION_FORMATS = [\n\t\"standard-webhooks\",\n\t\"inngest\",\n\t\"trigger\",\n\t\"cloudflare\",\n\t\"cloudevents\",\n\t\"raw\",\n] as const;\n\nexport const SUBSCRIPTION_RUNTIMES = [\n\t\"inngest\",\n\t\"trigger\",\n\t\"cloudflare\",\n\t\"node\",\n] as const;\n\nexport const SUBSCRIPTION_STATUSES = [\"active\", \"paused\", \"error\"] as const;\n\nexport const SUBSCRIPTION_FILTER_OPERATORS = [\n\t\"eq\",\n\t\"neq\",\n\t\"gt\",\n\t\"gte\",\n\t\"lt\",\n\t\"lte\",\n\t\"in\",\n] as const;\n\nconst webhookUrl = z\n\t.string()\n\t.trim()\n\t.min(1)\n\t.refine(\n\t\t(value) => value.startsWith(\"http://\") || value.startsWith(\"https://\"),\n\t\t\"must be an http(s) URL\",\n\t);\n\nconst name = z.string().trim().min(1).max(128);\nconst resourceName = z.string().trim().min(1).max(128);\n\nexport const SubscriptionStatusSchema: z.ZodType<SubscriptionStatus> = z.enum(\n\tSUBSCRIPTION_STATUSES,\n);\nexport const SubscriptionFormatSchema: z.ZodType<SubscriptionFormat> =\n\tz.enum(SUBSCRIPTION_FORMATS);\nexport const SubscriptionRuntimeSchema: z.ZodType<SubscriptionRuntime> = z.enum(\n\tSUBSCRIPTION_RUNTIMES,\n);\n\nexport const SubscriptionFilterPrimitiveSchema: z.ZodType<SubscriptionFilterPrimitive> =\n\tz.union([z.string(), z.number().finite(), z.boolean()]);\n\nexport const SubscriptionFilterOperatorSchema: z.ZodType<SubscriptionFilterOperator> =\n\tz.union([\n\t\tz.object({ eq: SubscriptionFilterPrimitiveSchema }).strict(),\n\t\tz.object({ neq: SubscriptionFilterPrimitiveSchema }).strict(),\n\t\tz.object({ gt: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ gte: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ lt: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz.object({ lte: z.union([z.string(), z.number().finite()]) }).strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\tin: z.array(SubscriptionFilterPrimitiveSchema).min(1),\n\t\t\t})\n\t\t\t.strict(),\n\t]);\n\nexport const SubscriptionFilterClauseSchema: z.ZodType<SubscriptionFilterClause> =\n\tz.union([\n\t\tSubscriptionFilterPrimitiveSchema,\n\t\tSubscriptionFilterOperatorSchema,\n\t]);\n\nexport const SubscriptionFilterSchema: z.ZodType<SubscriptionFilter> = z.record(\n\tz.string().min(1),\n\tSubscriptionFilterClauseSchema,\n);\n\n// --- Chain triggers (direct chain-level subscriptions) -----------------------\n// A chain subscription reacts to raw chain events matched directly off the\n// Index/Streams clock (no subgraph). `triggers` is an array of these filters —\n// the JSON mirror of the subgraph runtime's `SubgraphFilter` union. Defined\n// here (not imported from @secondlayer/subgraphs) to avoid a shared→subgraphs\n// cycle; the evaluator maps these to `SubgraphFilter` at match time. Amounts are\n// non-negative integer strings (uint128 can exceed JS safe-int) or numbers.\n\nexport const CHAIN_TRIGGER_TYPES = [\n\t\"stx_transfer\",\n\t\"stx_mint\",\n\t\"stx_burn\",\n\t\"stx_lock\",\n\t\"ft_transfer\",\n\t\"ft_mint\",\n\t\"ft_burn\",\n\t\"nft_transfer\",\n\t\"nft_mint\",\n\t\"nft_burn\",\n\t\"contract_call\",\n\t\"contract_deploy\",\n\t\"print_event\",\n] as const;\n\nconst triggerAmount = z.union([\n\tz.string().trim().regex(/^\\d+$/, \"must be a non-negative integer string\"),\n\tz.number().int().nonnegative(),\n]);\n/** Principal/identifier/name patterns — `*` wildcards allowed (matched by the\n * evaluator). */\nconst triggerPattern = z.string().trim().min(1);\nconst trait = z.string().trim().min(1);\n\nexport const ChainTriggerSchema: z.ZodType<ChainTrigger> = z.discriminatedUnion(\n\t\"type\",\n\t[\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"stx_transfer\"),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t\tmaxAmount: triggerAmount.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"stx_mint\"),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"stx_burn\"),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"stx_lock\"),\n\t\t\t\tlockedAddress: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"ft_transfer\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"ft_mint\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"ft_burn\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\tminAmount: triggerAmount.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"nft_transfer\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"nft_mint\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\trecipient: triggerPattern.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"nft_burn\"),\n\t\t\t\tassetIdentifier: triggerPattern.optional(),\n\t\t\t\tsender: triggerPattern.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"contract_call\"),\n\t\t\t\tcontractId: triggerPattern.optional(),\n\t\t\t\tfunctionName: triggerPattern.optional(),\n\t\t\t\tcaller: triggerPattern.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"contract_deploy\"),\n\t\t\t\tdeployer: triggerPattern.optional(),\n\t\t\t\tcontractName: triggerPattern.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t\tz\n\t\t\t.object({\n\t\t\t\ttype: z.literal(\"print_event\"),\n\t\t\t\tcontractId: triggerPattern.optional(),\n\t\t\t\ttopic: triggerPattern.optional(),\n\t\t\t\ttrait: trait.optional(),\n\t\t\t})\n\t\t\t.strict(),\n\t],\n);\n\nexport const ChainTriggersSchema: z.ZodType<ChainTrigger[]> = z\n\t.array(ChainTriggerSchema)\n\t.min(1)\n\t.max(50);\n\nexport const CreateSubscriptionRequestSchema: z.ZodType<ParsedCreateSubscriptionRequest> =\n\tz\n\t\t.object({\n\t\t\tname,\n\t\t\t// Subgraph mode (kind=subgraph): subgraphName + tableName + optional filter.\n\t\t\tsubgraphName: resourceName.optional(),\n\t\t\ttableName: resourceName.optional(),\n\t\t\tfilter: SubscriptionFilterSchema.optional(),\n\t\t\t// Chain mode (kind=chain): triggers.\n\t\t\ttriggers: ChainTriggersSchema.optional(),\n\t\t\turl: webhookUrl,\n\t\t\tformat: SubscriptionFormatSchema.default(\"standard-webhooks\"),\n\t\t\truntime: SubscriptionRuntimeSchema.nullable().optional(),\n\t\t\tauthConfig: z.record(z.string(), z.unknown()).optional(),\n\t\t\tmaxRetries: z.number().int().min(0).max(100).optional(),\n\t\t\ttimeoutMs: z.number().int().min(100).max(300_000).optional(),\n\t\t\tconcurrency: z.number().int().min(1).max(100).optional(),\n\t\t})\n\t\t.refine(\n\t\t\t(v) => {\n\t\t\t\tconst subgraphMode =\n\t\t\t\t\tv.subgraphName !== undefined || v.tableName !== undefined;\n\t\t\t\tconst chainMode = v.triggers !== undefined;\n\t\t\t\tif (chainMode && subgraphMode) return false;\n\t\t\t\tif (chainMode) return true;\n\t\t\t\t// Subgraph mode requires BOTH subgraphName and tableName.\n\t\t\t\treturn v.subgraphName !== undefined && v.tableName !== undefined;\n\t\t\t},\n\t\t\t{\n\t\t\t\tmessage:\n\t\t\t\t\t\"provide either { subgraphName, tableName } for a subgraph subscription OR { triggers } for a chain subscription — not both\",\n\t\t\t},\n\t\t)\n\t\t.refine((v) => v.filter === undefined || v.triggers === undefined, {\n\t\t\tmessage:\n\t\t\t\t\"`filter` applies to subgraph subscriptions; chain subscriptions use `triggers`\",\n\t\t\tpath: [\"filter\"],\n\t\t});\n\nexport const UpdateSubscriptionRequestSchema: z.ZodType<UpdateSubscriptionRequest> =\n\tz\n\t\t.object({\n\t\t\tname: name.optional(),\n\t\t\turl: webhookUrl.optional(),\n\t\t\tfilter: SubscriptionFilterSchema.optional(),\n\t\t\tformat: SubscriptionFormatSchema.optional(),\n\t\t\truntime: SubscriptionRuntimeSchema.nullable().optional(),\n\t\t\tauthConfig: z.record(z.string(), z.unknown()).optional(),\n\t\t\tmaxRetries: z.number().int().min(0).max(100).optional(),\n\t\t\ttimeoutMs: z.number().int().min(100).max(300_000).optional(),\n\t\t\tconcurrency: z.number().int().min(1).max(100).optional(),\n\t\t})\n\t\t.refine((value) => Object.keys(value).length > 0, {\n\t\t\tmessage: \"At least one field must be provided\",\n\t\t});\n\nexport const ReplaySubscriptionRequestSchema: z.ZodType<ReplaySubscriptionRequest> =\n\tz\n\t\t.object({\n\t\t\tfromBlock: z.number().int().nonnegative(),\n\t\t\ttoBlock: z.number().int().nonnegative(),\n\t\t\tforce: z.string().trim().min(1).max(64).optional(),\n\t\t})\n\t\t.refine((value) => value.fromBlock <= value.toBlock, {\n\t\t\tmessage: \"fromBlock must be less than or equal to toBlock\",\n\t\t\tpath: [\"toBlock\"],\n\t\t});\n\nexport type SubscriptionStatus = (typeof SUBSCRIPTION_STATUSES)[number];\n/** Polymorphic subscription mode (mirrors db/types `SubscriptionKind`). */\nexport type SubscriptionKind = \"subgraph\" | \"chain\";\nexport type SubscriptionFormat = (typeof SUBSCRIPTION_FORMATS)[number];\nexport type SubscriptionRuntime = (typeof SUBSCRIPTION_RUNTIMES)[number];\nexport type SubscriptionFilterPrimitive = string | number | boolean;\nexport type SubscriptionFilterOperator =\n\t| { eq: SubscriptionFilterPrimitive }\n\t| { neq: SubscriptionFilterPrimitive }\n\t| { gt: string | number }\n\t| { gte: string | number }\n\t| { lt: string | number }\n\t| { lte: string | number }\n\t| { in: SubscriptionFilterPrimitive[] };\nexport type SubscriptionFilterClause =\n\t| SubscriptionFilterPrimitive\n\t| SubscriptionFilterOperator;\nexport type SubscriptionFilter = Record<string, SubscriptionFilterClause>;\n\nexport type ChainTriggerType = (typeof CHAIN_TRIGGER_TYPES)[number];\n/** Non-negative integer amount over JSON (string for uint128 safety, or number). */\nexport type ChainTriggerAmount = string | number;\n\ninterface TraitScoped {\n\ttrait?: string;\n}\n\n/** JSON mirror of the subgraph runtime's `SubgraphFilter` union. */\nexport type ChainTrigger =\n\t| {\n\t\t\ttype: \"stx_transfer\";\n\t\t\tsender?: string;\n\t\t\trecipient?: string;\n\t\t\tminAmount?: ChainTriggerAmount;\n\t\t\tmaxAmount?: ChainTriggerAmount;\n\t }\n\t| { type: \"stx_mint\"; recipient?: string; minAmount?: ChainTriggerAmount }\n\t| { type: \"stx_burn\"; sender?: string; minAmount?: ChainTriggerAmount }\n\t| {\n\t\t\ttype: \"stx_lock\";\n\t\t\tlockedAddress?: string;\n\t\t\tminAmount?: ChainTriggerAmount;\n\t }\n\t| ({\n\t\t\ttype: \"ft_transfer\";\n\t\t\tassetIdentifier?: string;\n\t\t\tsender?: string;\n\t\t\trecipient?: string;\n\t\t\tminAmount?: ChainTriggerAmount;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"ft_mint\";\n\t\t\tassetIdentifier?: string;\n\t\t\trecipient?: string;\n\t\t\tminAmount?: ChainTriggerAmount;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"ft_burn\";\n\t\t\tassetIdentifier?: string;\n\t\t\tsender?: string;\n\t\t\tminAmount?: ChainTriggerAmount;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"nft_transfer\";\n\t\t\tassetIdentifier?: string;\n\t\t\tsender?: string;\n\t\t\trecipient?: string;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"nft_mint\";\n\t\t\tassetIdentifier?: string;\n\t\t\trecipient?: string;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"nft_burn\";\n\t\t\tassetIdentifier?: string;\n\t\t\tsender?: string;\n\t } & TraitScoped)\n\t| ({\n\t\t\ttype: \"contract_call\";\n\t\t\tcontractId?: string;\n\t\t\tfunctionName?: string;\n\t\t\tcaller?: string;\n\t } & TraitScoped)\n\t| { type: \"contract_deploy\"; deployer?: string; contractName?: string }\n\t| ({\n\t\t\ttype: \"print_event\";\n\t\t\tcontractId?: string;\n\t\t\ttopic?: string;\n\t } & TraitScoped);\n\n/** Args for a chain-trigger builder — every field of a variant except `type`. */\ntype TriggerArgs<T extends ChainTrigger[\"type\"]> = Omit<\n\tExtract<ChainTrigger, { type: T }>,\n\t\"type\"\n>;\n\n/**\n * Ergonomic chain-trigger constructors for `subscriptions.create({ triggers })`.\n * Each returns a bare `ChainTrigger` (the wire shape the API expects):\n *\n * ```ts\n * client.subscriptions.create({\n * url: \"https://my.app/webhook\",\n * triggers: [trigger.contractCall({ contractId: \"SP....amm\", functionName: \"swap-*\" })],\n * });\n * ```\n */\nexport const trigger = {\n\tstxTransfer: (f: TriggerArgs<\"stx_transfer\"> = {}): ChainTrigger => ({\n\t\ttype: \"stx_transfer\",\n\t\t...f,\n\t}),\n\tstxMint: (f: TriggerArgs<\"stx_mint\"> = {}): ChainTrigger => ({\n\t\ttype: \"stx_mint\",\n\t\t...f,\n\t}),\n\tstxBurn: (f: TriggerArgs<\"stx_burn\"> = {}): ChainTrigger => ({\n\t\ttype: \"stx_burn\",\n\t\t...f,\n\t}),\n\tstxLock: (f: TriggerArgs<\"stx_lock\"> = {}): ChainTrigger => ({\n\t\ttype: \"stx_lock\",\n\t\t...f,\n\t}),\n\tftTransfer: (f: TriggerArgs<\"ft_transfer\"> = {}): ChainTrigger => ({\n\t\ttype: \"ft_transfer\",\n\t\t...f,\n\t}),\n\tftMint: (f: TriggerArgs<\"ft_mint\"> = {}): ChainTrigger => ({\n\t\ttype: \"ft_mint\",\n\t\t...f,\n\t}),\n\tftBurn: (f: TriggerArgs<\"ft_burn\"> = {}): ChainTrigger => ({\n\t\ttype: \"ft_burn\",\n\t\t...f,\n\t}),\n\tnftTransfer: (f: TriggerArgs<\"nft_transfer\"> = {}): ChainTrigger => ({\n\t\ttype: \"nft_transfer\",\n\t\t...f,\n\t}),\n\tnftMint: (f: TriggerArgs<\"nft_mint\"> = {}): ChainTrigger => ({\n\t\ttype: \"nft_mint\",\n\t\t...f,\n\t}),\n\tnftBurn: (f: TriggerArgs<\"nft_burn\"> = {}): ChainTrigger => ({\n\t\ttype: \"nft_burn\",\n\t\t...f,\n\t}),\n\tcontractCall: (f: TriggerArgs<\"contract_call\"> = {}): ChainTrigger => ({\n\t\ttype: \"contract_call\",\n\t\t...f,\n\t}),\n\tcontractDeploy: (f: TriggerArgs<\"contract_deploy\"> = {}): ChainTrigger => ({\n\t\ttype: \"contract_deploy\",\n\t\t...f,\n\t}),\n\tprintEvent: (f: TriggerArgs<\"print_event\"> = {}): ChainTrigger => ({\n\t\ttype: \"print_event\",\n\t\t...f,\n\t}),\n} as const;\n\nexport interface CreateSubscriptionRequest {\n\tname: string;\n\t/** Subgraph mode. */\n\tsubgraphName?: string;\n\ttableName?: string;\n\tfilter?: SubscriptionFilter;\n\t/** Chain mode. */\n\ttriggers?: ChainTrigger[];\n\turl: string;\n\tformat?: SubscriptionFormat;\n\truntime?: SubscriptionRuntime | null;\n\tauthConfig?: Record<string, unknown>;\n\tmaxRetries?: number;\n\ttimeoutMs?: number;\n\tconcurrency?: number;\n}\n\nexport interface ParsedCreateSubscriptionRequest\n\textends Omit<CreateSubscriptionRequest, \"format\"> {\n\tformat: SubscriptionFormat;\n}\n\nexport interface UpdateSubscriptionRequest {\n\tname?: string;\n\turl?: string;\n\tfilter?: SubscriptionFilter;\n\tformat?: SubscriptionFormat;\n\truntime?: SubscriptionRuntime | null;\n\tauthConfig?: Record<string, unknown>;\n\tmaxRetries?: number;\n\ttimeoutMs?: number;\n\tconcurrency?: number;\n}\n\nexport type ParsedUpdateSubscriptionRequest = UpdateSubscriptionRequest;\n\nexport interface ReplaySubscriptionRequest {\n\tfromBlock: number;\n\ttoBlock: number;\n\tforce?: string;\n}\n\nexport type ParsedReplaySubscriptionRequest = ReplaySubscriptionRequest;\n\nexport interface SubscriptionSummary {\n\tid: string;\n\tname: string;\n\tstatus: SubscriptionStatus;\n\tkind: SubscriptionKind;\n\t/** Null for chain subscriptions. */\n\tsubgraphName: string | null;\n\t/** Null for chain subscriptions. */\n\ttableName: string | null;\n\tformat: SubscriptionFormat;\n\truntime: SubscriptionRuntime | null;\n\turl: string;\n\tlastDeliveryAt: string | null;\n\tlastSuccessAt: string | null;\n\tcreatedAt: string;\n\tupdatedAt: string;\n}\n\nexport interface SubscriptionDetail extends SubscriptionSummary {\n\tfilter: Record<string, unknown>;\n\t/** Chain-trigger filters (chain subscriptions only). */\n\ttriggers: ChainTrigger[] | null;\n\tauthConfig: Record<string, unknown>;\n\tmaxRetries: number;\n\ttimeoutMs: number;\n\tconcurrency: number;\n\tcircuitFailures: number;\n\tcircuitOpenedAt: string | null;\n\tlastError: string | null;\n}\n\nexport interface CreateSubscriptionResponse {\n\tsubscription: SubscriptionDetail;\n\t/** Plaintext signing secret — surfaced ONCE. Store it server-side. */\n\tsigningSecret: string;\n}\n\nexport interface RotateSecretResponse {\n\tsubscription: SubscriptionDetail;\n\tsigningSecret: string;\n}\n\nexport interface DeliveryRow {\n\tid: string;\n\tattempt: number;\n\tstatusCode: number | null;\n\terrorMessage: string | null;\n\tdurationMs: number | null;\n\tresponseBody: string | null;\n\tdispatchedAt: string;\n}\n\nexport interface ReplayResult {\n\treplayId: string;\n\tenqueuedCount: number;\n\tscannedCount: number;\n}\n\nexport interface DeadRow {\n\tid: string;\n\teventType: string;\n\tattempt: number;\n\tblockHeight: number;\n\ttxId: string | null;\n\tpayload: Record<string, unknown>;\n\tfailedAt: string | null;\n\tcreatedAt: string;\n}\n\nexport interface SubscriptionSchemaColumn {\n\ttype?: unknown;\n}\n\nexport interface SubscriptionSchemaTable {\n\tcolumns: Record<string, SubscriptionSchemaColumn>;\n}\n\nexport type SubscriptionSchemaTables = Record<string, SubscriptionSchemaTable>;\n\nconst SCALAR_COLUMN_TYPES = new Set([\n\t\"text\",\n\t\"uint\",\n\t\"int\",\n\t\"principal\",\n\t\"boolean\",\n\t\"timestamp\",\n]);\n\nconst COMPARISON_COLUMN_TYPES = new Set([\"uint\", \"int\", \"timestamp\"]);\n\nfunction formatIssuePath(path: PropertyKey[]): string {\n\treturn path.length > 0 ? `${path.map(String).join(\".\")}: ` : \"\";\n}\n\nexport function formatSubscriptionSchemaErrors(error: z.ZodError): string[] {\n\treturn error.issues.map(\n\t\t(issue) => `${formatIssuePath(issue.path)}${issue.message}`,\n\t);\n}\n\nfunction operatorForClause(clause: SubscriptionFilterClause): string {\n\tif (clause === null || typeof clause !== \"object\" || Array.isArray(clause)) {\n\t\treturn \"eq\";\n\t}\n\treturn Object.keys(clause)[0] ?? \"eq\";\n}\n\nexport function validateSubscriptionFilterForTable(input: {\n\tsubgraphName?: string;\n\ttableName: string;\n\tfilter?: unknown;\n\ttables: SubscriptionSchemaTables;\n}): string[] {\n\tconst errors: string[] = [];\n\tconst table = input.tables[input.tableName];\n\tif (!table) {\n\t\tconst names = Object.keys(input.tables);\n\t\terrors.push(\n\t\t\t`Unknown table \"${input.tableName}\"${\n\t\t\t\tinput.subgraphName ? ` in subgraph \"${input.subgraphName}\"` : \"\"\n\t\t\t}.${names.length > 0 ? ` Available tables: ${names.join(\", \")}.` : \"\"}`,\n\t\t);\n\t\treturn errors;\n\t}\n\n\tif (input.filter === undefined) return errors;\n\n\tconst parsed = SubscriptionFilterSchema.safeParse(input.filter);\n\tif (!parsed.success) {\n\t\treturn formatSubscriptionSchemaErrors(parsed.error);\n\t}\n\n\tfor (const [field, clause] of Object.entries(parsed.data)) {\n\t\tconst column = table.columns[field];\n\t\tif (!column) {\n\t\t\terrors.push(\n\t\t\t\t`Unknown filter field \"${field}\" on table \"${input.tableName}\".`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst columnType =\n\t\t\ttypeof column.type === \"string\" ? column.type.toLowerCase() : \"\";\n\t\tif (!SCALAR_COLUMN_TYPES.has(columnType)) {\n\t\t\terrors.push(\n\t\t\t\t`Filter field \"${field}\" has unsupported type \"${columnType || \"unknown\"}\"; subscription filters require scalar columns.`,\n\t\t\t);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst operator = operatorForClause(clause);\n\t\tif (\n\t\t\t(operator === \"gt\" ||\n\t\t\t\toperator === \"gte\" ||\n\t\t\t\toperator === \"lt\" ||\n\t\t\t\toperator === \"lte\") &&\n\t\t\t!COMPARISON_COLUMN_TYPES.has(columnType)\n\t\t) {\n\t\t\terrors.push(\n\t\t\t\t`Operator \"${operator}\" is not supported for ${columnType} field \"${field}\".`,\n\t\t\t);\n\t\t}\n\t}\n\n\treturn errors;\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAEO,IAAM,uBAAuB;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAEO,IAAM,wBAAwB;AAAA,EACpC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAEO,IAAM,wBAAwB,CAAC,UAAU,UAAU,OAAO;AAE1D,IAAM,gCAAgC;AAAA,EAC5C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AAEA,IAAM,aAAa,EACjB,OAAO,EACP,KAAK,EACL,IAAI,CAAC,EACL,OACA,CAAC,UAAU,MAAM,WAAW,SAAS,KAAK,MAAM,WAAW,UAAU,GACrE,wBACD;AAED,IAAM,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG;AAC7C,IAAM,eAAe,EAAE,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,GAAG;AAE9C,IAAM,2BAA0D,EAAE,KACxE,qBACD;AACO,IAAM,2BACZ,EAAE,KAAK,oBAAoB;AACrB,IAAM,4BAA4D,EAAE,KAC1E,qBACD;AAEO,IAAM,oCACZ,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,QAAQ,CAAC,CAAC;AAEhD,IAAM,mCACZ,EAAE,MA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AM,KAAK,IAAI,OAAO,IACpE;AAAA,IACA,OAAO;AAAA,EACR;AAAA,EAEA,IAAI,MAAM,WAAW;AAAA,IAAW,OAAO;AAAA,EAEvC,MAAM,SAAS,yBAAyB,UAAU,MAAM,MAAM;AAAA,EAC9D,IAAI,CAAC,OAAO,SAAS;AAAA,IACpB,OAAO,+BAA+B,OAAO,KAAK;AAAA,EACnD;AAAA,EAEA,YAAY,OAAO,WAAW,OAAO,QAAQ,OAAO,IAAI,GAAG;AAAA,IAC1D,MAAM,SAAS,MAAM,QAAQ;AAAA,IAC7B,IAAI,CAAC,QAAQ;AAAA,MACZ,OAAO,KACN,yBAAyB,oBAAoB,MAAM,aACpD;AAAA,MACA;AAAA,IACD;AAAA,IAEA,MAAM,aACL,OAAO,OAAO,SAAS,WAAW,OAAO,KAAK,YAAY,IAAI;AAAA,IAC/D,IAAI,CAAC,oBAAoB,IAAI,UAAU,GAAG;AAAA,MACzC,OAAO,KACN,iBAAiB,gCAAgC,cAAc,0DAChE;AAAA,MACA;AAAA,IACD;AAAA,IAEA,MAAM,WAAW,kBAAkB,MAAM;AAAA,IACzC,KACE,aAAa,QACb,aAAa,SACb,aAAa,QACb,aAAa,UACd,CAAC,wBAAwB,IAAI,UAAU,GACtC;AAAA,MACD,OAAO,KACN,aAAa,kCAAkC,qBAAqB,SACrE;AAAA,IACD;AAAA,EACD;AAAA,EAEA,OAAO;AAAA;",
|
|
8
|
+
"debugId": "420686B508CCE30264756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { type Kysely, sql } from "kysely";
|
|
2
|
+
|
|
3
|
+
// Direct chain-level subscriptions ("lambda for Stacks"): let a webhook target a
|
|
4
|
+
// contract / event-type / function-call (or a trait like all SIP-010) WITHOUT
|
|
5
|
+
// deploying a subgraph. A subscription is now polymorphic — `kind='subgraph'`
|
|
6
|
+
// (the existing subgraph_name + table_name + column filter) XOR `kind='chain'`
|
|
7
|
+
// (a `triggers` array of chain filters). A new evaluator loop reads the public
|
|
8
|
+
// Index/Streams clock, matches triggers, and writes to subscription_outbox; the
|
|
9
|
+
// existing emitter delivers both kinds unchanged. `kind` is an explicit
|
|
10
|
+
// discriminator (do NOT infer chain rows from event_type strings).
|
|
11
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
12
|
+
await sql`SET lock_timeout = '30s'`.execute(db);
|
|
13
|
+
|
|
14
|
+
// subscriptions: add the polymorphic columns, relax the subgraph-only NOT
|
|
15
|
+
// NULLs, and enforce exactly-one-mode via CHECK. Existing rows default to
|
|
16
|
+
// kind='subgraph' and already satisfy the constraint.
|
|
17
|
+
await sql`ALTER TABLE subscriptions ADD COLUMN kind TEXT NOT NULL DEFAULT 'subgraph'`.execute(
|
|
18
|
+
db,
|
|
19
|
+
);
|
|
20
|
+
await sql`ALTER TABLE subscriptions ADD COLUMN triggers JSONB`.execute(db);
|
|
21
|
+
await sql`ALTER TABLE subscriptions ALTER COLUMN subgraph_name DROP NOT NULL`.execute(
|
|
22
|
+
db,
|
|
23
|
+
);
|
|
24
|
+
await sql`ALTER TABLE subscriptions ALTER COLUMN table_name DROP NOT NULL`.execute(
|
|
25
|
+
db,
|
|
26
|
+
);
|
|
27
|
+
await sql`
|
|
28
|
+
ALTER TABLE subscriptions ADD CONSTRAINT subscriptions_kind_shape CHECK (
|
|
29
|
+
(kind = 'subgraph' AND subgraph_name IS NOT NULL AND table_name IS NOT NULL AND triggers IS NULL)
|
|
30
|
+
OR
|
|
31
|
+
(kind = 'chain' AND triggers IS NOT NULL AND subgraph_name IS NULL AND table_name IS NULL)
|
|
32
|
+
)
|
|
33
|
+
`.execute(db);
|
|
34
|
+
|
|
35
|
+
// subscription_outbox: chain rows carry neither subgraph_name nor table_name.
|
|
36
|
+
// Add an explicit kind discriminator so reorg/cleanup queries don't depend on
|
|
37
|
+
// event_type string matching.
|
|
38
|
+
await sql`ALTER TABLE subscription_outbox ADD COLUMN kind TEXT NOT NULL DEFAULT 'subgraph'`.execute(
|
|
39
|
+
db,
|
|
40
|
+
);
|
|
41
|
+
await sql`ALTER TABLE subscription_outbox ALTER COLUMN subgraph_name DROP NOT NULL`.execute(
|
|
42
|
+
db,
|
|
43
|
+
);
|
|
44
|
+
await sql`ALTER TABLE subscription_outbox ALTER COLUMN table_name DROP NOT NULL`.execute(
|
|
45
|
+
db,
|
|
46
|
+
);
|
|
47
|
+
// Reorg rollback path snapshots chain outbox rows at/above the fork point.
|
|
48
|
+
await sql`CREATE INDEX subscription_outbox_chain_height_idx ON subscription_outbox (block_height) WHERE kind = 'chain'`.execute(
|
|
49
|
+
db,
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// Single global high-water mark for the trigger evaluator (one loop serves
|
|
53
|
+
// ALL chain subscriptions). The boolean-PK + CHECK enforces exactly one row.
|
|
54
|
+
await sql`
|
|
55
|
+
CREATE TABLE trigger_evaluator_state (
|
|
56
|
+
id BOOLEAN PRIMARY KEY DEFAULT TRUE CHECK (id = TRUE),
|
|
57
|
+
last_processed_block BIGINT NOT NULL DEFAULT 0,
|
|
58
|
+
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
59
|
+
)
|
|
60
|
+
`.execute(db);
|
|
61
|
+
await sql`INSERT INTO trigger_evaluator_state (id) VALUES (TRUE)`.execute(db);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
65
|
+
// Reverting the feature abandons chain subscriptions. Delete them before
|
|
66
|
+
// dropping `kind` so surviving rows are all subgraph-shaped — otherwise a
|
|
67
|
+
// later re-`up` would default them to kind='subgraph' and violate the
|
|
68
|
+
// re-added CHECK (chain rows have null subgraph_name/table_name). Outbox rows
|
|
69
|
+
// cascade via the subscription_id FK.
|
|
70
|
+
await sql`DELETE FROM subscriptions WHERE kind = 'chain'`.execute(db);
|
|
71
|
+
await sql`DROP TABLE IF EXISTS trigger_evaluator_state`.execute(db);
|
|
72
|
+
await sql`DROP INDEX IF EXISTS subscription_outbox_chain_height_idx`.execute(
|
|
73
|
+
db,
|
|
74
|
+
);
|
|
75
|
+
await sql`ALTER TABLE subscription_outbox DROP COLUMN IF EXISTS kind`.execute(
|
|
76
|
+
db,
|
|
77
|
+
);
|
|
78
|
+
await sql`ALTER TABLE subscriptions DROP CONSTRAINT IF EXISTS subscriptions_kind_shape`.execute(
|
|
79
|
+
db,
|
|
80
|
+
);
|
|
81
|
+
await sql`ALTER TABLE subscriptions DROP COLUMN IF EXISTS triggers`.execute(
|
|
82
|
+
db,
|
|
83
|
+
);
|
|
84
|
+
await sql`ALTER TABLE subscriptions DROP COLUMN IF EXISTS kind`.execute(db);
|
|
85
|
+
// Leaves subgraph_name/table_name nullable; existing rows remain valid.
|
|
86
|
+
}
|