forked from zama-ai/fhevm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hardhat.config.ts
245 lines (221 loc) · 7.16 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
import '@nomicfoundation/hardhat-toolbox';
import dotenv from 'dotenv';
import * as fs from 'fs';
import 'hardhat-deploy';
import 'hardhat-ignore-warnings';
import 'hardhat-preprocessor';
import { TASK_PREPROCESS } from 'hardhat-preprocessor';
import type { HardhatUserConfig, extendProvider } from 'hardhat/config';
import { task } from 'hardhat/config';
import type { NetworkUserConfig } from 'hardhat/types';
import { resolve } from 'path';
import * as path from 'path';
import CustomProvider from './CustomProvider';
// Adjust the import path as needed
import './tasks/accounts';
import './tasks/getEthereumAddress';
import './tasks/mint';
import './tasks/taskDeploy';
import './tasks/taskGatewayRelayer';
import './tasks/taskIdentity';
import './tasks/taskTFHE';
extendProvider(async (provider, config, network) => {
const newProvider = new CustomProvider(provider);
return newProvider;
});
// Function to recursively get all .sol files in a folder
function getAllSolidityFiles(dir: string, fileList: string[] = []): string[] {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
getAllSolidityFiles(filePath, fileList);
} else if (filePath.endsWith('.sol')) {
fileList.push(filePath);
}
});
return fileList;
}
task('compile:specific', 'Compiles only the specified contract')
.addParam('contract', "The contract's path")
.setAction(async ({ contract }, hre) => {
// Adjust the configuration to include only the specified contract
hre.config.paths.sources = contract;
await hre.run('compile');
});
task('coverage-mock', 'Run coverage after running pre-process task').setAction(async function (args, env) {
// Get all .sol files in the examples/ folder
const examplesPath = path.join(env.config.paths.root, 'examples/');
const solidityFiles = getAllSolidityFiles(examplesPath);
// Backup original files
const originalContents: Record<string, string> = {};
solidityFiles.forEach((filePath) => {
originalContents[filePath] = fs.readFileSync(filePath, { encoding: 'utf8' });
});
try {
// Run pre-process task
await env.run(TASK_PREPROCESS);
// Run coverage task
await env.run('coverage');
} finally {
// Restore original files
for (const filePath in originalContents) {
fs.writeFileSync(filePath, originalContents[filePath], { encoding: 'utf8' });
}
}
});
const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || './.env';
dotenv.config({ path: resolve(__dirname, dotenvConfigPath) });
// Ensure that we have all the environment variables we need.
const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
throw new Error('Please set your MNEMONIC in a .env file');
}
const network = process.env.HARDHAT_NETWORK;
function getRemappings() {
return fs
.readFileSync('remappings.txt', 'utf8')
.split('\n')
.filter(Boolean) // remove empty lines
.map((line: string) => line.trim().split('='));
}
const chainIds = {
zama: 8009,
local: 9000,
localNetwork1: 9000,
multipleValidatorTestnet: 8009,
};
function getChainConfig(chain: keyof typeof chainIds): NetworkUserConfig {
let jsonRpcUrl: string;
switch (chain) {
case 'local':
jsonRpcUrl = 'http://localhost:8545';
break;
case 'localNetwork1':
jsonRpcUrl = 'http://127.0.0.1:9650/ext/bc/fhevm/rpc';
break;
case 'multipleValidatorTestnet':
jsonRpcUrl = 'https://rpc.fhe-ethermint.zama.ai';
break;
case 'zama':
jsonRpcUrl = 'https://devnet.zama.ai';
break;
}
return {
accounts: {
count: 10,
mnemonic,
path: "m/44'/60'/0'/0",
},
chainId: chainIds[chain],
url: jsonRpcUrl,
};
}
task('test', async (taskArgs, hre, runSuper) => {
// Run modified test task
if (network === 'hardhat') {
// in fhevm mode all this block is done when launching the node via `pnmp fhevm:start`
const privKeyDeployer = process.env.PRIVATE_KEY_GATEWAY_DEPLOYER;
const privKeyOwner = process.env.PRIVATE_KEY_GATEWAY_OWNER;
const privKeyRelayer = process.env.PRIVATE_KEY_GATEWAY_RELAYER;
const deployerAddress = new hre.ethers.Wallet(privKeyDeployer!).address;
const ownerAddress = new hre.ethers.Wallet(privKeyOwner!).address;
const relayerAddress = new hre.ethers.Wallet(privKeyRelayer!).address;
await hre.run('task:computePredeployAddress', { privateKey: privKeyDeployer });
const bal = '0x1000000000000000000000000000000000000000';
const p1 = hre.network.provider.send('hardhat_setBalance', [deployerAddress, bal]);
const p2 = hre.network.provider.send('hardhat_setBalance', [ownerAddress, bal]);
const p3 = hre.network.provider.send('hardhat_setBalance', [relayerAddress, bal]);
await Promise.all([p1, p2, p3]);
await hre.run('compile');
await hre.run('task:deployGateway', { privateKey: privKeyDeployer, ownerAddress: ownerAddress });
const parsedEnv = dotenv.parse(fs.readFileSync('gateway/.env.gateway'));
const gatewayContractAddress = parsedEnv.GATEWAY_CONTRACT_PREDEPLOY_ADDRESS;
await hre.run('task:addRelayer', {
privateKey: privKeyOwner,
gatewayAddress: gatewayContractAddress,
relayerAddress: relayerAddress,
});
}
await runSuper();
});
const config: HardhatUserConfig = {
preprocess: {
eachLine: (hre) => ({
transform: (line: string) => {
if (network === 'hardhat') {
// checks if HARDHAT_NETWORK env variable is set to "hardhat" to use the remapping for the mocked version of TFHE.sol
if (line.match(/".*.sol";$/)) {
// match all lines with `"<any-import-path>.sol";`
for (const [from, to] of getRemappings()) {
if (line.includes(from)) {
line = line.replace(from, to);
break;
}
}
}
}
return line;
},
}),
},
defaultNetwork: 'local',
namedAccounts: {
deployer: 0,
},
mocha: {
timeout: 500000,
},
gasReporter: {
currency: 'USD',
enabled: process.env.REPORT_GAS ? true : false,
excludeContracts: [],
src: './examples',
},
networks: {
hardhat: {
accounts: {
count: 10,
mnemonic,
path: "m/44'/60'/0'/0",
},
},
zama: getChainConfig('zama'),
localDev: getChainConfig('local'),
local: getChainConfig('local'),
localNetwork1: getChainConfig('localNetwork1'),
multipleValidatorTestnet: getChainConfig('multipleValidatorTestnet'),
},
paths: {
artifacts: './artifacts',
cache: './cache',
sources: './examples',
tests: './test',
},
solidity: {
version: '0.8.24',
settings: {
metadata: {
// Not including the metadata hash
// https://github.com/paulrberg/hardhat-template/issues/31
bytecodeHash: 'none',
},
// Disable the optimizer when debugging
// https://hardhat.org/hardhat-network/#solidity-optimizer-support
optimizer: {
enabled: true,
runs: 800,
},
evmVersion: 'cancun',
},
},
warnings: {
'*': {
'transient-storage': false,
},
},
typechain: {
outDir: 'types',
target: 'ethers-v6',
},
};
export default config;