-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathgenerate-chain-yamls.ts
More file actions
executable file
·401 lines (343 loc) · 14.4 KB
/
Copy pathgenerate-chain-yamls.ts
File metadata and controls
executable file
·401 lines (343 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env node
import fs from "node:fs"
import path from "node:path"
import { fileURLToPath } from "node:url"
import Handlebars from "handlebars"
import { RpcWebSocketClient } from "rpc-websocket-client"
import { Hex, hexToNumber } from "viem"
import { type Configuration, getConfigs, getEnv, getValidChains } from "../src/configs"
const skipRpc = process.argv.includes("--skip-rpc")
const root = process.cwd()
const currentEnv = getEnv()
const validChains = skipRpc
? new Map<string, Configuration>(Object.entries(getConfigs()))
: getValidChains()
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
// Load and compile templates
const templatesDir = path.join(__dirname, "templates")
const partialsDir = path.join(templatesDir, "partials")
// Register partials
Handlebars.registerPartial("handlers", fs.readFileSync(path.join(partialsDir, "handlers.hbs"), "utf8"))
Handlebars.registerPartial("metadata", fs.readFileSync(path.join(partialsDir, "metadata.hbs"), "utf8"))
Handlebars.registerPartial("network-config", fs.readFileSync(path.join(partialsDir, "network-config.hbs"), "utf8"))
// Compile templates
const substrateTemplate = Handlebars.compile(
fs.readFileSync(path.join(templatesDir, "substrate-chain.yaml.hbs"), "utf8"),
)
const evmTemplate = Handlebars.compile(fs.readFileSync(path.join(templatesDir, "evm-chain.yaml.hbs"), "utf8"))
const multichainTemplate = Handlebars.compile(fs.readFileSync(path.join(templatesDir, "multichain.yaml.hbs"), "utf8"))
const EVM_TRACKED = [
// Envrionment Variable Tracked
"COIN_GECKGO_API_KEY",
] as const
const getChainTypesPath = (chain: string) => {
// Extract base chain name before the hyphen
const baseChainName = chain.split("-")[0]
const potentialPath = `./dist/substrate-chaintypes/${baseChainName}.js`
// Check if file exists
if (fs.existsSync(potentialPath)) {
return potentialPath
}
return null
}
const generateEndpoints = (chain: string) => {
const envKey = chain.replace(/-/g, "_").toUpperCase()
// Expect comma-separated endpoints in env var
return process.env[envKey]?.split(",") || []
}
// Generate chain-specific YAML files
const generateSubstrateYaml = async (chain: string, config: Configuration) => {
const chainTypesConfig = getChainTypesPath(chain)
const endpoints = generateEndpoints(chain)
let blockNumber: number
// Only connect to RPC when we actually need the live head (local/nexus-ci).
// For other environments we use the static startBlock from config.
if (skipRpc || (currentEnv !== "local" && currentEnv !== "nexus-ci")) {
blockNumber = config.startBlock
} else {
// Expect comma-separated endpoints in env var
const rpcUrl = process.env[chain.replace(/-/g, "_").toUpperCase()]?.split(",")[0]
const rpc = new RpcWebSocketClient()
await rpc.connect(rpcUrl as string)
const header = (await rpc.call("chain_getHeader", [])) as { number: Hex }
blockNumber = hexToNumber(header.number)
}
// Check if this is a Hyperbridge chain (stateMachineId is KUSAMA-4009 or POLKADOT-3367)
const isHyperbridgeChain = ["KUSAMA-4009", "POLKADOT-3367"].includes(config.stateMachineId)
// Check if price indexing should be enabled (Hyperbridge chain but not testnet)
const enablePriceIndexing = isHyperbridgeChain && currentEnv !== "testnet"
const templateData = {
name: `${chain}-chain`,
description: `${chain.charAt(0).toUpperCase() + chain.slice(1)} Chain Indexer`,
runner: {
node: {
name: "@subql/node",
version: ">=4.0.0",
},
},
config,
endpoints,
chainTypesConfig,
blockNumber,
isHyperbridgeChain,
enablePriceIndexing,
handlerKind: "substrate/EventHandler",
handlers: [
{ handler: "handleIsmpStateMachineUpdatedEvent", module: "ismp", method: "StateMachineUpdated" },
{ handler: "handleSubstrateRequestEvent", module: "ismp", method: "Request" },
{ handler: "handleSubstrateResponseEvent", module: "ismp", method: "Response" },
{ handler: "handleSubstratePostRequestHandledEvent", module: "ismp", method: "PostRequestHandled" },
{
handler: "handleSubstratePostRequestTimeoutHandledEvent",
module: "ismp",
method: "PostRequestTimeoutHandled",
},
{ handler: "handleSubstrateGetRequestHandledEvent", module: "ismp", method: "GetRequestHandled" },
{
handler: "handleSubstrateGetRequestTimeoutHandledEvent",
module: "ismp",
method: "GetRequestTimeoutHandled",
},
],
}
return substrateTemplate(templateData)
}
const generateEvmYaml = async (chain: string, config: Configuration) => {
const endpoints = generateEndpoints(chain)
let blockNumber: number
// Only connect to RPC when we actually need the live head (local env).
// For other environments we use the static startBlock from config.
if (skipRpc || currentEnv !== "local") {
blockNumber = config.startBlock
} else {
// Expect comma-separated endpoints in env var
const rpcUrl = process.env[chain.replace(/-/g, "_").toUpperCase()]?.split(",")[0]
const response = await fetch(rpcUrl as string, {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
},
body: JSON.stringify({
id: 1,
jsonrpc: "2.0",
method: "eth_blockNumber",
}),
})
const data = await response.json()
blockNumber = hexToNumber(data.result)
}
const templateData = {
name: chain,
description: `${chain.charAt(0).toUpperCase() + chain.slice(1)} Indexer`,
runner: {
node: {
name: "@subql/node-ethereum",
version: ">=3.0.0",
},
},
config,
endpoints,
blockNumber,
// Flattened (vault, underlyingToken) pairs so the template can emit one Deposit/Withdraw
// datasource per vault. The handler resolves underlyingToken from YIELD_VAULT_ADDRESSES.
yieldVaults:
config.type === "evm" && config.contracts?.yieldVaults
? Object.entries(config.contracts.yieldVaults).flatMap(([token, entry]) =>
entry.vaults.map((vault) => ({ vault, underlyingToken: token })),
)
: [],
handlerKind: "ethereum/LogHandler",
handlers: [
{ handler: "handleStateMachineUpdatedEvent", topics: ["StateMachineUpdated(string,uint256)"] },
{
handler: "handlePostRequestEvent",
topics: ["PostRequestEvent(string,string,address,bytes,uint256,uint256,bytes,uint256)"],
},
{ handler: "handlePostRequestHandledEvent", topics: ["PostRequestHandled(bytes32,address)"] },
{ handler: "handlePostRequestTimeoutHandledEvent", topics: ["PostRequestTimeoutHandled(bytes32,string)"] },
{
handler: "handleGetRequestEvent",
topics: ["GetRequestEvent(string,string,bytes,bytes[],uint256,uint256,uint256,bytes,uint256)"],
},
{ handler: "handleGetRequestHandledEvent", topics: ["GetRequestHandled(bytes32,address)"] },
{ handler: "handleGetRequestTimeoutHandledEvent", topics: ["GetRequestTimeoutHandled(bytes32,string)"] },
],
}
return evmTemplate(templateData)
}
async function generateAllChainYamls() {
for (const [chain, config] of validChains) {
const yaml =
config.type === "substrate"
? await generateSubstrateYaml(chain, config)
: await generateEvmYaml(chain, config)
fs.writeFileSync(root + `/src/configs/${chain}.yaml`, yaml)
console.log(`Generated ${root}/src/configs/${chain}.yaml`)
}
}
const generateMultichainYaml = () => {
const projects = Array.from(validChains.keys()).map((chain) => `./${chain}.yaml`)
const templateData = {
projects,
}
const yaml = multichainTemplate(templateData)
fs.writeFileSync(root + "/src/configs/subquery-multichain.yaml", yaml)
console.log("Generated subquery-multichain.yaml")
}
const generateChainIdsByGenesis = () => {
const chainIdsByGenesis = {}
validChains.forEach((config) => {
if (config.chainId) {
chainIdsByGenesis[config.chainId] = config.stateMachineId
}
})
const chainIdsByGenesisContent = `// Auto-generated, DO NOT EDIT \nexport const CHAIN_IDS_BY_GENESIS = ${JSON.stringify(chainIdsByGenesis, null, 2)}`
fs.writeFileSync(root + "/src/chain-ids-by-genesis.ts", chainIdsByGenesisContent)
console.log("Generated chain-ids-by-genesis.ts")
}
const generateChainsByIsmpHost = () => {
const chainsByIsmpHost = {}
validChains.forEach((config) => {
// Only include EVM chains with ethereumHost contract
if (config.type === "evm" && config.contracts?.ethereumHost) {
chainsByIsmpHost[config.stateMachineId] = config.contracts.ethereumHost
}
})
const chainsByIsmpHostContent = `// Auto-generated, DO NOT EDIT \nexport const CHAINS_BY_ISMP_HOST = ${JSON.stringify(chainsByIsmpHost, null, 2)}`
fs.writeFileSync(root + "/src/chains-by-ismp-host.ts", chainsByIsmpHostContent)
console.log("Generated chains-by-ismp-host.ts")
}
const generateChainsIntentGatewayV3Addresses = () => {
const intentGatewayV3 = {}
validChains.forEach((config) => {
if (config.type === "evm" && config.contracts?.intentGatewayV3) {
intentGatewayV3[config.stateMachineId] = config.contracts.intentGatewayV3
}
})
const value = `// Auto-generated, DO NOT EDIT \nexport const INTENT_GATEWAY_V3_ADDRESSES = ${JSON.stringify(intentGatewayV3, null, 2)}`
fs.writeFileSync(root + "/src/intent-gateway-v3-addresses.ts", value)
console.log("Generated intent-gateway-v3-addresses.ts")
}
const generateYieldVaultAddresses = () => {
const lines: string[] = []
lines.push("// Auto-generated, DO NOT EDIT")
lines.push("// ERC-4626 vault addresses per chain, keyed by underlying token address (lowercase).")
lines.push("// Aave stata token addresses sourced from https://github.com/bgd-labs/aave-address-book")
lines.push("// Values are arrays because multiple vaults may wrap the same underlying token.")
lines.push(
"// To add or update vaults, edit the \"yieldVaults\" field in the relevant chain entry",
)
lines.push("// in src/configs/config-mainnet.json (or config-testnet.json) and re-run codegen.")
lines.push("export const YIELD_VAULT_ADDRESSES: Record<string, Record<string, string[]>> = {")
let firstChain = true
validChains.forEach((config) => {
if (config.type !== "evm" || !config.contracts?.yieldVaults) return
if (!firstChain) lines.push("")
firstChain = false
lines.push(`\t// ${config.stateMachineId}`)
lines.push(`\t"${config.stateMachineId}": {`)
const entries = Object.entries(config.contracts.yieldVaults)
entries.forEach(([token, entry], i) => {
lines.push(`\t\t// ${entry.description}`)
const vaultsJson = JSON.stringify(entry.vaults)
const comma = i < entries.length - 1 ? "," : ""
lines.push(`\t\t"${token}": ${vaultsJson}${comma}`)
})
lines.push("\t},")
})
lines.push("}")
lines.push("")
fs.writeFileSync(root + "/src/yield-vault-addresses.ts", lines.join("\n"))
console.log("Generated yield-vault-addresses.ts")
}
const generateTokenSlotOverrides = () => {
const lines: string[] = []
lines.push("// Auto-generated, DO NOT EDIT")
lines.push("// Per-token ERC-20 storage slot overrides for tokens that don't follow the standard OZ layout")
lines.push("// (slot 0 = _balances, slot 1 = _allowances).")
lines.push("// To add or update entries, edit the \"tokenSlots\" field in the relevant chain entry")
lines.push("// in src/configs/config-mainnet.json (or config-testnet.json) and re-run codegen.")
lines.push(
"export const TOKEN_SLOT_OVERRIDES: Record<string, { balanceSlot: bigint; allowanceSlot: bigint }> = {",
)
const seen = new Map<string, { description: string; balanceSlot: number; allowanceSlot: number }>()
validChains.forEach((config) => {
if (config.type !== "evm" || !config.contracts?.tokenSlots) return
Object.entries(config.contracts.tokenSlots).forEach(([token, entry]) => {
seen.set(token.toLowerCase(), entry)
})
})
const entries = Array.from(seen.entries())
entries.forEach(([token, entry], i) => {
lines.push(`\t// ${entry.description}`)
const comma = i < entries.length - 1 ? "," : ""
lines.push(
`\t"${token}": { balanceSlot: ${entry.balanceSlot}n, allowanceSlot: ${entry.allowanceSlot}n }${comma}`,
)
})
lines.push("}")
lines.push("")
fs.writeFileSync(root + "/src/token-slot-overrides.ts", lines.join("\n"))
console.log("Generated token-slot-overrides.ts")
}
const generateTestnetStateMachineIds = () => {
const testnetStateMachineIds = new Set()
// Only generate testnet state machine IDs when in testnet environment
if (currentEnv === "testnet") {
validChains.forEach((config) => {
testnetStateMachineIds.add(config.stateMachineId)
})
}
const value = `// Auto-generated, DO NOT EDIT \nexport const TESTNET_STATE_MACHINE_IDS: string[] = ${JSON.stringify(Array.from(testnetStateMachineIds), null, 2)}`
fs.writeFileSync(root + "/src/testnet-state-machine-ids.ts", value)
console.log("Generated testnet-state-machine-ids.ts")
}
const generateEnvironmentConfig = () => {
const configurations = {}
// Set evm and substrate environment configurations
validChains.forEach((config, chain) => {
const envKey = chain.replace(/-/g, "_").toUpperCase()
const endpoints = process.env[envKey]?.split(",") || []
if (endpoints.length > 0) {
configurations[config.stateMachineId] = endpoints[0].trim()
}
})
EVM_TRACKED.forEach((e: string) => (configurations[e] = process.env?.[e] ?? null))
fs.writeFileSync(root + "/src/env-config.json", JSON.stringify(configurations, null, 2))
console.log("Generated env-config.json")
// If a built dist bundle exists, patch the inlined env-config so the running
// indexer picks up the updated RPC endpoints and API keys without rebuilding.
const distBundle = path.join(root, "dist", "index.js")
if (fs.existsSync(distBundle)) {
const bundle = fs.readFileSync(distBundle, "utf8")
// The bundler inlines env-config.json as JSON.parse('{...}'). Anchor on
// the COIN_GECKGO_API_KEY marker which is always present in the config.
const envConfigPattern = /JSON\.parse\('(\{[^']*COIN_GECKGO_API_KEY[^']*\})'\)/
if (!envConfigPattern.test(bundle)) {
console.warn("Could not find inlined env-config in dist/index.js; skipping patch")
return
}
// Escape any single quotes and backslashes so the replacement is a valid JS string literal
const serialized = JSON.stringify(configurations).replace(/\\/g, "\\\\").replace(/'/g, "\\'")
const patched = bundle.replace(envConfigPattern, `JSON.parse('${serialized}')`)
fs.writeFileSync(distBundle, patched)
console.log("Patched env-config in dist/index.js")
}
}
try {
await generateAllChainYamls()
generateMultichainYaml()
generateChainIdsByGenesis()
generateChainsByIsmpHost()
generateChainsIntentGatewayV3Addresses()
generateYieldVaultAddresses()
generateTokenSlotOverrides()
generateTestnetStateMachineIds()
generateEnvironmentConfig()
process.exit(0)
} catch (err) {
console.error("Error generating YAMLs:", err)
process.exit(1)
}