-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhardhat.config.js
302 lines (264 loc) · 7.79 KB
/
hardhat.config.js
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
const fs = require("fs");
const path = require('path');
require("@nomiclabs/hardhat-ethers");
require("@nomicfoundation/hardhat-foundry");
require("dotenv-defaults").config();
require("./tasks");
const { createProvider } = require("hardhat/internal/core/providers/construction");
const { getForgeConfig } = require("@nomicfoundation/hardhat-foundry/dist/src/foundry");
const {
glob
} = require('glob');
const foundry = getForgeConfig();
let accounts;
if (process.env.PRIVATE_KEY) {
accounts = [process.env.PRIVATE_KEY];
} else {
accounts = {
mnemonic:
process.env.MNEMONIC ||
"test test test test test test test test test test test junk",
};
}
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
foundry,
defaultNetwork: "mainnet",
solidity: {
compilers: [
{
version: foundry.solc,
settings: {
optimizer: {
enabled: foundry.optimizer,
runs: foundry.optimizer_runs
}
}
}
]
},
namedAccounts: {
deployer: {
default: 0,
}
},
networks: {
mainnet: {
url: process.env.MAINNET_RPC_URL,
api_key: process.env.MAINNET_ETHERSCAN_KEY,
chainId: 1,
accounts
},
bsc: {
url: process.env.BSC_RPC_URL,
api_key: process.env.BSC_ETHERSCAN_KEY,
chainId: 56,
accounts
},
avalanche: {
url: process.env.AVALANCHE_RPC_URL,
api_key: process.env.AVALANCHE_ETHERSCAN_KEY,
chainId: 43114,
accounts
},
polygon: {
url: process.env.POLYGON_RPC_URL,
api_key: process.env.POLYGON_ETHERSCAN_KEY,
chainId: 137,
accounts
},
arbitrum: {
url: process.env.ARBITRUM_RPC_URL,
api_key: process.env.ARBITRUM_ETHERSCAN_KEY,
chainId: 42161,
accounts,
forgeDeployExtraArgs: "--legacy"
},
optimism: {
url: process.env.OPTIMISM_RPC_URL,
api_key: process.env.OPTIMISM_ETHERSCAN_KEY,
chainId: 10,
accounts,
forgeDeployExtraArgs: "--legacy"
},
fantom: {
url: process.env.FANTOM_RPC_URL,
api_key: process.env.FTMSCAN_ETHERSCAN_KEY,
chainId: 250,
accounts
},
moonriver: {
url: process.env.MOONRIVER_RPC_URL,
api_key: process.env.MOONRIVER_ETHERSCAN_KEY,
chainId: 1285,
accounts
},
kava: {
url: process.env.KAVA_RPC_URL,
chainId: 2222,
lzChainId: 177,
accounts,
profile: "evm_paris",
forgeDeployExtraArgs: "--legacy"
},
linea: {
url: process.env.LINEA_RPC_URL,
api_key: process.env.LINEA_ETHERSCAN_KEY,
chainId: 59144,
accounts
},
base: {
url: process.env.BASE_RPC_URL,
api_key: process.env.BASE_ETHERSCAN_KEY,
chainId: 8453,
accounts
}
}
};
extendEnvironment((hre) => {
const providers = {};
hre.getNetworkConfigByName = (name) => {
return hre.config.networks[name];
};
hre.getNetworkConfigByChainId = (chainId) => {
const config = hre.findNetworkConfig((config) => config.chainId === chainId);
if (!config) {
console.error(`ChainId: ${chainId} not found in hardhat.config.js`);
process.exit(1);
}
return config;
};
hre.findNetworkConfig = (predicate) => {
// loop thru all hre.config.networks and find the one with the matching chainId
for (const [name, config] of Object.entries(hre.config.networks)) {
if (predicate(config)) {
return {
name,
...config
}
}
}
};
hre.getChainIdByNetworkName = (name) => {
return getNetworkConfigByName(name).chainId;
};
hre.getArtifact = async (artifact) => {
const [filepath, name] = artifact.split(':');
const file = `./${foundry.out}/${path.basename(filepath)}/${name}.json`;
if (!fs.existsSync(file)) {
console.error(`Artifact ${artifact} not found (${file})`);
process.exit(1);
}
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
hre.deploymentExists = (name, chainId) => {
return fs.existsSync(`./deployments/${chainId}/${name}.json`);
}
hre.getDeployment = async (name, chainId) => {
const file = `./deployments/${chainId}/${name}.json`;
if (!fs.existsSync(file)) {
console.error(`ChainId: ${chainId} does not have a deployment for ${name}. (${file} not found)`)
process.exit(1);
}
return JSON.parse(fs.readFileSync(file, 'utf8'));
};
hre.getAllDeploymentsByChainId = async (chainId) => {
const files = await glob(`./deployments/${chainId}/*.json`);
return files.map(file => {
return {
__extra: {
name: path.basename(file),
path: file
},
...JSON.parse(fs.readFileSync(file, 'utf8'))
}
});
}
hre.getAbi = async (artifactName) => {
const file = (await glob(`${foundry.out}/**/${artifactName}.json`))[0];
if (!file) {
console.error(`Artifact ${artifactName} not found inside ${foundry.out}/ folder`);
process.exit(1);
}
return (JSON.parse(fs.readFileSync(file, 'utf8'))).abi;
};
hre.getSigners = async () => {
return await hre.ethers.getSigners();
};
hre.getDeployer = async () => {
return (await getSigners())[0];
};
hre.getContractAt = async (artifactName, address) => {
const signer = (await getSigners())[0];
const abi = await getAbi(artifactName);
return await ethers.getContractAt(abi, address, signer);
};
hre.getContract = async (name, chainId) => {
const previousNetwork = getNetworkConfigByChainId(hre.network.config.chainId);
const currentNetwork = getNetworkConfigByChainId(chainId);
chainId = chainId || previousNetwork.chainId;
if (!chainId) {
console.error("No network specified, use `changeNetwork` to switch network or specify --network parameter.");
process.exit(1);
}
if (chainId != previousNetwork.chainId) {
await hre.changeNetwork(currentNetwork.name);
}
const deployment = await getDeployment(name, chainId);
const signer = (await hre.ethers.getSigners())[0];
const contract = await ethers.getContractAt(deployment.abi, deployment.address, signer);
if (chainId != previousNetwork.chainId) {
await hre.changeNetwork(previousNetwork.name);
}
return contract;
};
// create all network providers so it's easy to switch between them.
// Adapted from https://github.com/dmihal/hardhat-change-network/
hre.getProvider = (name) => {
if (!providers[name]) {
providers[name] = createProvider(
name,
hre.config.networks[name],
hre.config.paths,
hre.artifacts,
);
}
return providers[name];
};
hre.changeNetwork = (networkName) => {
if (!hre.config.networks[networkName]) {
throw new Error(`changeNetwork: Couldn't find network '${networkName}'`);
}
if (!providers[hre.network.name]) {
providers[hre.network.name] = hre.network.provider;
}
hre.network.name = networkName;
hre.network.config = hre.config.networks[networkName];
if (!providers[networkName]) {
providers[networkName] = createProvider(
networkName,
hre.config.networks[networkName],
hre.config.paths,
hre.artifacts,
);
}
hre.network.provider = providers[networkName];
if (hre.ethers) {
const { EthersProviderWrapper } = require("@nomiclabs/hardhat-ethers/internal/ethers-provider-wrapper");
hre.ethers.provider = new EthersProviderWrapper(hre.network.provider);
}
};
delete hre.config.networks.hardhat;
delete hre.config.networks.localhost;
// remove hardhat core tasks
delete hre.tasks.compile;
delete hre.tasks.test;
delete hre.tasks.run;
delete hre.tasks.clean;
delete hre.tasks.accounts;
delete hre.tasks.console;
delete hre.tasks.node;
delete hre.tasks.check;
delete hre.tasks.flatten;
delete hre.tasks["init-foundry"]
});