forked from darkforest-eth/eth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.ts
More file actions
361 lines (324 loc) · 10.4 KB
/
settings.ts
File metadata and controls
361 lines (324 loc) · 10.4 KB
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
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: ReturnType<typeof Contracts>;
initializers: ReturnType<typeof Initializers>;
adminPlanets: ReturnType<typeof AdminPlanets>;
}
}
export const Contracts = decoders.guard(
decoders.exact({
/**
* Network information
*/
NETWORK: decoders.string,
NETWORK_ID: decoders.number,
START_BLOCK: decoders.number,
/**
* Library addresses
*/
UTILS_LIBRARY_ADDRESS: decoders.string,
PLANET_LIBRARY_ADDRESS: decoders.string,
ARTIFACT_UTILS_LIBRARY_ADDRESS: decoders.string,
VERIFIER_LIBRARY_ADDRESS: decoders.string,
INITIALIZE_LIBRARY_ADDRESS: decoders.string,
LAZY_UPDATE_LIBRARY_ADDRESS: decoders.string,
/**
* Contract addresses
*/
CORE_CONTRACT_ADDRESS: decoders.string,
TOKENS_CONTRACT_ADDRESS: decoders.string,
GETTERS_CONTRACT_ADDRESS: decoders.string,
WHITELIST_CONTRACT_ADDRESS: decoders.string,
}),
{ style: 'simple' }
);
type PlanetTypeWeights = ExactArray4<ExactArray10<ExactArray5<number>>>;
// Handle Date or ISO8601 strings because the TOML parser converts to Date already
const dateInSeconds = decoders.map(decoders.either(decoders.date, decoders.iso8601), (val) =>
Math.floor(val.getTime() / 1000)
);
export const Initializers = decoders.guard(
decoders.exact({
ADMIN_CAN_ADD_PLANETS: withDefault(decoders.boolean, false),
WORLD_RADIUS_LOCKED: withDefault(decoders.boolean, false),
TOKEN_MINT_END_TIMESTAMP: withDefault(dateInSeconds, oneYearFromNow()),
TARGET4_RADIUS: withDefault(decoders.number, 800),
INITIAL_WORLD_RADIUS: withDefault(decoders.number, 8000),
/**
* SNARK keys & Perlin parameters
*/
DISABLE_ZK_CHECKS: withDefault(decoders.boolean, false),
PLANETHASH_KEY: decoders.number,
SPACETYPE_KEY: decoders.number,
BIOMEBASE_KEY: decoders.number,
PERLIN_MIRROR_X: withDefault(decoders.boolean, false),
PERLIN_MIRROR_Y: withDefault(decoders.boolean, false),
PERLIN_LENGTH_SCALE: withDefault(decoders.number, 8192), // must be power of two at most 8192
/**
* Game configuration
*/
MAX_NATURAL_PLANET_LEVEL: withDefault(decoders.number, 256),
TIME_FACTOR_HUNDREDTHS: withDefault(decoders.number, 100),
PERLIN_THRESHOLD_1: withDefault(decoders.number, 13),
PERLIN_THRESHOLD_2: withDefault(decoders.number, 15),
PERLIN_THRESHOLD_3: withDefault(decoders.number, 18),
INIT_PERLIN_MIN: withDefault(decoders.number, 12),
INIT_PERLIN_MAX: withDefault(decoders.number, 13),
BIOME_THRESHOLD_1: withDefault(decoders.number, 15),
BIOME_THRESHOLD_2: withDefault(decoders.number, 17),
PLANET_RARITY: withDefault(decoders.number, 16384),
PHOTOID_ACTIVATION_DELAY: withDefault(decoders.number, 60 * 60 * 12),
SPAWN_RIM_AREA: withDefault(decoders.number, 0),
LOCATION_REVEAL_COOLDOWN: withDefault(decoders.number, 60 * 60 * 24),
CLAIM_PLANET_COOLDOWN: withDefault(decoders.number, 60 * 60 * 3),
PLANET_TYPE_WEIGHTS: withDefault<PlanetTypeWeights>(
exactArray4(exactArray10(exactArray5(between(decoders.number, 0, 255)))),
[
[
[1, 0, 0, 0, 0],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
[13, 2, 0, 0, 1],
],
[
[1, 0, 0, 0, 0],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
[12, 2, 1, 0, 1],
],
[
[1, 0, 0, 0, 0],
[10, 4, 1, 0, 1],
[10, 4, 1, 0, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
],
[
[1, 0, 0, 0, 0],
[10, 4, 1, 0, 1],
[10, 4, 1, 0, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
[8, 4, 1, 2, 1],
],
]
),
ARTIFACT_POINT_VALUES: withDefault<Tuple6<number>>(
array6(decoders.number),
[0, 2000, 10000, 200000, 3000000, 20000000]
),
}),
{ style: 'simple' }
);
const AdminPlanet = decoders.exact({
x: decoders.number,
y: decoders.number,
level: decoders.number,
planetType: decoders.number,
requireValidLocationId: decoders.boolean,
revealLocation: decoders.boolean,
});
export const AdminPlanets = decoders.guard(decoders.array(AdminPlanet), { style: 'simple' });
// 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 {};
}
}
// Decoder helpers that will probably be refactored into a package
function withDefault<A>(decoder: decoders.Decoder<A>, def: A) {
return decoders.map(decoders.optional(decoder), (val) => {
if (val === undefined) {
return def;
} else {
return val;
}
});
}
function between(decoder: decoders.Decoder<number>, min: number, max: number) {
return decoders.compose(
decoder,
decoders.predicate((val) => val >= min && val <= max, `Must be between ${min} and ${max}`)
);
}
type ExactArray4<A> = [A, A, A, A];
function exactArray4<A>(decoder: decoders.Decoder<A>) {
return decoders.map(
decoders.compose(
decoders.array(decoder),
decoders.predicate((arr) => arr.length === 4, `Must be exactly 4-length`)
),
(value) => [value[0], value[1], value[2], value[3]] as ExactArray4<A>
);
}
type ExactArray5<A> = [A, A, A, A, A];
function exactArray5<A>(decoder: decoders.Decoder<A>) {
return decoders.map(
decoders.compose(
decoders.array(decoder),
decoders.predicate((arr) => arr.length === 5, `Must be exactly 5-length`)
),
(value) => [value[0], value[1], value[2], value[3], value[4]] as ExactArray5<A>
);
}
type Tuple6<A> = [A, A, A, A, A, A];
function array6<A>(decoder: decoders.Decoder<A>) {
return decoders.map(
decoders.compose(
decoders.array(decoder),
decoders.predicate((arr) => arr.length === 6, `Must be exactly 6-length`)
),
(value) => [value[0], value[1], value[2], value[3], value[4], value[5]] as Tuple6<A>
);
}
type ExactArray10<A> = [A, A, A, A, A, A, A, A, A, A];
function exactArray10<A>(decoder: decoders.Decoder<A>) {
return decoders.map(
decoders.compose(
decoders.array(decoder),
decoders.predicate((arr) => arr.length === 10, `Must be exactly 10-length`)
),
(value) =>
[
value[0],
value[1],
value[2],
value[3],
value[4],
value[5],
value[6],
value[7],
value[8],
value[9],
] as ExactArray10<A>
);
}
// Generates the default for TOKEN_MINT_END_TIMESTAMP
function oneYearFromNow() {
const oneYear = 60 * 60 * 24 * 365 * 1000;
// The default doesn't get passed through the transform so we still need to divide by 1000
return Math.floor((Date.now() + oneYear) / 1000);
}