-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.ts
273 lines (271 loc) · 8.52 KB
/
cli.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { ENS } from "./services/ens";
import { EthereumPrice } from "./services/price";
import { Shares } from "./services/shares";
import { Supply } from "./services/supply";
import { Events } from "./services/sync";
import { Treasuries } from "./services/treasuries";
yargs(hideBin(process.argv))
.env("API3TRACKER")
.option("endpoint", {
default: "http://localhost:8545/",
alias: "e",
type: "string",
description: "Ethereum JSON+RPC endpoint",
})
.command({
command: "ens [sub]",
describe: "Operations with ENS cache",
builder: (yargs) => {
return yargs.option(`sub`, {
choises: ["reset", "import", "download"],
type: "string",
describe: `logs subcommand - reset or download new`,
});
},
handler: async ({ endpoint, sub }) => {
if (sub === "reset") {
await ENS.resetAll();
console.log("ENS cache was deleted");
} else if (sub === "import") {
const total = await ENS.importLocal("./.cache");
console.log(`saved ${total} new ENS records`);
} else if (sub === "download") {
const total = await ENS.download(endpoint);
console.log(`saved ${total} new ENS records`);
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.command({
command: "logs [sub]",
describe: "Operations with API3 DAO contract logs",
builder: (yargs) => {
return yargs
.option(`sub`, {
choises: ["reset", "download"],
type: "string",
describe: `logs subcommand - reset or download new`,
})
.option("coingecko_host", {
default:
process.env.API3TRACKER_COINGECKO_HOST || "api.coingecko.com",
type: "string",
description: "Host to use for CoinGecko API",
})
.option("coingecko_api_key", {
default: process.env.API3TRACKER_COINGECKO_API_KEY || "",
type: "string",
description: "API KEY to be used for CoinGecko-compatible API",
});
},
// eslint-disable-next-line camelcase
handler: async ({ endpoint, sub, coingecko_host, coingecko_api_key }) => {
if (sub === "reset") {
await Events.resetAll();
console.log("events were deleted");
} else if (sub === "download") {
const priceReader = EthereumPrice(coingecko_host, coingecko_api_key);
const total = await Events.download(endpoint, priceReader);
console.log(`downloaded ${total} new events`);
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.command({
command: "shares [sub]",
describe: "Operations with API3 DAO shares cache",
builder: (yargs) => {
return yargs
.option(`sub`, {
choises: ["reset", "download", "votings", "totals"],
type: "string",
describe: `shares subcommand - reset or download new`,
})
.option("rps-limit", {
type: "boolean",
default: false,
description: "Limit requests to RPC as 1 per second",
})
.option("tag", {
type: "string",
default: "",
description: "if present, all members with this tag will be scanned",
})
.option("member", {
type: "string",
default: "",
description: "hex address of the member to check its shares",
});
},
handler: async ({ endpoint, sub, member, tag, rpsLimit }) => {
if (sub === "reset") {
await Shares.resetAll();
console.log("shares cache records were deleted");
} else if (sub === "votings") {
const total = await Shares.downloadVotings();
console.log(`updated ${total} new records`);
} else if (sub === "totals") {
await Shares.recalculateTotals();
} else if (sub === "download") {
if (tag) {
const total = await Shares.downloadMembers(endpoint, tag, rpsLimit);
console.log(`scanned ${total} members`);
} else {
const total = await Shares.download(endpoint, member, rpsLimit);
console.log(`downloaded ${total} new records`);
}
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.command({
command: "state [sub]",
describe: "Operations with API3 dao state (members and votings)",
builder: (yargs) => {
return yargs
.option(`sub`, {
choises: ["reset", "update", "next"],
type: "string",
describe: `supply subcommand - reset or update current state based on new blocks`,
})
.option("verbose-epochs", {
type: "boolean",
description: "Run with verbose epochs logging",
})
.option("use-archive", {
type: "boolean",
default: false,
description: "Run with using features of Ethereum archive node",
})
.option("stop-block", {
type: "number",
default: 0,
description:
"Calculate state until the certain block number and stop without writing it",
})
.option("verbose-member", {
type: "string",
description:
"Run with verbose member logging (hex address to be provided)",
})
.option("verbose-votings", {
type: "boolean",
description: "Run with verbose votings logging",
})
.option("verbose-blocks", {
alias: "v",
type: "boolean",
default: true,
description: "Run with verbose block logging",
});
},
handler: async ({
endpoint,
sub,
verboseBlocks,
verboseEpochs,
verboseVotings,
verboseMember,
stopBlock,
useArchive,
}) => {
if (sub === "reset") {
await Events.resetState();
console.log("Events state was reset");
} else if (sub === "next") {
const verbose = {
blocks: verboseBlocks || false,
epochs: verboseEpochs || false,
votings: verboseVotings || false,
member: verboseMember || "",
};
const termination = {
epoch: true,
block: 0,
};
const blocks = await Events.processState(
endpoint,
verbose,
termination,
useArchive,
);
console.log(`${blocks} blocks were processed`);
} else if (sub === "update") {
const verbose = {
blocks: verboseBlocks || false,
epochs: verboseEpochs || false,
votings: verboseVotings || false,
member: verboseMember || "",
};
const termination = {
epoch: false,
block: stopBlock,
};
const blocks = await Events.processState(
endpoint,
verbose,
termination,
useArchive,
);
console.log(`${blocks} blocks were processed`);
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.command({
command: "supply [sub]",
describe: "Operations with API3 token supply",
builder: (yargs) => {
return yargs.option(`sub`, {
choises: ["reset", "download"],
type: "string",
describe: `supply subcommand - reset or download current state`,
});
},
handler: async ({ endpoint, sub }) => {
if (sub === "reset") {
await Supply.resetAll();
console.log("Supply history was reset");
} else if (sub === "download") {
await Supply.download(endpoint);
console.log("Supply history was updated");
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.command({
command: "treasuries [sub]",
describe: "Operations with API3 DAO treasuries",
builder: (yargs) => {
return yargs.option(`sub`, {
choises: ["reset", "download"],
type: "string",
describe: `treasuries subcommand - reset or download current state`,
});
},
handler: async ({ endpoint, sub }) => {
if (sub === "reset") {
await Treasuries.resetAll();
console.log("Treasuries state was reset");
} else if (sub === "download") {
const total = await Treasuries.download(endpoint);
console.log(`downloaded state of ${total} balances`);
} else {
console.error("ERROR: Unknown sub-command");
process.exit(1);
}
},
})
.parse();