Skip to content

Commit 41d6168

Browse files
committed
feat: parse/simplify data before saving chains.ts
1 parent 0f96193 commit 41d6168

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

src/lib/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,11 @@ export type ChainIdNetworkResponse = {
619619
}
620620
}
621621

622+
export type Chain = Pick<
623+
ChainIdNetworkResponse,
624+
"name" | "infoURL" | "chainId" | "nativeCurrency" | "chain"
625+
>
626+
622627
export type ChainName = (typeof chains)[number]["name"]
623628

624629
// Wallets

src/scripts/update-chains.ts

Lines changed: 56 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,72 @@ import { execSync } from "child_process"
22
import fs from "fs"
33
import path from "path"
44

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"
613

714
/**
8-
* Fetches the chain ID network data from the specified endpoint.
15+
* Fetches the chain data from the chainid.network endpoint.
916
*
1017
* @returns A promise that resolves to an array of ChainIdNetwork objects.
1118
*/
12-
export async function fetchChainIdNetworks(): Promise<
19+
export const fetchChainIdNetworks = async (): Promise<
1320
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+
}
1832
}
1933

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+
2061
/**
2162
* Updates the chains data by calling the ChainIdNetworkImport function and
2263
* writing the result to a TypeScript file.
2364
*/
24-
export async function updateChainsTsFile() {
65+
export const updateChainsTsFile = async () => {
2566
// 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)
2771

2872
// Path to the new TypeScript file
2973
const chainsTsPath = path.join(process.cwd(), "src/data/chains.ts")
@@ -35,9 +79,11 @@ export async function updateChainsTsFile() {
3579
// Write the TypeScript content to the new file
3680
fs.writeFileSync(chainsTsPath, tsConst + `\n\n` + tsExport, "utf-8")
3781

82+
// Log results
83+
console.log(chains)
3884
console.log(`Generated/updated ${chainsTsPath}`)
3985

40-
// Run auto-linter on chainsTsPath
86+
// Run auto-linter on updated TypeScript file
4187
execSync(`npx prettier ${chainsTsPath} --write`)
4288
}
4389

0 commit comments

Comments
 (0)