-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
config-validation.ts
315 lines (270 loc) · 8.4 KB
/
config-validation.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
import * as t from "io-ts";
import { Context, getFunctionName, ValidationError } from "io-ts/lib";
import { Reporter } from "io-ts/lib/Reporter";
import { SUPPORTED_HARDFORKS } from "../../buidler-evm/provider/node";
import { BUIDLEREVM_NETWORK_NAME } from "../../constants";
import { BuidlerError } from "../errors";
import { ERRORS } from "../errors-list";
function stringify(v: any): string {
if (typeof v === "function") {
return getFunctionName(v);
}
if (typeof v === "number" && !isFinite(v)) {
if (isNaN(v)) {
return "NaN";
}
return v > 0 ? "Infinity" : "-Infinity";
}
return JSON.stringify(v);
}
function getContextPath(context: Context): string {
const keysPath = context
.slice(1)
.map(c => c.key)
.join(".");
return `${context[0].type.name}.${keysPath}`;
}
function getMessage(e: ValidationError): string {
const lastContext = e.context[e.context.length - 1];
return e.message !== undefined
? e.message
: getErrorMessage(
getContextPath(e.context),
e.value,
lastContext.type.name
);
}
function getErrorMessage(path: string, value: any, expectedType: string) {
return `Invalid value ${stringify(
value
)} for ${path} - Expected a value of type ${expectedType}.`;
}
export function failure(es: ValidationError[]): string[] {
return es.map(getMessage);
}
export function success(): string[] {
return [];
}
export const DotPathReporter: Reporter<string[]> = {
report: validation => validation.fold(failure, success)
};
function optional<TypeT, OutputT>(
codec: t.Type<TypeT, OutputT, unknown>,
name: string = `${codec.name} | undefined`
): t.Type<TypeT | undefined, OutputT | undefined, unknown> {
return new t.Type(
name,
(u: unknown): u is TypeT | undefined => u === undefined || codec.is(u),
(u, c) => (u === undefined ? t.success(u) : codec.validate(u, c)),
a => (a === undefined ? undefined : codec.encode(a))
);
}
// IMPORTANT: This t.types MUST be kept in sync with the actual types.
const BuidlerNetworkAccount = t.type({
privateKey: t.string,
balance: t.string
});
const BuidlerNetworkConfig = t.type({
hardfork: optional(t.string),
chainId: optional(t.number),
from: optional(t.string),
gas: optional(t.union([t.literal("auto"), t.number])),
gasPrice: optional(t.union([t.literal("auto"), t.number])),
gasMultiplier: optional(t.number),
accounts: optional(t.array(BuidlerNetworkAccount)),
blockGasLimit: optional(t.number),
throwOnTransactionFailures: optional(t.boolean),
throwOnCallFailures: optional(t.boolean)
});
const HDAccountsConfig = t.type({
mnemonic: t.string,
initialIndex: optional(t.number),
count: optional(t.number),
path: optional(t.string)
});
const OtherAccountsConfig = t.type({
type: t.string
});
const NetworkConfigAccounts = t.union([
t.literal("remote"),
t.array(t.string),
HDAccountsConfig,
OtherAccountsConfig
]);
const HttpNetworkConfig = t.type({
chainId: optional(t.number),
from: optional(t.string),
gas: optional(t.union([t.literal("auto"), t.number])),
gasPrice: optional(t.union([t.literal("auto"), t.number])),
gasMultiplier: optional(t.number),
url: optional(t.string),
accounts: optional(NetworkConfigAccounts)
});
const NetworkConfig = t.union([BuidlerNetworkConfig, HttpNetworkConfig]);
const Networks = t.record(t.string, NetworkConfig);
const ProjectPaths = t.type({
root: optional(t.string),
cache: optional(t.string),
artifacts: optional(t.string),
sources: optional(t.string),
tests: optional(t.string)
});
const EVMVersion = t.string;
const SolcOptimizerConfig = t.type({
enabled: optional(t.boolean),
runs: optional(t.number)
});
const SolcConfig = t.type({
version: optional(t.string),
optimizer: optional(SolcOptimizerConfig),
evmVersion: optional(EVMVersion)
});
const AnalyticsConfig = t.type({
enabled: optional(t.boolean)
});
const BuidlerConfig = t.type(
{
defaultNetwork: optional(t.string),
networks: optional(Networks),
paths: optional(ProjectPaths),
solc: optional(SolcConfig),
analytics: optional(AnalyticsConfig)
},
"BuidlerConfig"
);
/**
* Validates the config, throwing a BuidlerError if invalid.
* @param config
*/
export function validateConfig(config: any) {
const errors = getValidationErrors(config);
if (errors.length === 0) {
return;
}
let errorList = errors.join("\n * ");
errorList = ` * ${errorList}`;
throw new BuidlerError(ERRORS.GENERAL.INVALID_CONFIG, { errors: errorList });
}
export function getValidationErrors(config: any): string[] {
const errors = [];
// These can't be validated with io-ts
if (config !== undefined && typeof config.networks === "object") {
const buidlerNetwork = config.networks[BUIDLEREVM_NETWORK_NAME];
if (buidlerNetwork !== undefined) {
if (
buidlerNetwork.hardfork !== undefined &&
!SUPPORTED_HARDFORKS.includes(buidlerNetwork.hardfork)
) {
errors.push(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.hardfork is not supported. Use one of ${SUPPORTED_HARDFORKS.join(
", "
)}`
);
}
if (
buidlerNetwork.throwOnTransactionFailures !== undefined &&
typeof buidlerNetwork.throwOnTransactionFailures !== "boolean"
) {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.throwOnTransactionFailures`,
buidlerNetwork.throwOnTransactionFailures,
"boolean | undefined"
)
);
}
if (
buidlerNetwork.throwOnCallFailures !== undefined &&
typeof buidlerNetwork.throwOnCallFailures !== "boolean"
) {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.throwOnCallFailures`,
buidlerNetwork.throwOnCallFailures,
"boolean | undefined"
)
);
}
if (buidlerNetwork.url !== undefined) {
errors.push(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME} can't have an url`
);
}
if (
buidlerNetwork.blockGasLimit !== undefined &&
typeof buidlerNetwork.blockGasLimit !== "number"
) {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.blockGasLimit`,
buidlerNetwork.blockGasLimit,
"number | undefined"
)
);
}
if (buidlerNetwork.accounts !== undefined) {
if (Array.isArray(buidlerNetwork.accounts)) {
for (const account of buidlerNetwork.accounts) {
if (typeof account.privateKey !== "string") {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.accounts[].privateKey`,
account.privateKey,
"string"
)
);
}
if (typeof account.balance !== "string") {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.accounts[].balance`,
account.balance,
"string"
)
);
}
}
} else {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${BUIDLEREVM_NETWORK_NAME}.accounts`,
buidlerNetwork.accounts,
"[{privateKey: string, balance: string}] | undefined"
)
);
}
}
}
for (const [networkName, netConfig] of Object.entries<any>(
config.networks
)) {
if (networkName === BUIDLEREVM_NETWORK_NAME) {
continue;
}
if (networkName === "localhost" && netConfig.url === undefined) {
continue;
}
if (typeof netConfig.url !== "string") {
errors.push(
getErrorMessage(
`BuidlerConfig.networks.${networkName}.url`,
netConfig.url,
"string"
)
);
}
}
}
// io-ts can get confused if there are errors that it can't understand.
// Especially around BuidlerEVM's config. It will treat it as an HTTPConfig,
// and may give a loot of errors.
if (errors.length > 0) {
return errors;
}
const result = BuidlerConfig.decode(config);
if (result.isRight()) {
return errors;
}
const ioTsErrors = DotPathReporter.report(result);
return [...errors, ...ioTsErrors];
}