forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuidler.config.js
240 lines (206 loc) · 7.11 KB
/
buidler.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
'use strict';
const path = require('path');
const { gray, yellow } = require('chalk');
const { usePlugin, task, extendEnvironment } = require('@nomiclabs/buidler/config');
const { SOLC_OUTPUT_FILENAME } = require('@nomiclabs/buidler/internal/constants');
usePlugin('@nomiclabs/buidler-truffle5'); // uses and exposes web3 via buidler-web3 plugin
usePlugin('solidity-coverage');
usePlugin('buidler-ast-doc'); // compile ASTs for use with synthetix-docs
usePlugin('buidler-gas-reporter');
const { logContractSizes } = require('./publish/src/contract-size');
const {
constants: { inflationStartTimestampInSecs, AST_FILENAME, AST_FOLDER, BUILD_FOLDER },
} = require('.');
const {
DEFAULTS: { optimizerRuns },
} = require('./publish/src/commands/build');
const log = (...text) => console.log(gray(...['└─> [DEBUG]'].concat(text)));
const GAS_PRICE = 20e9; // 20 GWEI
const CACHE_FOLDER = 'cache';
const baseNetworkConfig = {
blockGasLimit: 0x1fffffffffffff,
initialDate: new Date(inflationStartTimestampInSecs * 1000).toISOString(),
gasPrice: GAS_PRICE,
// default to allow unlimited sized so that if we run buidler EVM in isolation (via npx buidler node)
// it will use this setting and allow any type of compiled contracts
allowUnlimitedContractSize: true,
};
extendEnvironment(bre => {
bre.log = log;
// NOTE: mutating bre.artifacts seems to cause issues with solidity-coverage, so adding
// "linkWithLegacySupport" to bre is a workaround
// base definition of legacy link support (no legacy support by default)
bre.linkWithLegacySupport = async (artifact, linkTo) => {
if (!bre.legacy) {
return artifact.link(await bre.artifacts.require(linkTo).new());
}
const originalContractName = artifact.contractName;
if (artifact.legacy) {
// This little hack is necessary as artifact.link will use the contractName to
// lookup the contract's bytecode and we need it
artifact.contractName += '_Legacy';
}
await artifact.link(
// link SafeDecimalMath - which will use legacy by default in legacy mode
// UNLESS this artifact is not a legacy one
await bre.artifacts.require(linkTo, { ignoreLegacy: !artifact.legacy }).new()
);
artifact.contractName = originalContractName;
};
// extend how contract testing works
const oldContractFnc = bre.contract;
bre.contract = (contract, cb) => {
oldContractFnc(contract, accounts => {
const oldRequire = bre.artifacts.require.bind(bre.artifacts);
// Prevent the contract undergoing testing from using the legacy source file
// (cause the tests are designed for the newer source, not the legacy)
before(() => {
if (bre.legacy) {
bre.artifacts.require = (name, opts = {}) => {
if (name === contract || opts.ignoreLegacy) {
return oldRequire(name);
}
try {
const artifact = oldRequire(name + '_Legacy');
artifact.legacy = true;
if (process.env.DEBUG) {
log('Using legacy source for', name);
}
return artifact;
} catch (err) {
return oldRequire(name);
}
};
}
});
after(() => {
bre.artifacts.require = oldRequire;
});
describe(
bre.legacy
? 'when integrating with legacy contracts'
: 'when integrating with modern contracts',
() => cb(accounts)
);
});
};
});
// Support for running the tests in "legacy" mode. This enabled the "legacy" flag in the buidler
// runtime environment (BRE) and tests can then load up _Legacy sources instead where required.
// Note: this assumes `npm run compile:legacy` has already been run (we can't run it from in here)
task('test:legacy', 'run the tests with legacy components')
.addOptionalVariadicPositionalParam('testFiles', 'An optional list of files to test', [])
.setAction(async (taskArguments, bre) => {
bre.legacy = true;
if (process.env.DEBUG) {
console.log(yellow('Legacy mode enabled.'));
}
await bre.run('test', taskArguments);
});
const optimizeIfRequired = ({ bre, taskArguments: { optimizer } }) => {
if (optimizer || bre.optimizer) {
// only show message once if re-run
if (bre.optimizer === undefined) {
console.log(gray('Adding optimizer, runs', yellow(optimizerRuns)));
}
// Use optimizer (slower) but simulates real contract size limits and gas usage
// Note: does not consider actual deployed optimization runs from
// publish/src/contract-overrides.js
bre.config.solc.optimizer = { enabled: true, runs: optimizerRuns };
bre.config.networks.buidlerevm.allowUnlimitedContractSize = false;
} else {
if (bre.optimizer === undefined) {
console.log(gray('Optimizer disabled. Unlimited contract sizes allowed.'));
}
bre.config.solc.optimizer = { enabled: false };
bre.config.networks.buidlerevm.allowUnlimitedContractSize = true;
}
// flag here so that if invoked via "buidler test" the argument will persist to the compile stage
bre.optimizer = !!optimizer;
};
task('compile')
.addFlag('showsize', 'Show size of compiled contracts')
.addFlag('optimizer', 'Compile with the optimizer')
.setAction(async (taskArguments, bre, runSuper) => {
optimizeIfRequired({ bre, taskArguments });
await runSuper(taskArguments);
if (taskArguments.showsize) {
const compiled = require(path.resolve(
__dirname,
BUILD_FOLDER,
CACHE_FOLDER,
SOLC_OUTPUT_FILENAME
));
const contracts = Object.entries(compiled.contracts).filter(([contractPath]) =>
/^contracts\/[\w]+.sol/.test(contractPath)
);
const contractToObjectMap = contracts.reduce(
(memo, [, entries]) =>
Object.assign(
{},
memo,
Object.entries(entries).reduce((_memo, [name, entry]) => {
_memo[name] = entry.evm.deployedBytecode.object;
return _memo;
}, {})
),
{}
);
logContractSizes({ contractToObjectMap });
}
});
task('test')
.addFlag('optimizer', 'Compile with the optimizer')
.addFlag('gas', 'Compile gas usage')
.addOptionalParam('grep', 'Filter tests to only those with given logic')
.setAction(async (taskArguments, bre, runSuper) => {
optimizeIfRequired({ bre, taskArguments });
const { gas, grep } = taskArguments;
if (grep) {
console.log(gray('Filtering tests to those containing'), yellow(grep));
bre.config.mocha.grep = grep;
}
if (gas) {
console.log(gray(`Enabling ${yellow('gas')} reports, tests will run slower`));
bre.config.gasReporter.enabled = true;
if (!grep) {
console.log(gray(`Ignoring test specs containing`, yellow('@gas-skip')));
bre.config.mocha.grep = '@gas-skip';
bre.config.mocha.invert = true;
}
}
await runSuper(taskArguments);
});
module.exports = {
GAS_PRICE,
solc: {
version: '0.5.16',
},
paths: {
sources: './contracts',
tests: './test/contracts',
artifacts: path.join(BUILD_FOLDER, 'artifacts'),
cache: path.join(BUILD_FOLDER, CACHE_FOLDER),
},
astdocs: {
path: path.join(BUILD_FOLDER, AST_FOLDER),
file: AST_FILENAME,
ignores: 'test-helpers',
},
networks: {
buidlerevm: baseNetworkConfig,
coverage: Object.assign(
{
url: 'http://localhost:8545',
allowUnlimitedContractSize: true,
},
baseNetworkConfig
),
},
gasReporter: {
enabled: false,
showTimeSpent: true,
currency: 'USD',
outputFile: 'test-gas-used.log',
},
};