forked from darkforest-eth/eth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.ts
136 lines (116 loc) · 3.66 KB
/
settings.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
import { AdminPlanets, Contracts, Initializers } from '@darkforest_eth/settings';
import toml from '@iarna/toml';
import chalk from 'chalk';
import { cosmiconfigSync } from 'cosmiconfig';
import * as decoders from 'decoders';
// HRE stuff
import 'hardhat/types/runtime';
import * as path from 'path';
import resolvePackage from 'resolve-package-path';
import { dedent } from 'ts-dedent';
declare module 'hardhat/types/runtime' {
interface HardhatRuntimeEnvironment {
DEPLOYER_MNEMONIC: string | undefined;
ADMIN_PUBLIC_ADDRESS: string | undefined;
packageDirs: {
'@darkforest_eth/contracts': string;
'@darkforest_eth/snarks': string;
};
contracts: Contracts;
initializers: Initializers;
adminPlanets: AdminPlanets;
}
}
// Util for parsing & validating schemas with pretty printing
export function parse<S>(guard: decoders.Guard<S>, data: unknown): ReturnType<decoders.Guard<S>> {
try {
return guard(data);
} catch (err) {
if (err instanceof Error) {
printValidationErrors(err);
} else {
console.log(err);
}
process.exit(1);
}
}
// A function that iterates over a Hardhat `lazyObject` to force them to be loaded.
//
// This is needed because some of our Schemas have required properties but aren't
// immediately validated due to `lazyObject`.
export function required<S extends { [key: string]: unknown }>(schema: S, keys: Array<keyof S>) {
const header = 'Required keys/values:';
const messages = keys.map((key, idx) => {
if (typeof key === 'string' || typeof key === 'number') {
return `${idx + 1}. ${key}: ${schema[key]}`;
} else {
console.error(chalk.red('Invalid key'), key);
process.exit(1);
}
});
const longest = messages.reduce((max, msg) => Math.max(msg.length, max), header.length);
const stars = '*'.repeat(longest);
const msg = dedent`
${stars}
${header}
${messages.join('\n')}
${stars}
`;
// We pretty much just log them so we have something to do with them.
console.log(chalk.green(msg));
}
function printValidationErrors(err: Error) {
const header = `Encountered configuration errors:`;
const message = err.message.trim();
const longest = err.message
.split('\n')
.reduce((max, msg) => Math.max(msg.length, max), header.length);
const stars = '*'.repeat(longest);
const msg = dedent`
${stars}
${header}
${message}
${stars}
`;
console.error(chalk.red(msg));
}
// Resolve workspace package directories
export function resolvePackageDir(pkg: string) {
const contractsPkg = resolvePackage(pkg, __dirname);
if (!contractsPkg) {
throw new Error(`Unable to find the ${pkg} package. Exiting...`);
}
return path.dirname(contractsPkg);
}
function tomlLoader(filename: string, content: string) {
try {
return toml.parse(content);
} catch (err) {
console.error(chalk.red(`Error parsing ${path.basename(filename)}`));
if (err instanceof Error) {
console.error(chalk.yellow(err.message));
}
process.exit(1);
}
}
const explorers: { [key: string]: ReturnType<typeof cosmiconfigSync> } = {};
export function load(network: string): { [key: string]: unknown } {
let explorer = explorers[network];
if (!explorer) {
// Config file loading stuff, cache it based on network key
explorer = explorers[network] = cosmiconfigSync('darkforest', {
cache: true,
searchPlaces: [`darkforest.${network}.toml`, 'darkforest.toml'],
loaders: {
'.toml': tomlLoader,
},
});
}
const result = explorer.search();
if (result) {
return result.config;
} else {
console.warn(chalk.yellow('Could not find `darkforest.toml` - using defaults.'));
return {};
}
}