-
Notifications
You must be signed in to change notification settings - Fork 18
/
generate.task.js
132 lines (120 loc) · 3.9 KB
/
generate.task.js
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
const {
Generator,
ProviderCoinGecko,
ProviderLegacyToken,
ProviderTrusted,
ProviderJupiterTokenList,
ProviderIgnore,
Tag,
ChainId,
} = require('@solflare-wallet/utl-aggregator')
const { CronJob } = require('cron')
const fs = require('fs')
async function handle(fileName = null) {
console.log(`${name} | start ${inProgress} | ${new Date().toISOString()}`)
const jupiterTokenListUrl = process.env.JUPITER_TOKEN_LIST_URL ?? "https://tokens.jup.ag/tokens"
const trustedTokenList = process.env.TRUSTED_TOKEN_LIST_URL ?? null
const ignoreTokenList = process.env.IGNORE_TOKEN_LIST_URL ?? null
const coinGeckoApiKey = process.env.COINGECKO_API_KEY ?? null
const rpcUrlMainnet = process.env.RPC_URL_MAINNET
const rpcUrlDevnet = process.env.RPC_URL_DEVNET
const baseTokenListCdnUrl = process.env.TOKEN_LIST_CDN_URL
// Can be used to clear cache
// ProviderLegacyToken.clearCache(ChainId.MAINNET)
// ProviderLegacyToken.clearCache(ChainId.DEVNET)
const generator = new Generator([
...(trustedTokenList
? [
new ProviderTrusted(trustedTokenList, [], ChainId.MAINNET),
new ProviderTrusted(trustedTokenList, [], ChainId.DEVNET),
]
: []),
// new ProviderLegacyToken(
// baseTokenListCdnUrl,
// rpcUrlMainnet,
// {
// throttle: 2000,
// batchAccountsInfo: 200, // 1000
// batchSignatures: 200, // 250
// batchTokenHolders: 1, // 4
// },
// [Tag.LP_TOKEN],
// ChainId.MAINNET
// ),
new ProviderLegacyToken(
baseTokenListCdnUrl,
rpcUrlDevnet,
{
throttle: 2000,
batchAccountsInfo: 1000, // 1000
batchSignatures: 250, // 250
batchTokenHolders: 1, // 4
},
[Tag.LP_TOKEN],
ChainId.DEVNET,
60,
20
),
new ProviderJupiterTokenList(
jupiterTokenListUrl
),
new ProviderCoinGecko(coinGeckoApiKey, rpcUrlMainnet, {
throttle: 200,
throttleCoinGecko: 65 * 1000,
batchAccountsInfo: 200, // 1000
batchCoinGecko: 400, // 400
}),
],
[
...(ignoreTokenList
? [
new ProviderIgnore(ignoreTokenList, [], ChainId.MAINNET),
new ProviderIgnore(ignoreTokenList, [], ChainId.DEVNET),
]
: []),
])
const tokenMap = await generator.generateTokenList()
console.log(`${name} | generated ${inProgress} | ${new Date().toISOString()}`)
if (fileName) {
fs.writeFile(
`./${fileName}`,
JSON.stringify(tokenMap),
'utf8',
function (err) {
if (err) {
return console.log(`${name} | file errored`, err)
}
console.log(`${name} | file saved`)
}
)
} else {
console.log(`${name} | returned`)
return tokenMap
}
}
/* istanbul ignore next */
const cronJob = () =>
new CronJob(
process.env.CRON_TIME ?? '0 0 */4 * * *',
async () => {
if (inProgress) {
console.log(`${name} | skip already running ${inProgress} | ${Date.now()}`)
return
}
inProgress = Date.now()
try {
await handle('solana-tokenlist.json')
} catch (err) {
console.log(`${name} | errored ${inProgress}`, err)
} finally {
console.log(`${name} | finished ${inProgress} | ${Date.now()}`)
inProgress = false
}
},
null,
true,
'UTC'
)
const name = 'generate-utl'
let inProgress = null
module.exports = { handle, cronJob }