@@ -2,28 +2,72 @@ import { execSync } from "child_process"
2
2
import fs from "fs"
3
3
import path from "path"
4
4
5
- import { type ChainIdNetworkResponse } from "@/lib/types"
5
+ import type { Chain , ChainIdNetworkResponse } from "@/lib/types"
6
+
7
+ import {
8
+ CHAINID_NETWORK_ENDPOINT ,
9
+ ETH ,
10
+ EXCLUDED_NAMES ,
11
+ TESTNETS ,
12
+ } from "../lib/constants"
6
13
7
14
/**
8
- * Fetches the chain ID network data from the specified endpoint.
15
+ * Fetches the chain data from the chainid.network endpoint.
9
16
*
10
17
* @returns A promise that resolves to an array of ChainIdNetwork objects.
11
18
*/
12
- export async function fetchChainIdNetworks ( ) : Promise <
19
+ export const fetchChainIdNetworks = async ( ) : Promise <
13
20
ChainIdNetworkResponse [ ]
14
- > {
15
- const response = await fetch ( "https://chainid.network/chains.json" )
16
- const data = await response . json ( )
17
- return data as ChainIdNetworkResponse [ ]
21
+ > => {
22
+ try {
23
+ const response = await fetch ( CHAINID_NETWORK_ENDPOINT )
24
+
25
+ if ( ! response . ok ) throw new Error ( "Failed to fetch chains.json data" )
26
+
27
+ return await response . json ( )
28
+ } catch ( error ) {
29
+ console . error ( ( error as Error ) . message )
30
+ return [ ]
31
+ }
18
32
}
19
33
34
+ const parseChains = async (
35
+ chains : ChainIdNetworkResponse [ ]
36
+ ) : Promise < Chain [ ] > =>
37
+ chains
38
+ . filter ( ( item : ChainIdNetworkResponse ) => {
39
+ const { name, status, chain, nativeCurrency } = item
40
+
41
+ const containsEther = nativeCurrency . name . toLowerCase ( ) . includes ( "ether" )
42
+ const containsETH = nativeCurrency . symbol . includes ( ETH )
43
+ const isEthereum = chain === ETH || containsETH || containsEther
44
+
45
+ const isActive = ! [ "deprecated" , "incubating" ] . includes ( status || "" )
46
+ const isExcluded = [ ...EXCLUDED_NAMES , ...TESTNETS ] . some ( ( word ) =>
47
+ name . toLowerCase ( ) . includes ( word )
48
+ )
49
+
50
+ return isEthereum && isActive && ! isExcluded
51
+ } )
52
+ // Map into simplified Chain object
53
+ . map ( ( { chain, name, infoURL, chainId, nativeCurrency } ) => ( {
54
+ name,
55
+ infoURL,
56
+ chainId,
57
+ nativeCurrency,
58
+ chain,
59
+ } ) )
60
+
20
61
/**
21
62
* Updates the chains data by calling the ChainIdNetworkImport function and
22
63
* writing the result to a TypeScript file.
23
64
*/
24
- export async function updateChainsTsFile ( ) {
65
+ export const updateChainsTsFile = async ( ) => {
25
66
// Get the data from ChainIdNetworkImport
26
- const chains = await fetchChainIdNetworks ( )
67
+ const returnedChains = await fetchChainIdNetworks ( )
68
+
69
+ // Parse the chains data
70
+ const chains = await parseChains ( returnedChains )
27
71
28
72
// Path to the new TypeScript file
29
73
const chainsTsPath = path . join ( process . cwd ( ) , "src/data/chains.ts" )
@@ -35,9 +79,11 @@ export async function updateChainsTsFile() {
35
79
// Write the TypeScript content to the new file
36
80
fs . writeFileSync ( chainsTsPath , tsConst + `\n\n` + tsExport , "utf-8" )
37
81
82
+ // Log results
83
+ console . log ( chains )
38
84
console . log ( `Generated/updated ${ chainsTsPath } ` )
39
85
40
- // Run auto-linter on chainsTsPath
86
+ // Run auto-linter on updated TypeScript file
41
87
execSync ( `npx prettier ${ chainsTsPath } --write` )
42
88
}
43
89
0 commit comments