This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
406 lines (380 loc) · 11.1 KB
/
main.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env node
import "dotenv/config";
import inquirer, { Question } from "inquirer";
import { hideBin } from "yargs/helpers";
import yargs from "yargs/yargs";
import { ChainId } from "@aave/contract-helpers";
import {
createFork,
forkIdToForkParams,
fundAccount,
getForkParameters,
} from "./src/tenderly";
import {
createProposal,
deployPayload,
passAndExecuteProposal,
} from "./src/governance";
import { executeL2Payload } from "./src/l2Gov";
inquirer.registerPrompt("fuzzypath", require("inquirer-fuzzy-path"));
type NewFork = {
forkType: "new";
};
type ExistingFork = {
forkType: "existing";
forkId: string;
};
type ForkOptions = NewFork | ExistingFork;
type CommonOptions = {
networkName?: keyof typeof ChainId;
networkId: ChainId;
blockNumber?: string;
forkNetworkId: string;
forkId?: string;
forkLabel?: string;
//
enterProposalId: boolean;
proposalId?: number;
enterPayloadAddress: boolean;
payloadAddress?: string;
enterArtifactPath: boolean;
artifactPath?: string;
//
userAddress?: string;
keepAlive?: boolean | string;
};
type Options = ForkOptions & CommonOptions;
interface SharedQuestion {
// shared
type: "string" | "list" | "confirm" | "fuzzypath" | "number";
default?: string | number | boolean;
// additional property for option in --help which requires static options
staticChoices?: string[] | number[];
choices?: string[] | number[] | ((args: Options) => string[]);
when?:
| ((args: Options) => boolean | undefined)
| ((args: Options) => Promise<boolean | undefined>);
inquirerOnly?: boolean;
message: string;
itemType?: "file";
}
interface InquirerQuestion extends SharedQuestion {
// inquirer
inquirerOnly: true;
}
interface YargsQuestion extends SharedQuestion {
// yargs
demandOption?: boolean;
describe: string;
coerce?:
| ((args: Options) => Promise<Partial<Options>>)
| ((args: Options) => Partial<Options>);
}
// The questions are split in two so we can async fetch networkId and similar implicit values in between
const initialQuestions: { [key: string]: InquirerQuestion | YargsQuestion } = {
// determine if we want to create a new fork or reuse existing one
forkType: {
// inquirer
message: "Want to use existing fork or create a new one?",
// shared
type: "list",
default: "new",
choices: ["new", "existing"],
inquirerOnly: true,
},
forkId: {
message: "Enter forkId",
describe: "Reuse existing fork id",
type: "string",
when: (args) => args.forkType === "existing",
},
};
const questions: { [key: string]: InquirerQuestion | YargsQuestion } = {
...initialQuestions,
// config to setup a custom fork
networkName: {
message: "Select network to fork",
describe: "Network to be forked",
type: "list",
choices: [
"mainnet",
"optimism",
"polygon",
"fantom",
"arbitrum_one",
"avalanche",
"harmony",
],
inquirerOnly: true,
when: (args) => args.forkType === "new",
},
networkId: {
message: "Select network to fork",
describe: "Network to be forked",
type: "list",
choices: [
ChainId.mainnet,
ChainId.optimism,
ChainId.polygon,
ChainId.fantom,
ChainId.arbitrum_one,
ChainId.avalanche,
ChainId.harmony,
],
default: ChainId.mainnet,
when: (args) => {
// little hack to implicitly set networkId
if (args.networkName) args.networkId = ChainId[args.networkName];
return args.forkType === "new" && !args.networkName;
},
},
blockNumber: {
message: "Select the blockNumber to fork off",
describe: "Blocknumber to fork off",
type: "string",
default: "latest",
when: (args) => args.forkType === "new",
},
forkNetworkId: {
message: "Select the networkId of the fork",
describe: "NetworkId used by the fork",
type: "string",
default: "3030",
when: (args) => args.forkType === "new",
},
enterProposalId: {
message: "Do you want to execute a pending proposalId?",
inquirerOnly: true,
type: "confirm",
when: (args) => Number(args.networkId) === ChainId.mainnet,
},
proposalId: {
message: "Existing proposalId to execute",
describe: "The proposal id to execute",
type: "number",
when: (args) =>
args.enterProposalId === true &&
Number(args.networkId) === ChainId.mainnet,
},
enterPayloadAddress: {
message: "Do you want to execute a deployed proposal payload?",
inquirerOnly: true,
type: "confirm",
when: (args) => !args.proposalId,
},
payloadAddress: {
message: "Enter the deployed payload address",
describe: "The payload address to execute",
type: "string",
when: (args) => args.enterPayloadAddress === true && !args.proposalId,
},
enterArtifactPath: {
message: "Do you want to deploy and execute a local payload?",
inquirerOnly: true,
type: "confirm",
when: (args) => !args.proposalId && !args.payloadAddress,
},
artifactPath: {
type: "fuzzypath",
itemType: "file",
message: "Path to artifact.json",
describe: "The path to the artifact to execute",
when: (args) =>
args.enterArtifactPath === true &&
!args.proposalId &&
!args.payloadAddress,
},
userAddress: {
message:
"Enter an address you want to fund with 1000 native currency on the fork?",
describe: "Address to fund with 1000 of native currency",
type: "string",
},
forkLabel: {
message:
"Enter label to be used on tenderly (keep empty for autogenerated)",
describe: "Label will be used in tenderly to differentiate between forks",
type: "string",
when: (args) => args.forkType === "new",
},
keepAlive: {
message: "Should the fork be kept alive after terminal is closed?",
describe: "Keep the fork alive after this session",
type: "confirm",
default: false,
when: (args) => args.forkType === "new",
},
};
function getPrompts(options: {
[key: string]: InquirerQuestion | YargsQuestion;
}) {
return Object.entries(options).map(
([name, { choices, default: _default, message, type, when }]) => ({
choices,
default: _default,
message,
name,
type,
when,
})
);
}
function typeToYargsType(type: SharedQuestion["type"]) {
if (type === "fuzzypath") return "string";
if (type === "confirm") return "boolean";
return type;
}
function getOptions(options: {
[key: string]: InquirerQuestion | YargsQuestion;
}) {
return Object.entries(options as { [key: string]: YargsQuestion })
.filter(([key, value]) => !value.inquirerOnly)
.reduce(
(
previous,
[
name,
{
staticChoices,
choices,
default: _default,
demandOption,
describe,
type,
coerce,
},
]
) => {
previous[name] = {
choices: staticChoices || choices || undefined,
default: _default,
demandOption,
describe,
type: typeToYargsType(type),
coerce,
};
return previous;
},
{} as any
);
}
function getName(options: Options) {
const unix = Math.floor(new Date().getTime() / 1000);
if (options.forkLabel) {
return `${unix}-${options.forkLabel}`;
} else if (options.proposalId) {
return `${unix}-proposalId-${options.proposalId}`;
} else if (options.payloadAddress) {
return `${unix}-payloadAddress-${options.payloadAddress}`;
} else if (options.artifactPath) {
return `${unix}-artifact-${options.artifactPath.replace(/^.*[\\\/]/, "")}`;
}
return "vanilla-fork";
}
(async () => {
const hideBinArgv = hideBin(process.argv);
if (!hideBinArgv.length) {
// figure out is using existing or new fork
const initialAnswers = await inquirer.prompt(getPrompts(initialQuestions));
if (initialAnswers.forkType === "existing") {
// seed parameters on existing fork
const params = await getForkParameters({ forkId: initialAnswers.forkId });
console.log(params);
initialAnswers.blockNumber = params.blockNumber;
initialAnswers.forkNetworkId = params.forkNetworkId;
initialAnswers.networkId = params.networkId;
}
const answers = await inquirer.prompt(
getPrompts(questions),
initialAnswers
);
Object.entries(answers).forEach(([key, value]) => {
value && hideBinArgv.push(`--${key}`, value as any);
});
}
let argv = (await yargs(hideBinArgv)
.usage("Usage: npx $0")
.options(getOptions(questions))
.parseAsync()) as unknown as Options;
const alias = getName(argv);
const forkId =
(argv as any).forkId ||
(await createFork({
alias,
forkNetworkId: argv.forkNetworkId,
networkId: String(argv.networkId),
blockNumber: argv.blockNumber === "latest" ? undefined : argv.blockNumber,
keepAlive: argv.keepAlive === true || argv.keepAlive === "true",
}));
const fork = forkIdToForkParams({ forkId });
if (argv.proposalId) {
await passAndExecuteProposal({
proposalId: Number(argv.proposalId),
provider: fork.provider,
});
} else if (argv.payloadAddress) {
if (Number(argv.networkId) === ChainId.mainnet) {
const proposalId = await createProposal({
payloadAddress: argv.payloadAddress,
provider: fork.provider,
});
await passAndExecuteProposal({
proposalId: proposalId,
provider: fork.provider,
});
} else {
await executeL2Payload({
payloadAddress: argv.payloadAddress,
provider: fork.provider,
networkId: argv.networkId,
});
}
} else if (argv.artifactPath) {
const payloadAddress = await deployPayload({
filePath: argv.artifactPath,
provider: fork.provider,
});
if (Number(argv.networkId) === ChainId.mainnet) {
const proposalId = await createProposal({
provider: fork.provider,
payloadAddress: payloadAddress,
});
await passAndExecuteProposal({
provider: fork.provider,
proposalId: proposalId,
});
} else {
await executeL2Payload({
payloadAddress,
provider: fork.provider,
networkId: argv.networkId,
});
}
}
if (argv.forkId && !argv.networkId) {
const params = await getForkParameters({
forkId: argv.forkId,
});
argv = { ...argv, ...params };
}
if (argv.userAddress) {
await fundAccount(forkId, argv.userAddress);
}
console.log(`To use this fork on the aave interface you need to do the following things.
1. Open the browser console on app.aave.com (or a local instance) and enter
--------------
localStorage.setItem('forkEnabled', 'true');
localStorage.setItem('forkBaseChainId', ${argv.networkId});
localStorage.setItem('forkNetworkId', ${argv.forkNetworkId});
localStorage.setItem("forkRPCUrl", "${fork.forkUrl}");
--------------
2. As localStorage is not observable you need to reload now.
3. You can now see & select forked mainnet markets on the ui.
To interact with them you still need to setup your wallet.
To setup your wallet you need to add a network with:
--------------
networkId: ${argv.networkId}
rpcUrl: ${fork.forkUrl}
--------------
`);
})();