Skip to content

Commit 05a70cc

Browse files
committed
load tokenlists dynamically
1 parent 495ca6d commit 05a70cc

11 files changed

+1948
-2074
lines changed

src/common/utils/tokenList.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1+
import fs from "node:fs"
12
import type { Address } from "viem"
2-
import tokenList1 from "../../tokenLists/tokenList_1"
3-
import tokenList146 from "../../tokenLists/tokenList_146"
4-
import tokenList1923 from "../../tokenLists/tokenList_1923"
5-
import tokenList8453 from "../../tokenLists/tokenList_8543"
6-
import tokenList80094 from "../../tokenLists/tokenList_80094"
73

84
export type TokenListItem = {
95
addressInfo: Address
@@ -19,13 +15,19 @@ export type TokenListItem = {
1915
}
2016
}
2117

22-
const cache: Record<number, TokenListItem[]> = {
23-
1: tokenList1 as TokenListItem[],
24-
8453: tokenList8453 as TokenListItem[],
25-
1923: tokenList1923 as TokenListItem[],
26-
146: tokenList146 as TokenListItem[],
27-
80094: tokenList80094 as TokenListItem[],
28-
}
18+
const cache: Record<number, TokenListItem[]> = {}
19+
;(function buildCache() {
20+
const dir = `${__dirname}/../../tokenLists`
21+
const files = fs.readdirSync(dir)
22+
for (const file of files) {
23+
const match = file.match(/(\d+)/g)
24+
if (!match) throw new Error("Invalid tokenlist file")
25+
const chainId = Number(match[0])
26+
cache[chainId] = JSON.parse(
27+
fs.readFileSync(`${dir}/${file}`).toString(),
28+
) as TokenListItem[]
29+
}
30+
})()
2931

3032
export default function getTokenList(chainId: number): TokenListItem[] {
3133
return cache[chainId] || []

0 commit comments

Comments
 (0)