forked from euler-xyz/euler-labels
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexagate-sync.js
More file actions
490 lines (454 loc) · 13.2 KB
/
Copy pathhexagate-sync.js
File metadata and controls
490 lines (454 loc) · 13.2 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
const fs = require("node:fs");
const API_BASE = "https://api.hexagate.com";
const GOV_VAULT_TAG_NAME = "GOV-Vault";
const TAG_KIND_ADDRESS = 1;
const ENTITY_TYPE_CONTRACT = 1;
const ENTITY_PARAM_TYPE = 1;
const TAG_PAGE_SIZE = 300;
const ENTITY_RESOLVE_PAGE_SIZE = 200;
const ADD_BATCH_SIZE = 50;
const DELETE_BATCH_SIZE = 100;
const REQUEST_TIMEOUT_MS = 30_000;
const REQUEST_MAX_RETRIES = 2;
const REQUEST_RETRY_BASE_MS = 1_000;
main().catch((err) => {
process.stderr.write(`ERROR: ${err.stack || err.message || err}\n`);
process.exit(1);
});
async function main() {
const args = parseArgs(process.argv.slice(2));
const apiKey = process.env.HEXAGATE_API_KEY;
if (!apiKey) throw Error("HEXAGATE_API_KEY env var is required");
const http = createClient(apiKey);
log("Fetching Hexagate chain registry…");
const supportedChains = await fetchSupportedChains(http);
log(`Found ${supportedChains.size} monitorable chains`);
log("Fetching kind=1 tag inventory…");
const allTags = await fetchAllAddressTags(http);
const byName = new Map(allTags.map((t) => [t.name, t]));
const govVault = byName.get(GOV_VAULT_TAG_NAME);
if (!govVault) {
throw Error(
`'${GOV_VAULT_TAG_NAME}' tag not found in Hexagate; refusing to proceed`,
);
}
log(`Found GOV-Vault tag id=${govVault.id}`);
log("Fetching current GOV-Vault membership…");
const govSnapshot = await http.get(
`/api/v1/monitoring/monitor_tags/${govVault.id}`,
);
const govByChain = normalizeEntitiesByChain(govSnapshot.entities_by_chain);
const chainDirs = fs
.readdirSync(".")
.filter((d) => /^\d+$/.test(d))
.map((d) => Number(d))
.filter((d) => !args.only || args.only.has(d))
.sort((a, b) => a - b);
if (args.only) {
const missing = [...args.only].filter((c) => !chainDirs.includes(c));
if (missing.length > 0) {
throw Error(
`--only references chains with no labels directory: ${missing.join(", ")}`,
);
}
}
const plan = [];
for (const chainId of chainDirs) {
const desired = readDesiredVaults(chainId);
const current = govByChain.get(chainId) ?? new Set();
if (!supportedChains.has(chainId)) {
plan.push({
chainId,
skipped: "chain not supported by Hexagate (not monitorable)",
desired,
current,
toAdd: new Set(),
toRemove: current,
chainTag: null,
});
continue;
}
const chainTag = discoverChainTag(allTags, chainId);
if (!chainTag) {
plan.push({
chainId,
skipped:
"no per-chain tag in Hexagate (no monitor configured for this chain)",
desired,
current,
toAdd: new Set(),
toRemove: new Set(),
chainTag: null,
});
continue;
}
const toAdd = setDiff(desired, current);
const toRemove = setDiff(current, desired);
plan.push({
chainId,
skipped: null,
desired,
current,
toAdd,
toRemove,
chainTag,
});
}
if (args.mode === "apply") {
for (const entry of plan) {
if (entry.toAdd.size > 0) {
await applyAdds(http, entry, govVault);
}
if (entry.toRemove.size > 0) {
await applyRemoves(http, entry);
}
}
}
process.stdout.write(renderSummary(plan, args.mode));
}
function parseArgs(argv) {
let mode = null;
let only = null;
for (let i = 0; i < argv.length; i++) {
const a = argv[i];
if (a === "--dry-run" || a === "--apply") {
if (mode) throw Error("specify exactly one of --dry-run or --apply");
mode = a === "--dry-run" ? "dry-run" : "apply";
} else if (a === "--only") {
const next = argv[++i];
if (!next) throw Error("--only requires a comma-separated chainId list");
only = new Set(
next.split(",").map((s) => {
const n = Number(s.trim());
if (!Number.isInteger(n) || n <= 0) {
throw Error(`--only: invalid chainId: ${s}`);
}
return n;
}),
);
} else if (a.startsWith("--only=")) {
only = new Set(
a
.slice("--only=".length)
.split(",")
.map((s) => {
const n = Number(s.trim());
if (!Number.isInteger(n) || n <= 0) {
throw Error(`--only: invalid chainId: ${s}`);
}
return n;
}),
);
} else {
throw Error(`unknown argument: ${a}`);
}
}
if (!mode) throw Error("specify exactly one of --dry-run or --apply");
return { mode, only };
}
function createClient(apiKey) {
const headers = {
"X-Hexagate-Api-Key": apiKey,
Accept: "application/json",
};
async function request(method, path, { query, body } = {}) {
const url = new URL(API_BASE + path);
if (query) {
for (const [k, v] of Object.entries(query)) {
if (v === undefined || v === null) continue;
if (Array.isArray(v)) {
for (const item of v) url.searchParams.append(k, String(item));
} else {
url.searchParams.set(k, String(v));
}
}
}
const init = { method, headers: { ...headers } };
if (body !== undefined) {
init.headers["Content-Type"] = "application/json";
init.body = JSON.stringify(body);
}
let lastErr = null;
for (let attempt = 0; attempt <= REQUEST_MAX_RETRIES; attempt++) {
try {
const res = await fetch(url, {
...init,
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
if (res.status >= 500 && attempt < REQUEST_MAX_RETRIES) {
const text = await res.text();
lastErr = Error(`${method} ${path} → HTTP ${res.status}: ${text}`);
log(
`retry ${attempt + 1}/${REQUEST_MAX_RETRIES}: ${method} ${path} (HTTP ${res.status})`,
);
await sleep(REQUEST_RETRY_BASE_MS * 2 ** attempt);
continue;
}
if (!res.ok) {
const text = await res.text();
throw Error(`${method} ${path} → HTTP ${res.status}: ${text}`);
}
if (res.status === 204) return null;
const ct = res.headers.get("content-type") || "";
return ct.includes("application/json") ? res.json() : res.text();
} catch (err) {
if (attempt < REQUEST_MAX_RETRIES && isTransient(err)) {
lastErr = err;
log(
`retry ${attempt + 1}/${REQUEST_MAX_RETRIES}: ${method} ${path} (${err.message || err})`,
);
await sleep(REQUEST_RETRY_BASE_MS * 2 ** attempt);
continue;
}
throw err;
}
}
throw lastErr;
}
return {
get: (path, query) => request("GET", path, { query }),
post: (path, body, query) => request("POST", path, { body, query }),
delete: (path, query) => request("DELETE", path, { query }),
};
}
async function fetchSupportedChains(http) {
const res = await http.get("/api/v1/chains/");
const out = new Set();
for (const [k, info] of Object.entries(res ?? {})) {
const cid = Number(k);
if (!Number.isFinite(cid) || cid <= 0) continue;
if (info?.chain_type !== "evm") continue;
if (!info?.concord_support) continue;
if (!info?.included_in_plan) continue;
if (info?.is_testnet) continue;
out.add(cid);
}
return out;
}
async function fetchAllAddressTags(http) {
const items = [];
for (let page = 1; ; page++) {
const res = await http.get("/api/v1/monitoring/monitor_tags/", {
page,
page_size: TAG_PAGE_SIZE,
kind: TAG_KIND_ADDRESS,
});
const got = res.items ?? [];
items.push(...got);
if (got.length < TAG_PAGE_SIZE) break;
}
return items;
}
function normalizeEntitiesByChain(ebc) {
const out = new Map();
for (const [k, addrs] of Object.entries(ebc ?? {})) {
out.set(Number(k), new Set(addrs.map((a) => a.toLowerCase())));
}
return out;
}
function discoverChainTag(allTags, chainId) {
const candidates = [];
for (const t of allTags) {
const ebc = t.entities_by_chain ?? {};
const keys = Object.keys(ebc).map(Number);
if (keys.length !== 1 || keys[0] !== chainId) continue;
candidates.push({ tag: t, n: ebc[String(chainId)].length });
}
if (candidates.length === 0) return null;
candidates.sort((a, b) => b.n - a.n);
if (candidates.length > 1) {
const others = candidates
.slice(1)
.map((c) => `${c.tag.name}=${c.n}`)
.join(", ");
log(
`chain ${chainId}: ${candidates.length} candidate per-chain tags, picking '${candidates[0].tag.name}' (${candidates[0].n}) over [${others}]`,
);
}
return candidates[0].tag;
}
function readDesiredVaults(chainId) {
const products = JSON.parse(
fs.readFileSync(`${chainId}/products.json`).toString(),
);
const out = new Set();
for (const product of Object.values(products)) {
for (const addr of product.vaults ?? []) out.add(addr.toLowerCase());
for (const addr of product.deprecatedVaults ?? [])
out.add(addr.toLowerCase());
}
return out;
}
function setDiff(a, b) {
const out = new Set();
for (const x of a) if (!b.has(x)) out.add(x);
return out;
}
async function applyAdds(http, entry, govVault) {
const addrs = [...entry.toAdd];
log(`chain ${entry.chainId}: adding ${addrs.length} entities…`);
const govTag = {
id: govVault.id,
name: govVault.name,
kind: TAG_KIND_ADDRESS,
};
const chainTag = {
id: entry.chainTag.id,
name: entry.chainTag.name,
kind: TAG_KIND_ADDRESS,
};
const existing = await fetchEntities(http, entry.chainId, addrs);
for (let i = 0; i < addrs.length; i += ADD_BATCH_SIZE) {
const slice = addrs.slice(i, i + ADD_BATCH_SIZE);
const body = [];
for (const address of slice) {
const ex = existing.get(address);
if (ex) {
const existingTags = (ex.monitor_tags ?? []).map((t) => ({
id: t.id,
name: t.name,
kind: t.kind,
}));
const ids = new Set(existingTags.map((t) => t.id));
const merged = [...existingTags];
if (!ids.has(govTag.id)) merged.push(govTag);
if (!ids.has(chainTag.id)) merged.push(chainTag);
if (merged.length === existingTags.length) continue;
body.push({
id: ex.id,
monitor_tags: merged,
monitor_entity: {
entity_type: ENTITY_TYPE_CONTRACT,
params: {
type: ENTITY_PARAM_TYPE,
address,
chain_id: entry.chainId,
},
},
});
} else {
body.push({
monitor_tags: [govTag, chainTag],
monitor_entity: {
entity_type: ENTITY_TYPE_CONTRACT,
params: {
type: ENTITY_PARAM_TYPE,
address,
chain_id: entry.chainId,
},
},
});
}
}
if (body.length === 0) continue;
await http.post("/api/v1/monitoring/entities_metadata/batch", body);
}
}
async function applyRemoves(http, entry) {
const addrs = [...entry.toRemove];
log(`chain ${entry.chainId}: removing ${addrs.length} entities…`);
const existing = await fetchEntities(http, entry.chainId, addrs);
const ids = [...existing.values()].map((e) => e.id);
for (let i = 0; i < ids.length; i += DELETE_BATCH_SIZE) {
const slice = ids.slice(i, i + DELETE_BATCH_SIZE);
await http.delete("/api/v1/monitoring/entities_metadata/batch", {
ids: slice,
});
}
}
async function fetchEntities(http, chainId, addresses) {
const out = new Map();
for (let i = 0; i < addresses.length; i += ENTITY_RESOLVE_PAGE_SIZE) {
const slice = addresses.slice(i, i + ENTITY_RESOLVE_PAGE_SIZE);
let page = 1;
for (;;) {
const res = await http.get("/api/v1/monitoring/entities_metadata/", {
page,
page_size: ENTITY_RESOLVE_PAGE_SIZE,
chains: [chainId],
addresses: slice,
});
const got = res.items ?? [];
for (const it of got) {
const addr = it.monitor_entity?.params?.address?.toLowerCase();
if (addr) out.set(addr, it);
}
if (got.length < ENTITY_RESOLVE_PAGE_SIZE) break;
page += 1;
}
}
return out;
}
function renderSummary(plan, mode) {
const lines = [];
lines.push(`# Hexagate sync — ${mode === "apply" ? "applied" : "dry-run"}`);
lines.push("");
lines.push("| chain | desired | hexagate | +adds | -removes | status |");
lines.push("|---|---:|---:|---:|---:|---|");
let totalAdd = 0;
let totalRemove = 0;
for (const e of plan) {
const status = e.skipped ? `skipped: ${e.skipped}` : "ok";
lines.push(
`| ${e.chainId} | ${e.desired.size} | ${e.current.size} | ${e.toAdd.size} | ${e.toRemove.size} | ${status} |`,
);
totalAdd += e.toAdd.size;
totalRemove += e.toRemove.size;
}
lines.push(`| **total** | | | **${totalAdd}** | **${totalRemove}** | |`);
lines.push("");
for (const e of plan) {
if (e.toAdd.size === 0 && e.toRemove.size === 0) continue;
const heading = e.skipped
? `chain ${e.chainId}: cleanup -${e.toRemove.size} (${e.skipped})`
: `chain ${e.chainId}: +${e.toAdd.size} / -${e.toRemove.size}`;
lines.push(`<details><summary>${heading}</summary>`);
lines.push("");
if (e.toAdd.size > 0) {
lines.push("**Add:**");
lines.push("");
for (const a of [...e.toAdd].sort()) lines.push(`- \`${a}\``);
lines.push("");
}
if (e.toRemove.size > 0) {
lines.push("**Remove:**");
lines.push("");
for (const a of [...e.toRemove].sort()) lines.push(`- \`${a}\``);
lines.push("");
}
lines.push("</details>");
lines.push("");
}
const skipped = plan.filter((e) => e.skipped);
if (skipped.length > 0) {
lines.push("**Skipped chains (no sync):**");
lines.push("");
for (const e of skipped) {
const cleanup =
e.toRemove.size > 0
? ` — cleaning up ${e.toRemove.size} stale entry(ies)`
: "";
lines.push(`- \`${e.chainId}\` — ${e.skipped}${cleanup}`);
}
lines.push("");
}
return lines.join("\n");
}
function log(msg) {
process.stderr.write(`${msg}\n`);
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
function isTransient(err) {
if (!err) return false;
if (err.name === "TimeoutError" || err.name === "AbortError") return true;
const code = err.cause?.code ?? err.code;
return (
code === "ECONNRESET" ||
code === "ETIMEDOUT" ||
code === "ECONNREFUSED" ||
code === "EAI_AGAIN" ||
code === "ENOTFOUND"
);
}