-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch-contracts.ts
435 lines (374 loc) · 12.8 KB
/
fetch-contracts.ts
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
import * as fs from "fs";
if (!process.env.LIT_ABI_SOURCE) {
throw new Error("❌ LIT_ABI_SOURCE is not defined");
}
if (!process.env.GH_LIT_ASSETS_READ_ONLY_API) {
throw new Error("❌ GH_LIT_ASSETS_READ_ONLY_API is not defined");
}
const DEV_BRANCH = process.env.DEV_BRANCH;
if (!DEV_BRANCH) {
throw new Error("❌ DEV_BRANCH is not defined");
}
type ABISource = {
repoName: string;
path: string;
branch: string;
fileExtensionToRemove: string;
abiSourceInJson: any[];
contractNameMap: any;
deployedContract: {
internalDev: string;
cayenne: string;
manzano: string;
habanero: string;
datilDev: string;
datilTest: string;
datilProd: string;
};
};
const LIT_ABI_SOURCE = {
dev: {
repoName: "lit-assets",
path: "rust/lit-core/lit-blockchain/abis",
branch: DEV_BRANCH,
fileExtensionToRemove: ".json",
abiSourceInJson: ["abi"],
contractNameMap: {
litTokenContractAddress: "LITToken",
pkpNftContractAddress: "PKPNFT",
pkpHelperContractAddress: "PKPHelper",
pkpPermissionsContractAddress: "PKPPermissions",
pkpNftMetadataContractAddress: "PKPNFTMetadata",
pubkeyRouterContractAddress: "PubkeyRouter",
rateLimitNftContractAddress: "RateLimitNFT",
stakingBalancesContractAddress: "StakingBalances",
stakingContractAddress: "Staking",
multisenderContractAddress: "Multisender",
allowlistContractAddress: "Allowlist",
paymentDelegationContractAddress: "PaymentDelegation",
priceFeedContractAddress: "PriceFeed",
cloneNetContractAddress: "CloneNet",
},
deployedContract: {
internalDev:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/internal-dev/deployed-lit-node-contracts-temp.json",
cayenne:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/cayenne/deployed-lit-node-contracts-temp.json",
manzano:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/manzano/deployed-lit-node-contracts-temp.json",
habanero:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/habanero/deployed-lit-node-contracts-temp.json",
datilDev:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-dev/deployed-lit-node-contracts-temp.json",
datilTest:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-test/deployed-lit-node-contracts-temp.json",
datilProd:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-prod/deployed-lit-node-contracts-temp.json",
},
},
prod: {
repoName: "networks",
branch: "main",
path: "abis",
fileExtensionToRemove: ".abi",
abiSourceInJson: [],
contractNameMap: {
litTokenContractAddress: "LITToken",
pkpNftContractAddress: "PKPNFT",
pkpHelperContractAddress: "PKPHelper",
pkpPermissionsContractAddress: "PKPPermissions",
pkpNftMetadataContractAddress: "PKPNFTMetadata",
pubkeyRouterContractAddress: "PubkeyRouter",
rateLimitNftContractAddress: "RateLimitNFT",
stakingBalancesContractAddress: "StakingBalances",
stakingContractAddress: "Staking",
multisenderContractAddress: "Multisender",
allowlistContractAddress: "Allowlist",
paymentDelegationContractAddress: "PaymentDelegation",
cloneNetContractAddress: "CloneNet",
},
deployedContract: {
internalDev:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/internal-dev/deployed-lit-node-contracts-temp.json",
cayenne:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/cayenne/deployed-lit-node-contracts-temp.json",
manzano:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/manzano/deployed-lit-node-contracts-temp.json",
habanero:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/habanero/deployed-lit-node-contracts-temp.json",
datilDev:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-dev/deployed-lit-node-contracts-temp.json",
datilTest:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-test/deployed-lit-node-contracts-temp.json",
datilProd:
"https://raw.githubusercontent.com/LIT-Protocol/networks/main/datil-prod/deployed-lit-node-contracts-temp.json",
},
},
};
type LIT_ABI_SOURCE_TYPES = keyof typeof LIT_ABI_SOURCE;
const SOURCE_BY_ENV: { [key in LIT_ABI_SOURCE_TYPES]: ABISource } = {
dev: LIT_ABI_SOURCE.dev,
prod: LIT_ABI_SOURCE.prod,
};
const source: ABISource =
process.env.LIT_ABI_SOURCE === "dev" ? SOURCE_BY_ENV.dev : SOURCE_BY_ENV.prod;
type LitNetwork =
| "cayenne"
| "internalDev"
| "manzano"
| "habanero"
| "datil-dev"
| "datil-test"
| "datil";
const TOKEN = process.env.GH_LIT_ASSETS_READ_ONLY_API;
const USERNAME = "LIT-Protocol";
const REPO_NAME = source.repoName;
const createPath = (PATH: string, BRANCH: string) => {
return `https://api.github.com/repos/${USERNAME}/${REPO_NAME}/contents/${PATH}?ref=${BRANCH}`;
};
function extractPathAfterMain(urlString: string): string {
const url = new URL(urlString);
const pathname = url.pathname;
const parts = pathname.split("/");
const mainIndex = parts.indexOf("main");
const desiredPath = parts.slice(mainIndex + 1).join("/");
return desiredPath;
}
async function getLastModified(filePath: string, network: string) {
console.log("filePath:", filePath);
const fileAPI = `https://api.github.com/repos/${USERNAME}/networks/commits?path=${filePath}`;
console.log("fileAPI:", fileAPI);
try {
const response = await fetch(fileAPI, HEADER);
const commits: any = await response.json();
if (commits.length > 0) {
return commits[0].commit.author.date;
}
console.error(`[${network}] No commits found for ${filePath}`);
return null;
} catch (error: any) {
console.error(
`[${network}] Error fetching last modified date: ${error.toString()}`
);
}
}
const HEADER = {
headers: {
Authorization: `token ${TOKEN}`,
Accept: "application/vnd.github.v3+json",
},
};
let cache: { [key in LitNetwork]?: any } = {};
async function getLitContractABIs(network: LitNetwork) {
const contractsData = [];
const path = createPath(source.path, source.branch);
console.log("path:", path);
const filesRes = await fetch(path, HEADER);
const files: any = await filesRes.json();
if (files.length <= 0) {
console.log(`[${network}] files length: ${files.length}`);
}
for (const file of files) {
const name = file.name.replace(source.fileExtensionToRemove, "");
console.log("name:", name);
if (!Object.values(source.contractNameMap).includes(name)) {
continue;
}
console.log(`[${network}] Getting file ${file.download_url}`);
const fileRes = await fetch(file.download_url, HEADER);
const fileData: any = await fileRes.json();
const getData = (fileData: any, abiSourceInJson: any[]): any => {
if (abiSourceInJson.length > 0) {
return abiSourceInJson.reduce((acc, key) => acc[key], fileData);
}
return fileData;
};
const data = getData(fileData, source.abiSourceInJson);
if (!data) {
console.log(`[${network}] No data for ${name}`);
}
contractsData.push({
name: file.name.replace(source.fileExtensionToRemove, ""),
contractName: fileData.contractName,
data,
});
}
if (!contractsData.length) {
console.log(`[${network}] No data`);
}
return contractsData;
}
async function updateContractsCache(network: LitNetwork): Promise<void> {
let API: string;
let filePath: string;
let lastModified: string;
switch (network) {
case "cayenne":
filePath = extractPathAfterMain(source.deployedContract.cayenne);
API = source.deployedContract.cayenne;
lastModified = await getLastModified(filePath, network);
break;
case "internalDev":
API = source.deployedContract.internalDev;
filePath = extractPathAfterMain(source.deployedContract.internalDev);
lastModified = await getLastModified(filePath, network);
break;
case "manzano":
filePath = extractPathAfterMain(source.deployedContract.manzano);
API = source.deployedContract.manzano;
lastModified = await getLastModified(filePath, network);
break;
case "habanero":
filePath = extractPathAfterMain(source.deployedContract.habanero);
API = source.deployedContract.habanero;
lastModified = await getLastModified(filePath, network);
break;
case "datil-dev":
filePath = extractPathAfterMain(source.deployedContract.datilDev);
API = source.deployedContract.datilDev;
lastModified = await getLastModified(filePath, network);
break;
case "datil-test":
filePath = extractPathAfterMain(source.deployedContract.datilTest);
API = source.deployedContract.datilTest;
lastModified = await getLastModified(filePath, network);
break;
case "datil":
filePath = extractPathAfterMain(source.deployedContract.datilProd);
API = source.deployedContract.datilProd;
lastModified = await getLastModified(filePath, network);
break;
}
let diamonData: any;
console.log(`[${network}] Trying to get lit contract ABIs`);
try {
diamonData = await getLitContractABIs(network);
console.log(`✅ [${network}] Got diamonData`);
} catch (e: any) {
console.log(
`❌ [${network}] Error getting lit contract ABIs => ${e.toString()}`
);
}
let res: any;
try {
res = await fetch(API);
} catch (e: any) {
console.log(`❌ [${network}] Error fetching API => ${e.toString()}`);
}
let resData: any;
try {
resData = await res.json();
} catch (e: any) {
console.log(`❌ [${network}] Error parsing res.json() => ${e.toString()}`);
}
const data = [];
if (network !== "cayenne") {
cache[network] = {
config: {
chainId: resData?.chainId ?? null,
rpcUrl: resData?.rpcUrl ?? null,
chainName: resData?.chainName ?? null,
litNodeDomainName: resData?.litNodeDomainName ?? null,
litNodePort: resData?.litNodePort ?? null,
rocketPort: resData?.rocketPort ?? null,
},
data: [],
};
} else {
cache[network] = {
data: [],
};
}
for (const [name, address] of Object.entries(resData)) {
const contractFileName = source.contractNameMap[name];
if (contractFileName) {
if (!diamonData) {
console.error(`❗️❗️ [${network}] diamonData is ${diamonData}`);
}
let ABI: any;
try {
ABI = diamonData.find(
(item: { name: string }) => item.name === contractFileName
);
} catch (e: any) {
console.error(
`❗️❗️ [${network}] Error finding contractFileName in diamonData => ${e.toString()}`
);
}
if (!ABI) {
console.log(
`❗️❗️ contractFileName: ${contractFileName} not found in diamonData`
);
}
if (!Object.values(source.contractNameMap).includes(contractFileName)) {
continue;
}
cache[network].data.push({
name: contractFileName,
contracts: [
{
network: network,
address_hash: address,
inserted_at: lastModified,
ABI: ABI?.data ?? [],
},
],
});
} else {
// console.log(`\x1b[33m%s\x1b[0m`, `❗️ "${name}" is not mapped`);
}
}
console.log(`✅ [${network}] Cache Updated`);
// Ensure the directory exists
const dir = `./dist/${process.env.LIT_ABI_SOURCE}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// Write the cache to a file
fs.writeFileSync(
`${dir}/${network}.json`,
JSON.stringify(cache[network], null, 2)
);
// find '-', remove it and capitalize the next letter
let networkName = network
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join("");
// lower case the first letter
networkName = networkName.charAt(0).toLowerCase() + networkName.slice(1);
// also write a .ts file for the cache, like export const networkName
fs.writeFileSync(
`${dir}/${network}.ts`,
`export const ${networkName} = ${JSON.stringify(
cache[network],
null,
2
)} as const`
);
// also write a .js file for the cache, like export const networkName
fs.writeFileSync(
`${dir}/${network}.js`,
`export const ${networkName} = ${JSON.stringify(cache[network], null, 2)}`
);
fs.writeFileSync(
`${dir}/${network}.cjs`,
`"use strict";\n\nmodule.exports = ${JSON.stringify(
cache[network],
null,
2
)};`
);
}
const litNetworks: LitNetwork[] = [
"cayenne",
"internalDev",
"manzano",
"habanero",
"datil-dev",
"datil-test",
"datil",
];
// Initial update for all items
litNetworks.forEach(async (network) => {
await updateContractsCache(network);
});