forked from matter-labs/zksync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
86 lines (69 loc) · 2.56 KB
/
index.ts
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
import { Command } from 'commander';
import { diffTokenLists, TokenInfo } from '@uniswap/token-lists';
import { getTokenList } from 'reading-tool';
import fetch from 'node-fetch';
import * as readline from 'readline';
import * as fs from 'fs';
import * as path from 'path';
const configPath = path.join(process.env.ZKSYNC_HOME as string, `infrastructure/token-lists-manager`);
const tokenListConfig = JSON.parse(
fs.readFileSync(`${configPath}/.token-lists-sources-config.json`, { encoding: 'utf-8' })
);
function saveTokenList(source: string, tokeList: TokenInfo[]) {
const path = `${process.env.ZKSYNC_HOME as string}/etc/token-lists/${source}.json`;
fs.writeFileSync(path, JSON.stringify(tokeList));
}
async function fetchFromSourceTokenList(source: string): Promise<TokenInfo[] | null> {
try {
const link = tokenListConfig[source];
const response = await fetch(link);
const tokenList = (await response.json()).tokens as TokenInfo[];
return tokenList;
} catch (err) {
console.log('Failed to load new token list: ', err);
return null;
}
}
async function updateTokenList(source: string): Promise<boolean> {
console.log(`Update ${source} token list`);
const oldTokenList = getTokenList(source) as TokenInfo[];
const newTokenList = await fetchFromSourceTokenList(source);
console.log(`diff: `, diffTokenLists(oldTokenList, newTokenList));
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const input = (await new Promise((resolve) => {
rl.question('Want to update tokens with such changes? (Y/n)\n', (input) => {
rl.close();
resolve(input);
});
})) as string;
if (input.toLowerCase() != 'y') {
return false;
}
saveTokenList(source, newTokenList);
return true;
}
async function main() {
const program = new Command();
program.version('0.1.0').name('token-lists-manager');
program
.command('update <source>')
.description('Update token list')
.action(async (source: string) => {
let success = await updateTokenList(source);
if (success) {
console.log(`${source} token list updated successfully`);
} else {
console.log(`Failed to update ${source} token list`);
}
});
await program.parseAsync(process.argv);
}
main()
.then(() => process.exit(0))
.catch((err) => {
console.error('Error:', err.message || err);
process.exit(1);
});