This repository has been archived by the owner on Jan 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
hardhat.config.ts
368 lines (326 loc) · 10.8 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
import '@nomicfoundation/hardhat-foundry'
import '@nomiclabs/hardhat-ethers'
import '@typechain/hardhat'
import dotenv from 'dotenv'
import fs from 'fs'
import 'hardhat-contract-sizer'
import 'hardhat-deploy'
import 'hardhat-storage-layout-changes'
import { HardhatUserConfig, task } from 'hardhat/config'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
dotenv.config()
const lazyImport = async (module: any) => {
return await import(module)
}
async function saveDeployments(
env: string,
deploymentData: { [key in string]: string },
branch?: string,
) {
const deploymentsJsonPath = `${process.cwd()}/deployments.json`
let deploymentsJson = { [env]: {} }
if (fs.existsSync(deploymentsJsonPath)) {
deploymentsJson = JSON.parse(
fs.readFileSync(deploymentsJsonPath).toString(),
)
}
if (branch) {
deploymentsJson[env] = {
...deploymentsJson[env],
[branch]: deploymentData,
}
} else {
deploymentsJson[env] = { ...deploymentsJson[env], ...deploymentData }
}
fs.writeFileSync(deploymentsJsonPath, JSON.stringify(deploymentsJson))
}
async function saveDeploymentsFacets(
filename: string,
env: string,
updatedFacets: { [key: string]: string },
branch?: string,
) {
const deploymentsJsonPath = `${process.cwd()}/${filename}`
let deploymentsJson = { [env]: {} }
if (fs.existsSync(deploymentsJsonPath)) {
deploymentsJson = JSON.parse(
fs.readFileSync(deploymentsJsonPath).toString(),
)
}
const facets = deploymentsJson[env]['Facets']
for (const facetIndex in facets) {
const facetName = facets[facetIndex].name
if (updatedFacets[facetName]) {
facets[facetIndex].address = updatedFacets[facetName]
}
}
fs.writeFileSync(deploymentsJsonPath, JSON.stringify(deploymentsJson))
}
async function saveSubnetRegistry(
env: string,
subnetRegistryData: { [key in string]: string },
) {
const subnetRegistryJsonPath = `${process.cwd()}/subnet.registry.json`
let subnetRegistryJson = { [env]: {} }
if (fs.existsSync(subnetRegistryJsonPath)) {
subnetRegistryJson = JSON.parse(
fs.readFileSync(subnetRegistryJsonPath).toString(),
)
}
subnetRegistryJson[env] = {
...subnetRegistryJson[env],
...subnetRegistryData,
}
fs.writeFileSync(subnetRegistryJsonPath, JSON.stringify(subnetRegistryJson))
}
async function saveSubnetActor(
env: string,
subnetRegistryData: { [key in string]: string },
) {
const subnetRegistryJsonPath = `${process.cwd()}/subnet.actor.json`
let subnetRegistryJson = { [env]: {} }
if (fs.existsSync(subnetRegistryJsonPath)) {
subnetRegistryJson = JSON.parse(
fs.readFileSync(subnetRegistryJsonPath).toString(),
)
}
subnetRegistryJson[env] = {
...subnetRegistryJson[env],
...subnetRegistryData,
}
fs.writeFileSync(subnetRegistryJsonPath, JSON.stringify(subnetRegistryJson))
}
async function getSubnetRegistry(
env: string,
): Promise<{ [key in string]: string }> {
const subnetRegistryJsonPath = `${process.cwd()}/subnet.registry.json`
let subnetRegistry = {}
if (fs.existsSync(subnetRegistryJsonPath)) {
subnetRegistry = JSON.parse(
fs.readFileSync(subnetRegistryJsonPath).toString(),
)[env]
}
return subnetRegistry
}
async function getSubnetActor(
env: string,
): Promise<{ [key in string]: string }> {
const subnetRegistryJsonPath = `${process.cwd()}/subnet.actor.json`
let subnetRegistry = {}
if (fs.existsSync(subnetRegistryJsonPath)) {
subnetRegistry = JSON.parse(
fs.readFileSync(subnetRegistryJsonPath).toString(),
)[env]
}
return subnetRegistry
}
async function getDeployments(
env: string,
): Promise<{ [key in string]: string }> {
const deploymentsJsonPath = `${process.cwd()}/deployments.json`
let deployments = {}
if (fs.existsSync(deploymentsJsonPath)) {
deployments = JSON.parse(
fs.readFileSync(deploymentsJsonPath).toString(),
)[env]
}
return deployments
}
task(
'deploy-libraries',
'Build and deploys all libraries on the selected network',
async (args, hre: HardhatRuntimeEnvironment) => {
const { deploy } = await lazyImport('./scripts/deploy-libraries')
const libsDeployment = await deploy()
console.log(libsDeployment)
await saveDeployments(hre.network.name, libsDeployment, 'libs')
},
)
task(
'deploy-gateway',
'Builds and deploys the Gateway contract on the selected network',
async (args, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name
const deployments = await getDeployments(network)
const { deploy } = await lazyImport('./scripts/deploy-gateway')
const gatewayDeployment = await deploy(deployments.libs)
console.log(JSON.stringify(gatewayDeployment, null, 2))
await saveDeployments(network, gatewayDeployment)
},
)
task(
'deploy-subnet-registry',
'Builds and deploys the Registry contract on the selected network',
async (args, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name
const { deploy } = await lazyImport('./scripts/deploy-registry')
const subnetRegistryDeployment = await deploy()
console.log(JSON.stringify(subnetRegistryDeployment, null, 2))
await saveSubnetRegistry(network, subnetRegistryDeployment)
},
)
task(
'deploy-gw-diamond-and-facets',
'Builds and deploys Gateway Actor diamond and its facets',
async (args, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name
const deployments = await getDeployments(network)
const { deployDiamond } = await lazyImport(
'./scripts/deploy-gw-diamond',
)
const gatewayActorDiamond = await deployDiamond(deployments.libs)
await saveDeployments(network, gatewayActorDiamond)
},
)
task(
'deploy-sa-diamond-and-facets',
'Builds and deploys Subnet Actor diamond and its facets',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
const network = hre.network.name
const deployments = await getDeployments(network)
const { deployDiamond } = await lazyImport(
'./scripts/deploy-sa-diamond',
)
const subnetActorDiamond = await deployDiamond(
deployments.Gateway,
deployments.libs,
)
await saveSubnetActor(network, subnetActorDiamond)
},
)
task(
'deploy',
'Builds and deploys all contracts on the selected network',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
await hre.run('deploy-libraries')
await hre.run('deploy-gateway')
},
)
task(
'deploy-gw-diamond',
'Builds and deploys Gateway Actor diamond',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
await hre.run('deploy-libraries')
await hre.run('deploy-gw-diamond-and-facets')
},
)
task(
'deploy-sa-diamond',
'Builds and deploys Subnet Actor diamond',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
await hre.run('deploy-libraries')
await hre.run('deploy-sa-diamond-and-facets')
},
)
task(
'upgrade-gw-diamond',
'Upgrades IPC Gateway Actor Diamond Facets on an EVM-compatible subnet using hardhat',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
const network = hre.network.name
const deployments = await getDeployments(network)
const { upgradeDiamond } = await lazyImport(
'./scripts/upgrade-gw-diamond',
)
const updatedFacets = await upgradeDiamond(deployments)
await saveDeploymentsFacets('deployments.json', network, updatedFacets)
},
)
task(
'upgrade-sr-diamond',
'Upgrades IPC Subnet Registry Diamond Facets on an EVM-compatible subnet using hardhat',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
const network = hre.network.name
const subnetRegistry = await getSubnetRegistry(network)
const { upgradeDiamond } = await lazyImport(
'./scripts/upgrade-sr-diamond',
)
const updatedFacets = await upgradeDiamond(subnetRegistry)
await saveDeploymentsFacets(
'subnet.registry.json',
network,
updatedFacets,
)
},
)
task(
'upgrade-sa-diamond',
'Upgrades IPC Subnet Actor Diamond Facets on an EVM-compatible subnet using hardhat',
async (args, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')
const network = hre.network.name
const deployments = await getSubnetActor(network)
const { upgradeDiamond } = await lazyImport(
'./scripts/upgrade-sa-diamond',
)
const updatedFacets = await upgradeDiamond(deployments)
await saveDeploymentsFacets('subnet.actor.json', network, updatedFacets)
},
)
/** @type import('hardhat/config').HardhatUserConfig */
const config: HardhatUserConfig = {
defaultNetwork: 'calibrationnet',
networks: {
mainnet: {
chainId: 314,
url: process.env.RPC_URL!,
accounts: [process.env.PRIVATE_KEY!],
timeout: 1000000,
},
calibrationnet: {
chainId: 314159,
url: process.env.RPC_URL!,
accounts: [process.env.PRIVATE_KEY!],
timeout: 1000000,
},
localnet: {
chainId: 31415926,
url: process.env.RPC_URL!,
accounts: [process.env.PRIVATE_KEY!],
},
// automatically fetch chainID for network
auto: {
chainId: parseInt(process.env.CHAIN_ID!, 16),
url: process.env.RPC_URL!,
accounts: [process.env.PRIVATE_KEY!],
// timeout to support also slow networks (like calibration/mainnet)
timeout: 1000000,
},
},
solidity: {
compilers: [
{
version: '0.8.19',
settings: {
viaIR: true,
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
typechain: {
outDir: 'typechain',
target: 'ethers-v5',
},
paths: {
storageLayouts: '.storage-layouts',
},
storageLayoutChanges: {
contracts: [
'GatewayDiamond',
'SubnetActorDiamond',
'GatewayActorModifiers',
'SubnetActorModifiers',
],
fullPath: false,
},
}
export default config