/
build.js
233 lines (200 loc) · 6.63 KB
/
build.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
'use strict';
const path = require('path');
const fs = require('fs');
const { gray, green, yellow, red } = require('chalk');
const { findSolFiles, flatten, compile } = require('../solidity');
const {
constants: { COMPILED_FOLDER, CONTRACTS_FOLDER, FLATTENED_FOLDER, BUILD_FOLDER },
ovmIgnored,
} = require('../../..');
const { stringify } = require('../util');
const {
sizeOfContracts,
logContractSizes,
pcentToColorFnc,
sizeChange,
} = require('../contract-size');
const DEFAULTS = {
buildPath: path.join(__dirname, '..', '..', '..', BUILD_FOLDER),
optimizerRuns: 200,
};
const overrides = require('../contract-overrides');
const build = async ({
buildPath = DEFAULTS.buildPath,
optimizerRuns = DEFAULTS.optimizerRuns,
skipUnchanged,
testHelpers,
showWarnings,
showContractSize,
useOvm,
} = {}) => {
console.log(gray(`Starting build${useOvm ? ' using OVM' : ''}...`));
if (!fs.existsSync(buildPath)) {
fs.mkdirSync(buildPath);
}
// Flatten all the contracts.
// Start with the libraries, then copy our own contracts on top to ensure
// if there's a naming clash our code wins.
console.log(gray('Finding .sol files...'));
const libraries = findSolFiles({ sourcePath: 'node_modules' });
const contracts = findSolFiles({
sourcePath: CONTRACTS_FOLDER,
ignore: [].concat(!testHelpers ? /^test-helpers\// : []),
});
if (useOvm) {
console.log(gray(` Sources to be ignored for OVM compilation (see publish/ovm-ignore.json):`));
const contractPaths = Object.keys(contracts);
contractPaths.map(contractPath => {
const filename = path.basename(contractPath, '.sol');
const isIgnored = ovmIgnored.some(ignored => filename === ignored);
if (isIgnored) {
console.log(gray(` > ${filename}`));
delete contracts[contractPath];
}
});
}
const allSolFiles = { ...libraries, ...contracts };
console.log(
gray(
`Found ${Object.keys(contracts).length} sources, and ${
Object.keys(libraries).length
} possible libraries`
)
);
console.log(gray('Flattening contracts...'));
const sources = await flatten({ files: allSolFiles, contracts });
const unchangedContracts = [];
const flattenedPath = path.join(buildPath, FLATTENED_FOLDER);
Object.entries(sources).forEach(([key, { content }]) => {
const toWrite = path.join(flattenedPath, key);
try {
// try make path for sub-folders (note: recursive flag only from nodejs 10.12.0)
fs.mkdirSync(path.dirname(toWrite), { recursive: true });
} catch (e) {}
// open existing if any
if (fs.existsSync(toWrite)) {
const existing = fs.readFileSync(toWrite).toString();
if (content === existing) {
unchangedContracts.push(key);
}
}
fs.writeFileSync(toWrite, content);
});
const compiledPath = path.join(buildPath, COMPILED_FOLDER);
// Ok, now we need to compile all the files.
console.log(gray(`Compiling contracts... Default optimizer runs is set to ${optimizerRuns}`));
let allErrors = [];
let allWarnings = [];
const allArtifacts = {};
const allCompiledFilePaths = [];
const previousSizes = [];
for (const contract of Object.keys(sources)) {
const contractName = contract
.match(/^.+(?=\.sol$)/)[0]
.split('/')
.slice(-1)[0];
const toWrite = path.join(compiledPath, contractName);
const filePath = `${toWrite}.json`;
const prevSizeIfAny = fs.existsSync(filePath)
? await sizeOfContracts({
contractToObjectMap: { [filePath]: require(filePath).evm.deployedBytecode.object },
})[0]
: undefined;
if (prevSizeIfAny) {
previousSizes.push(prevSizeIfAny);
}
let runs = optimizerRuns; // default
if (typeof overrides[contract] === 'object') {
runs = overrides[contract].runs;
}
console.log(
gray(
`Attempting compile of ${contract}${
runs !== optimizerRuns ? ` (override optimizerRuns: ${runs})` : ''
}`
)
);
if (skipUnchanged && unchangedContracts.indexOf(contract) >= 0) {
console.log(
gray(
'\tSource unchanged. Assuming that last deploy completed and skipping. (⚠⚠⚠ Do not use for production deploys!).'
)
);
continue;
}
const { artifacts, errors, warnings } = compile({
sources: {
[contract]: sources[contract],
},
runs,
useOvm,
});
Object.assign(allArtifacts, artifacts);
allErrors = allErrors.concat(errors);
allWarnings = allWarnings.concat(warnings);
if (warnings.length && showWarnings) {
console.log(gray(warnings.map(({ formattedMessage }) => formattedMessage).join('\n')));
}
if (errors.length) {
console.log(red(`${contract} errors detected`));
console.log(red(errors.map(({ formattedMessage }) => formattedMessage)));
// now in order to ensure that it does not flag skip unchanged, delete the flattened file
fs.unlinkSync(path.join(flattenedPath, contract));
} else {
try {
// try make path for sub-folders (note: recursive flag only from nodejs 10.12.0)
fs.mkdirSync(path.dirname(toWrite), { recursive: true });
} catch (e) {}
fs.writeFileSync(filePath, stringify(artifacts[contractName]));
const { pcent, bytes, length } = sizeOfContracts({
contractToObjectMap: { [filePath]: artifacts[contractName].evm.deployedBytecode.object },
})[0];
console.log(
green(`${contract}`),
gray('build using'),
pcentToColorFnc({ pcent, content: `${bytes} (${pcent})` }),
sizeChange({ prevSizeIfAny, length })
);
allCompiledFilePaths.push(filePath);
}
}
console.log(
(allErrors.length > 0 ? red : yellow)(
`Compiled with ${allWarnings.length} warnings and ${allErrors.length} errors`
)
);
// We're built!
console.log(green('Build succeeded'));
if (showContractSize) {
const contractToObjectMap = allCompiledFilePaths
.filter(file => fs.existsSync(file))
.reduce((memo, file) => {
memo[file] = require(file).evm.deployedBytecode.object;
return memo;
}, {});
logContractSizes({ previousSizes, contractToObjectMap });
}
};
module.exports = {
build,
DEFAULTS,
cmd: program =>
program
.command('build')
.description('Build (flatten and compile) solidity files')
.option('-b, --build-path <value>', 'Build path for built files', DEFAULTS.buildPath)
.option(
'-k, --skip-unchanged',
'Skip any contracts that seem as though they have not changed (infers from flattened file and does not strictly check bytecode. ⚠⚠⚠ DO NOT USE FOR PRODUCTION BUILDS.'
)
.option(
'-o, --optimizer-runs <value>',
'Number of runs for the optimizer by default',
DEFAULTS.optimizerRuns
)
.option('-s, --show-contract-size', 'Show contract sizes')
.option('-t, --test-helpers', 'Also compile the test-helpers')
.option('-w, --show-warnings', 'Show warnings')
.option('-z, --use-ovm', 'Use Optimism OVM-compatible compiler')
.action(build),
};