-
Notifications
You must be signed in to change notification settings - Fork 39
/
hardhat.config.ts
482 lines (421 loc) · 17.3 KB
/
hardhat.config.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import '@nomiclabs/hardhat-ethers';
import '@nomiclabs/hardhat-vyper';
import '@nomiclabs/hardhat-waffle';
import 'hardhat-local-networks-config-plugin';
import 'hardhat-ignore-warnings';
import 'tsconfig-paths/register';
import './src/helpers/setupTests';
import { task } from 'hardhat/config';
import { TASK_TEST } from 'hardhat/builtin-tasks/task-names';
import { HardhatRuntimeEnvironment } from 'hardhat/types';
import path from 'path';
import { existsSync, readdirSync, readFileSync, statSync } from 'fs';
import { checkArtifact, extractArtifact } from './src/artifact';
import test from './src/test';
import Task, { TaskMode, TaskStatus } from './src/task';
import Verifier from './src/verifier';
import logger, { Logger } from './src/logger';
import {
checkActionIds,
checkActionIdUniqueness,
saveActionIds,
getActionIdInfo,
fetchTheGraphPermissions,
} from './src/actionId';
import {
checkContractDeploymentAddresses,
checkTimelockAuthorizerConfig,
getTimelockAuthorizerConfigDiff,
saveContractDeploymentAddresses,
saveTimelockAuthorizerConfig,
withRetries,
} from './src/network';
const THEGRAPHURLS: { [key: string]: string } = {};
task('deploy', 'Run deployment task')
.addParam('id', 'Deployment task ID')
.addFlag('force', 'Ignore previous deployments')
.addOptionalParam('key', 'Etherscan API key to verify contracts')
.setAction(
async (args: { id: string; force?: boolean; key?: string; verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const apiKey = args.key ?? (hre.config.networks[hre.network.name] as any).verificationAPIKey;
const verifier = apiKey ? new Verifier(hre.network, apiKey) : undefined;
await new Task(args.id, TaskMode.LIVE, hre.network.name, verifier).run(args);
}
);
task('verify-contract', `Verify a task's deployment on a block explorer`)
.addParam('id', 'Deployment task ID')
.addParam('name', 'Contract name')
.addParam('address', 'Contract address')
.addParam('args', 'ABI-encoded constructor arguments')
.addOptionalParam('key', 'Etherscan API key to verify contracts')
.setAction(
async (
args: { id: string; name: string; address: string; key: string; args: string; verbose?: boolean },
hre: HardhatRuntimeEnvironment
) => {
Logger.setDefaults(false, args.verbose || false);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const apiKey = args.key ?? (hre.config.networks[hre.network.name] as any).verificationAPIKey;
const verifier = apiKey ? new Verifier(hre.network, apiKey) : undefined;
// Contracts can only be verified in Live mode
await new Task(args.id, TaskMode.LIVE, hre.network.name, verifier).verify(args.name, args.address, args.args);
}
);
task('extract-artifacts', `Extract contract artifacts from their build-info`)
.addOptionalParam('id', 'Specific task ID')
.addOptionalParam('file', 'Target build-info file name')
.addOptionalParam('name', 'Contract name')
.setAction(async (args: { id?: string; file?: string; name?: string; verbose?: boolean }) => {
Logger.setDefaults(false, args.verbose || false);
if (args.id) {
const task = new Task(args.id, TaskMode.READ_ONLY);
extractArtifact(task, args.file, args.name);
} else {
for (const taskID of Task.getAllTaskIds()) {
const task = new Task(taskID, TaskMode.READ_ONLY);
extractArtifact(task, args.file, args.name);
}
}
});
task('check-deployments', `Check that all tasks' deployments correspond to their build-info and inputs`)
.addOptionalParam('id', 'Specific task ID')
.setAction(async (args: { id?: string; force?: boolean; verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
// The force argument above is actually not passed (and not required or used in CHECK mode), but it is the easiest
// way to address type issues.
Logger.setDefaults(false, args.verbose || false);
logger.log(`Checking deployments for ${hre.network.name}...`, '');
if (args.id) {
await new Task(args.id, TaskMode.CHECK, hre.network.name).run(args);
} else {
for (const taskID of Task.getAllTaskIds()) {
if (taskID.startsWith('00000000-')) {
continue;
}
const task = new Task(taskID, TaskMode.CHECK, hre.network.name);
const outputDir = path.resolve(task.dir(), 'output');
if (existsSync(outputDir) && statSync(outputDir).isDirectory()) {
const outputFiles = readdirSync(outputDir);
if (outputFiles.some((outputFile) => outputFile.includes(hre.network.name))) {
// Not all tasks have outputs for all networks, so we skip those that don't
await withRetries(async () => task.run(args));
}
}
}
}
});
task('check-artifacts', `check that contract artifacts correspond to their build-info`)
.addOptionalParam('id', 'Specific task ID')
.setAction(async (args: { id?: string; verbose?: boolean }) => {
Logger.setDefaults(false, args.verbose || false);
if (args.id) {
const task = new Task(args.id, TaskMode.READ_ONLY);
checkArtifact(task);
} else {
for (const taskID of Task.getAllTaskIds()) {
const task = new Task(taskID, TaskMode.READ_ONLY);
checkArtifact(task);
}
}
});
task('save-action-ids', `Print the action IDs for a particular contract and checks their uniqueness`)
.addOptionalParam('id', 'Specific task ID')
.addOptionalParam('name', 'Contract name')
.addOptionalParam('address', 'Address of Pool created from a factory')
.setAction(
async (args: { id: string; name: string; address?: string; verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
async function saveActionIdsTask(
args: { id: string; name: string; address?: string; verbose?: boolean },
hre: HardhatRuntimeEnvironment
) {
Logger.setDefaults(false, args.verbose || false);
// The user is calculating action IDs for a contract which isn't included in the task outputs.
// Most likely this is for a pool which is to be deployed from a factory contract deployed as part of the task.
if (args.address) {
if (!args.id || !args.name) {
throw new Error(
"Provided an address for Pool created from a factory but didn't specify task or contract name."
);
}
const task = new Task(args.id, TaskMode.READ_ONLY, hre.network.name);
await saveActionIds(task, args.name, args.address);
return;
}
// The user is calculating the action IDs for a particular task or contract within a particular task.
if (args.id && args.name) {
const task = new Task(args.id, TaskMode.READ_ONLY, hre.network.name);
await saveActionIds(task, args.name);
return;
}
async function generateActionIdsForTask(taskId: string): Promise<void> {
const task = new Task(taskId, TaskMode.READ_ONLY, hre.network.name);
const outputDir = path.resolve(task.dir(), 'output');
if (existsSync(outputDir) && statSync(outputDir).isDirectory()) {
for (const outputFile of readdirSync(outputDir)) {
const outputFilePath = path.resolve(outputDir, outputFile);
if (outputFile.includes(hre.network.name) && statSync(outputFilePath).isFile()) {
const fileContents = JSON.parse(readFileSync(outputFilePath).toString());
const contractNames = Object.keys(fileContents);
for (const contractName of contractNames) {
await saveActionIds(task, contractName);
}
}
}
}
}
if (args.id) {
await generateActionIdsForTask(args.id);
return;
}
// We're calculating action IDs for whichever contracts we can pull enough information from disk for.
// This will calculate action IDs for any contracts which are a named output from a task.
for (const taskID of Task.getAllTaskIds()) {
await generateActionIdsForTask(taskID);
}
}
await saveActionIdsTask(args, hre);
checkActionIdUniqueness(hre.network.name);
}
);
task('check-action-ids', `Check that contract action-ids correspond the expected values`)
.addOptionalParam('id', 'Specific task ID')
.setAction(async (args: { id?: string; verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
logger.log(`Checking action IDs for ${hre.network.name}...`, '');
if (args.id) {
const task = new Task(args.id, TaskMode.READ_ONLY, hre.network.name);
await checkActionIds(task);
} else {
for (const taskID of Task.getAllTaskIds()) {
const task = new Task(taskID, TaskMode.READ_ONLY, hre.network.name);
await withRetries(async () => checkActionIds(task));
}
}
checkActionIdUniqueness(hre.network.name);
});
task('get-action-id-info', `Returns all the matches for the given actionId`)
.addPositionalParam('id', 'ActionId to use for the lookup')
.setAction(async (args: { id: string; verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
logger.info(`Looking for action ID info on ${hre.network.name}...`);
const actionIdInfo = await getActionIdInfo(args.id, hre.network.name);
if (actionIdInfo) {
logger.log(`Found the following matches:`, '');
logger.log(JSON.stringify(actionIdInfo, null, 2), '');
} else {
logger.log(`No entries found for the actionId`, '');
}
});
task('get-action-ids-info', `Reconstructs all the permissions from TheGraph AP and action-ids files`).setAction(
async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
logger.log(`Fetching permissions using TheGraph API on ${hre.network.name}...`, '');
const permissions = await fetchTheGraphPermissions(THEGRAPHURLS[hre.network.name]);
const infos = (
await Promise.all(permissions.map((permission) => getActionIdInfo(permission.action.id, hre.network.name)))
).map((info, index) => ({
...info,
grantee: permissions[index].account,
actionId: permissions[index].action.id,
txHash: permissions[index].txHash,
}));
logger.log(`Found the following matches:`, '');
process.stdout.write(JSON.stringify(infos, null, 2));
}
);
task('build-address-lookup', `Build a lookup table from contract addresses to the relevant deployment`).setAction(
async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
if (hre.network.name === 'hardhat') {
logger.warn(`invalid network: ${hre.network.name}`);
return;
}
// Create Task objects, excluding tokens tasks.
const tasks = Task.getAllTaskIds()
.filter((taskId) => !taskId.startsWith('00000000-'))
.map((taskId) => new Task(taskId, TaskMode.READ_ONLY, hre.network.name));
saveContractDeploymentAddresses(tasks, hre.network.name);
logger.success(`Address lookup generated for network ${hre.network.name}`);
}
);
task(
'check-address-lookup',
`Check whether the existing lookup table from contract addresses to the relevant deployments is correct`
).setAction(async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
if (hre.network.name === 'hardhat') {
logger.warn(`invalid network: ${hre.network.name}`);
return;
}
// Create Task objects, excluding tokens tasks.
const tasks = Task.getAllTaskIds()
.filter((taskId) => !taskId.startsWith('00000000-'))
.map((taskId) => new Task(taskId, TaskMode.READ_ONLY, hre.network.name));
const addressLookupFileOk = checkContractDeploymentAddresses(tasks, hre.network.name);
if (!addressLookupFileOk) {
throw new Error(
`Address lookup file is incorrect for network ${hre.network.name}. Please run 'build-address-lookup' to regenerate it`
);
} else {
logger.success(`Address lookup file is correct for network ${hre.network.name}`);
}
});
task('build-timelock-authorizer-config', `Builds JSON file with Timelock Authorizer configuration`).setAction(
async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
if (hre.network.name === 'hardhat') {
logger.warn(`invalid network: ${hre.network.name}`);
return;
}
// Get active timelock authorizer task.
const tasks = Task.getAllTaskIds()
.filter((taskId) => taskId.includes('timelock-authorizer'))
.map((taskId) => new Task(taskId, TaskMode.READ_ONLY, hre.network.name))
.filter((task) => task.getStatus() === TaskStatus.ACTIVE);
if (tasks.length !== 1) {
const errorMsg = tasks.length === 0 ? 'not found' : 'is not unique';
logger.error(`Active timelock authorizer task ${errorMsg}`);
return;
}
saveTimelockAuthorizerConfig(tasks[0], hre.network.name);
logger.success(`Timelock Authorizer config JSON generated for network ${hre.network.name}`);
}
);
task(
'check-timelock-authorizer-config',
`Check whether the existing timelock authorizer configuration file is correct`
).setAction(async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
if (hre.network.name === 'hardhat') {
logger.warn(`invalid network: ${hre.network.name}`);
return;
}
// Get active timelock authorizer task.
const tasks = Task.getAllTaskIds()
.filter((taskId) => taskId.includes('timelock-authorizer'))
.map((taskId) => new Task(taskId, TaskMode.READ_ONLY, hre.network.name))
.filter((task) => task.getStatus() === TaskStatus.ACTIVE);
if (tasks.length !== 1) {
const errorMsg = tasks.length === 0 ? 'not found' : 'is not unique';
logger.error(`Active timelock authorizer task ${errorMsg}`);
return;
}
const isConfigOk = checkTimelockAuthorizerConfig(tasks[0], hre.network.name);
if (isConfigOk) {
logger.success(`Timelock Authorizer config JSON is correct for network ${hre.network.name}`);
} else {
throw new Error(
`Timelock Authorizer config file is incorrect for network ${hre.network.name}. Please run 'build-timelock-authorizer-config' to regenerate it`
);
}
});
task(
'verify-timelock-authorizer-config',
`Check whether the existing timelock authorizer configuration file matches the delays configured onchain`
).setAction(async (args: { verbose?: boolean }, hre: HardhatRuntimeEnvironment) => {
Logger.setDefaults(false, args.verbose || false);
if (hre.network.name === 'hardhat') {
logger.warn(`invalid network: ${hre.network.name}`);
return;
}
// Get active timelock authorizer task.
const tasks = Task.getAllTaskIds()
.filter((taskId) => taskId.includes('timelock-authorizer'))
.map((taskId) => new Task(taskId, TaskMode.READ_ONLY, hre.network.name))
.filter((task) => task.getStatus() === TaskStatus.ACTIVE);
if (tasks.length !== 1) {
const errorMsg = tasks.length === 0 ? 'not found' : 'is not unique';
logger.error(`Active timelock authorizer task ${errorMsg}`);
return;
}
const configDiff = await getTimelockAuthorizerConfigDiff(tasks[0], hre.network.name);
if (configDiff.length === 0) {
logger.success(`Timelock Authorizer config is correctly applied on-chain for network ${hre.network.name}`);
} else {
throw new Error(
`Timelock Authorizer config file is incorrect for network ${
hre.network.name
}. Differences found:\n${JSON.stringify(configDiff, null, 2)}`
);
}
});
task(TASK_TEST).addOptionalParam('id', 'Specific task ID of the fork test to run.').setAction(test);
export default {
mocha: {
timeout: 600000,
},
solidity: {
compilers: [
{
version: '0.7.1',
settings: {
optimizer: {
enabled: true,
runs: 9999,
},
},
},
{
version: '0.8.24',
settings: {
evmVersion: 'cancun',
optimizer: {
enabled: true,
runs: 9999,
},
},
},
],
},
vyper: {
compilers: [{ version: '0.3.1' }, { version: '0.3.3' }],
},
paths: {
artifacts: './src/helpers/.hardhat/artifacts',
cache: './src/helpers/.hardhat/cache',
sources: './src/helpers/contracts',
},
warnings: {
'*': {
'shadowing-opcode': 'off',
default: 'error',
},
},
etherscan: {
customChains: [
{
network: 'zkemv',
chainId: 1101,
urls: {
apiURL: 'https://api-zkevm.polygonscan.com/api',
browserURL: 'https://zkevm.polygonscan.com/',
},
},
{
network: 'base',
chainId: 8453,
urls: {
apiURL: 'https://api.basescan.org/api',
browserURL: 'https://basescan.org/',
},
},
{
network: 'fraxtal',
chainId: 252,
urls: {
apiURL: 'https://api.fraxscan.com/api',
browserURL: 'https://fraxscan.com/',
},
},
{
network: 'mode',
chainId: 34443,
urls: {
apiURL: 'https://api.routescan.io/v2/network/mainnet/evm/34443/etherscan',
browserURL: 'https://modescan.io',
},
},
],
},
};