-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathrefresh-data.ts
More file actions
291 lines (257 loc) · 7.15 KB
/
refresh-data.ts
File metadata and controls
291 lines (257 loc) · 7.15 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
import { readFileSync, writeFileSync } from "fs";
import https from "https";
import { dirname, join } from "path";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const readJsonFile = (path: string) => {
return JSON.parse(readFileSync(join(__dirname, path), "utf8"));
};
const OVERRIDE_NETWORKS: Network[] = readJsonFile("./override-networks.json");
const OVERRIDE_CHAIN_NAMES = readJsonFile("./override-chain-names.json");
const OVERRIDE_RPCS_BY_CHAIN_ID = readJsonFile("./override-rpc.json");
async function fetchAndSaveChains() {
try {
console.log("🐛 Fetching chains from API...");
const response = await new Promise((resolve, reject) => {
https.get("https://chainid.network/chains.json", (res: any) => {
let data = "";
res.on("data", (chunk: any) => {
data += chunk;
});
res.on("end", () => {
resolve(data);
});
res.on("error", (err: any) => {
reject(err);
});
});
});
return JSON.parse(response as string);
} catch (error) {
console.error("Error fetching data:", error);
return [];
}
}
export type Network = {
name: string;
chain: string;
icon?: string;
slug: string;
rpc: string[];
features?: {
name: string;
}[];
faucets?: string[];
nativeCurrency?: {
name: string;
symbol: string;
decimals: number;
};
infoURL?: string;
shortName?: string;
chainId: number | null;
networkId: number | null;
slip44?: number;
ens?: {
registry: string;
};
explorers?: {
name?: string;
url: string;
standard?: string;
}[];
status?: string;
};
export type Chain = {
chain: string;
icon?: string;
networks: Network[];
};
const getFinalRpcs = (network: Network) => {
const chainId = network.chainId;
const rpcs = network.rpc;
if (chainId && chainId in OVERRIDE_RPCS_BY_CHAIN_ID) {
const newRpcs = new Set([
...OVERRIDE_RPCS_BY_CHAIN_ID[
chainId as unknown as keyof typeof OVERRIDE_RPCS_BY_CHAIN_ID
],
...rpcs,
]);
return Array.from(newRpcs);
}
return network.rpc;
};
const OVERRIDE_NETWORK_NAMES = {
Ropsten: "Ethereum Ropsten Testnet",
Rinkeby: "Ethereum Rinkeby Testnet",
Goerli: "Ethereum Goerli Testnet",
Holesky: "Ethereum Holesky Testnet",
};
const getChainName = (chain: string, network: string) => {
const chainName =
OVERRIDE_CHAIN_NAMES[chain as keyof typeof OVERRIDE_CHAIN_NAMES] || chain;
if (chainName === "Ethereum") {
const override = getEthChainOverrideByNetwork(network);
if (override) {
return override;
}
} else if ((chainName as string) === "NEAR") {
if (!network.startsWith("NEAR")) {
// Assume that the correct chain name is the first word of the network name
return network.split(" ")[0] || network;
}
} else if ((chainName as string) === "Polygon") {
if (network.startsWith("Polygon zkEVM")) {
return "Polygon zkEVM";
}
}
return chainName;
};
function getEthChainOverrideByNetwork(network: string) {
if (network.startsWith("Ethereum")) {
return "Ethereum";
}
if (network.startsWith("OP ") || network.startsWith("Optimism ")) {
return "Optimism";
}
if (network.startsWith("Cycle Network")) {
return "Cycle Network";
}
if (network.startsWith("Molereum Network")) {
return "Molereum Network";
}
if (network.startsWith("GRVT Exchange")) {
return "GRVT Exchange";
}
if (network.startsWith("Proof of Play")) {
return "Proof of Play";
}
if (network.startsWith("Zytron Linea")) {
return "Zytron Linea";
}
if (network.startsWith("Elastos Smart Chain")) {
return "Elastos Smart Chain";
}
if (network in OVERRIDE_ETH_CHAIN_NAMES_BY_NETWORK) {
return OVERRIDE_ETH_CHAIN_NAMES_BY_NETWORK[
network as keyof typeof OVERRIDE_ETH_CHAIN_NAMES_BY_NETWORK
];
}
return network.split(" ")[0] || network;
}
const OVERRIDE_ETH_CHAIN_NAMES_BY_NETWORK = {
"AB Core Testnet": "AB Core",
"Anytype EVM Chain": "Anytype EVM Chain",
"Beverly Hills": "Beverly Hills",
};
const EXCLUDE_CHAINS_FROM_API = ["ARC"];
const processNetworks = async () => {
const networksFromApi = await fetchAndSaveChains();
if (!networksFromApi.length) {
console.error("❌ No chains fetched from API");
return [];
}
console.log("🦋 Chains fetched from API, starting processing...");
const NETWORKS_FROM_API: Network[] = networksFromApi
.filter((network: Network) => {
if (
!network.rpc?.length ||
network.status === "deprecated" ||
network.name.match(/deprecated/i) ||
network.name.match(/discard/i) ||
EXCLUDE_CHAINS_FROM_API.includes(network.chain)
) {
return false;
}
return true;
})
.map((network: Network) => ({
...network,
slug: network.name.toLowerCase().replace(/\s+/g, "-"),
rpc: network.rpc.filter((rpc: string) => rpc.startsWith("https")),
}));
const CHAINS_FROM_API: Chain[] = Object.values(
NETWORKS_FROM_API.reduce(
(acc, network) => {
const networkName =
OVERRIDE_NETWORK_NAMES[
network.name as keyof typeof OVERRIDE_NETWORK_NAMES
] || network.name;
const chain = getChainName(network.chain, networkName);
const existingChain = acc[chain] as Chain | undefined;
const explorers = network.explorers?.filter((explorer) =>
explorer.url.startsWith("https")
);
if (existingChain) {
return {
...acc,
[chain]: {
...existingChain,
networks: [
...existingChain.networks,
{
...network,
chain,
name: networkName,
rpc: getFinalRpcs(network),
explorers,
},
],
},
};
}
return {
...acc,
[chain]: {
chain,
icon: network.icon,
networks: [
{
...network,
name: networkName,
chain,
rpc: getFinalRpcs(network),
explorers,
},
],
},
};
},
{} as Record<string, Chain>
)
);
const OVERRIDEN_CHAINS_BY_NAME = OVERRIDE_NETWORKS.reduce(
(acc, overridenNetwork) => {
const chain = overridenNetwork.chain;
if (chain in acc) {
acc[chain].networks.push({
...overridenNetwork,
});
return acc;
}
acc[chain] = {
chain,
networks: [overridenNetwork],
};
return acc;
},
{} as Record<string, Chain>
);
const OVERRIDEN_CHAINS = Object.values(OVERRIDEN_CHAINS_BY_NAME);
return [...CHAINS_FROM_API, ...OVERRIDEN_CHAINS].sort((a, b) =>
a.chain.localeCompare(b.chain)
);
};
(async () => {
const chains = await processNetworks();
if (!chains.length) {
return;
}
console.log(`🤟 Chains: ${chains.length}`);
// write chains to file
writeFileSync(
join(__dirname, "./chains.generated.json"),
JSON.stringify(chains, null, 2)
);
})();