1
+ import fs from "node:fs"
1
2
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"
7
3
8
4
export type TokenListItem = {
9
5
addressInfo : Address
@@ -19,13 +15,19 @@ export type TokenListItem = {
19
15
}
20
16
}
21
17
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
+ } ) ( )
29
31
30
32
export default function getTokenList ( chainId : number ) : TokenListItem [ ] {
31
33
return cache [ chainId ] || [ ]
0 commit comments