-
Notifications
You must be signed in to change notification settings - Fork 21
/
hardhat.config.ts
188 lines (154 loc) · 5.48 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
import "dotenv/config";
import { HardhatUserConfig, task, subtask, types } from "hardhat/config";
import "@nomicfoundation/hardhat-verify";
import "@nomicfoundation/hardhat-toolbox";
import "hardhat-gas-reporter";
import "hardhat-contract-sizer";
import "solidity-coverage";
import "hardhat-spdx-license-identifier";
import "hardhat-abi-exporter";
import "hardhat-storage-layout";
import "@openzeppelin/hardhat-upgrades";
import * as fs from "fs";
import "@typechain/hardhat";
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-chai-matchers";
const config: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.10",
settings: {
optimizer: {
enabled: true,
runs: 100,
},
},
},
],
},
networks: {
hardhat: {
accounts: {
accountsBalance: "100000000000000000000000000",
},
},
bsc: {
url: process.env.BSC_RPC || `https://bsc-dataseed.binance.org/`,
chainId: 56,
accounts: [`0x${process.env.DEPLOYER_PRIVATE_KEY}`],
},
bsc_testnet: {
url: process.env.BSC_TESTNET_RPC || `https://data-seed-prebsc-2-s3.binance.org:8545/`,
chainId: 97,
accounts: [`0x${process.env.DEPLOYER_PRIVATE_KEY}`],
gasPrice: 15000000000, // 15 gwei
},
},
etherscan: {
apiKey: {
bsc: process.env.BSC_API_KEY || "",
bscTestnet: process.env.BSC_API_KEY || "",
},
},
mocha: {
grep: "^(?!.*; using Ganache).*",
},
contractSizer: {
alphaSort: true,
runOnCompile: true,
disambiguatePaths: false,
},
gasReporter: {
enabled: process.env.REPORT_GAS ? true : false,
currency: "USD",
},
typechain: {
outDir: "typechain",
},
sourcify: {
enabled: true,
},
};
export default config;
function getSortedFiles(dependenciesGraph: any) {
const tsort = require("tsort");
const graph = tsort();
const filesMap: any = {};
const resolvedFiles = dependenciesGraph.getResolvedFiles();
resolvedFiles.forEach((f: any) => (filesMap[f.sourceName] = f));
for (const [from, deps] of dependenciesGraph.entries()) {
for (const to of deps) {
graph.add(to.sourceName, from.sourceName);
}
}
const topologicalSortedNames = graph.sort();
// If an entry has no dependency it won't be included in the graph, so we
// add them and then dedup the array
const withEntries: any = topologicalSortedNames.concat(resolvedFiles.map((f: any) => f.sourceName));
const sortedNames: any = new Set(withEntries).values();
return sortedNames.map((n: any) => filesMap[n]);
}
function getFileWithoutImports(resolvedFile: any) {
const IMPORT_SOLIDITY_REGEX = /^\s*import(\s+)[\s\S]*?;\s*$/gm;
return resolvedFile.content.rawContent.replace(IMPORT_SOLIDITY_REGEX, "").trim();
}
subtask("flat:get-flattened-sources", "Returns all contracts and their dependencies flattened")
.addOptionalParam("files", undefined, undefined, types.any)
.addOptionalParam("output", undefined, undefined, types.string)
.setAction(async ({ files, output }, { run }) => {
const dependencyGraph = await run("flat:get-dependency-graph", { files });
console.log(dependencyGraph);
let flattened = "";
if (dependencyGraph.getResolvedFiles().length === 0) {
return flattened;
}
const sortedFiles = getSortedFiles(dependencyGraph);
let isFirst = true;
for (const file of sortedFiles) {
if (!isFirst) {
flattened += "\n";
}
flattened += `// File ${file.getVersionedName()}\n`;
flattened += `${getFileWithoutImports(file)}\n`;
isFirst = false;
}
// Remove every line started with "// SPDX-License-Identifier:"
flattened = flattened.replace(/SPDX-License-Identifier:/gm, "License-Identifier:");
flattened = `// SPDX-License-Identifier: MIXED\n\n${flattened}`;
// Remove every line started with "pragma experimental ABIEncoderV2;" except the first one
flattened = flattened.replace(/pragma experimental ABIEncoderV2;\n/gm, ((i) => (m: any) => (!i++ ? m : ""))(0));
// Remove every line started with "pragma abicoder v2;" except the first one
flattened = flattened.replace(/pragma abicoder v2;\n/gm, ((i) => (m: any) => (!i++ ? m : ""))(0));
// Remove every line started with "pragma solidity ****" except the first one
flattened = flattened.replace(/pragma solidity .*$\n/gm, ((i) => (m: any) => (!i++ ? m : ""))(0));
flattened = flattened.trim();
if (output) {
console.log("Writing to", output);
fs.writeFileSync(output, flattened);
return "";
}
return flattened;
});
subtask("flat:get-dependency-graph")
.addOptionalParam("files", undefined, undefined, types.any)
.setAction(async ({ files }, { run }) => {
const sourcePaths =
files === undefined ? await run("compile:solidity:get-source-paths") : files.map((f: any) => fs.realpathSync(f));
const sourceNames = await run("compile:solidity:get-source-names", {
sourcePaths,
});
const dependencyGraph = await run("compile:solidity:get-dependency-graph", { sourceNames });
return dependencyGraph;
});
task("flat", "Flattens and prints contracts and their dependencies")
.addOptionalVariadicPositionalParam("files", "The files to flatten", undefined, types.inputFile)
.addOptionalParam("output", "Specify the output file", undefined, types.string)
.setAction(async ({ files, output }, { run }) => {
console.log(
await run("flat:get-flattened-sources", {
files,
output,
}),
);
});