1 | 'use strict';
|
2 |
|
3 | const w3utils = require('web3-utils');
|
4 | const abiDecoder = require('abi-decoder');
|
5 |
|
6 |
|
7 | const data = {
|
8 | kovan: require('./publish/deployed/kovan'),
|
9 | rinkeby: require('./publish/deployed/rinkeby'),
|
10 | ropsten: require('./publish/deployed/ropsten'),
|
11 | mainnet: require('./publish/deployed/mainnet'),
|
12 | goerli: require('./publish/deployed/goerli'),
|
13 | 'goerli-ovm': require('./publish/deployed/goerli-ovm'),
|
14 | };
|
15 |
|
16 | const assets = require('./publish/assets.json');
|
17 | const ovmIgnored = require('./publish/ovm-ignore.json');
|
18 |
|
19 | const networks = ['local', 'kovan', 'rinkeby', 'ropsten', 'mainnet', 'goerli'];
|
20 |
|
21 | const networkToChainId = {
|
22 | mainnet: 1,
|
23 | ropsten: 3,
|
24 | rinkeby: 4,
|
25 | goerli: 5,
|
26 | kovan: 42,
|
27 | };
|
28 |
|
29 | const constants = {
|
30 | BUILD_FOLDER: 'build',
|
31 | CONTRACTS_FOLDER: 'contracts',
|
32 | COMPILED_FOLDER: 'compiled',
|
33 | FLATTENED_FOLDER: 'flattened',
|
34 | AST_FOLDER: 'ast',
|
35 |
|
36 | CONFIG_FILENAME: 'config.json',
|
37 | PARAMS_FILENAME: 'params.json',
|
38 | SYNTHS_FILENAME: 'synths.json',
|
39 | STAKING_REWARDS_FILENAME: 'rewards.json',
|
40 | OWNER_ACTIONS_FILENAME: 'owner-actions.json',
|
41 | DEPLOYMENT_FILENAME: 'deployment.json',
|
42 | VERSIONS_FILENAME: 'versions.json',
|
43 | FEEDS_FILENAME: 'feeds.json',
|
44 | OVM_IGNORE_FILENAME: 'ovm-ignore.json',
|
45 |
|
46 | AST_FILENAME: 'asts.json',
|
47 |
|
48 | ZERO_ADDRESS: '0x' + '0'.repeat(40),
|
49 |
|
50 | inflationStartTimestampInSecs: 1551830400,
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 | const defaults = {
|
56 | WAITING_PERIOD_SECS: (60 * 5).toString(),
|
57 | PRICE_DEVIATION_THRESHOLD_FACTOR: w3utils.toWei('3'),
|
58 | TRADING_REWARDS_ENABLED: false,
|
59 | ISSUANCE_RATIO: w3utils
|
60 | .toBN(1)
|
61 | .mul(w3utils.toBN(1e18))
|
62 | .div(w3utils.toBN(6))
|
63 | .toString(),
|
64 | FEE_PERIOD_DURATION: (3600 * 24 * 7).toString(),
|
65 | TARGET_THRESHOLD: '1',
|
66 | LIQUIDATION_DELAY: (3600 * 24 * 3).toString(),
|
67 | LIQUIDATION_RATIO: w3utils.toWei('0.5'),
|
68 | LIQUIDATION_PENALTY: w3utils.toWei('0.1'),
|
69 | RATE_STALE_PERIOD: (3600 * 25).toString(),
|
70 | EXCHANGE_FEE_RATES: {
|
71 | forex: w3utils.toWei('0.003'),
|
72 | commodity: w3utils.toWei('0.003'),
|
73 | equities: w3utils.toWei('0.003'),
|
74 | crypto: w3utils.toWei('0.01'),
|
75 | index: w3utils.toWei('0.01'),
|
76 | },
|
77 | MINIMUM_STAKE_TIME: (3600 * 24).toString(),
|
78 | DEBT_SNAPSHOT_STALE_TIME: (43800).toString(),
|
79 | AGGREGATOR_WARNING_FLAGS: {
|
80 | mainnet: '0x4A5b9B4aD08616D11F3A402FF7cBEAcB732a76C6',
|
81 | kovan: '0x6292aa9a6650ae14fbf974e5029f36f95a1848fd',
|
82 | },
|
83 | INITIAL_ISSUANCE: w3utils.toWei(`${100e6}`),
|
84 | };
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | const toBytes32 = key => w3utils.rightPad(w3utils.asciiToHex(key), 64);
|
90 |
|
91 | const getFolderNameForNetwork = ({ network, useOvm = false }) => {
|
92 | if (network.includes('ovm')) {
|
93 | return network;
|
94 | }
|
95 |
|
96 | return useOvm ? `${network}-ovm` : network;
|
97 | };
|
98 |
|
99 | const getPathToNetwork = ({ network = 'mainnet', file = '', useOvm = false, path } = {}) =>
|
100 | path.join(__dirname, 'publish', 'deployed', getFolderNameForNetwork({ network, useOvm }), file);
|
101 |
|
102 |
|
103 | const loadDeploymentFile = ({ network, path, fs, deploymentPath, useOvm = false }) => {
|
104 | if (!deploymentPath && network !== 'local' && (!path || !fs)) {
|
105 | return data[getFolderNameForNetwork({ network, useOvm })].deployment;
|
106 | }
|
107 | const pathToDeployment = deploymentPath
|
108 | ? path.join(deploymentPath, constants.DEPLOYMENT_FILENAME)
|
109 | : getPathToNetwork({ network, useOvm, path, file: constants.DEPLOYMENT_FILENAME });
|
110 |
|
111 | if (!fs.existsSync(pathToDeployment)) {
|
112 | throw Error(`Cannot find deployment for network: ${network}.`);
|
113 | }
|
114 | return JSON.parse(fs.readFileSync(pathToDeployment));
|
115 | };
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | const getTarget = ({
|
121 | network = 'mainnet',
|
122 | useOvm = false,
|
123 | contract,
|
124 | path,
|
125 | fs,
|
126 | deploymentPath,
|
127 | } = {}) => {
|
128 | const deployment = loadDeploymentFile({ network, useOvm, path, fs, deploymentPath });
|
129 | if (contract) return deployment.targets[contract];
|
130 | else return deployment.targets;
|
131 | };
|
132 |
|
133 |
|
134 |
|
135 |
|
136 | const getSource = ({
|
137 | network = 'mainnet',
|
138 | useOvm = false,
|
139 | contract,
|
140 | path,
|
141 | fs,
|
142 | deploymentPath,
|
143 | } = {}) => {
|
144 | const deployment = loadDeploymentFile({ network, useOvm, path, fs, deploymentPath });
|
145 | if (contract) return deployment.sources[contract];
|
146 | else return deployment.sources;
|
147 | };
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | const getAST = ({ source, path, fs, match = /^contracts\// } = {}) => {
|
153 | let fullAST;
|
154 | if (path && fs) {
|
155 | const pathToAST = path.resolve(
|
156 | __dirname,
|
157 | constants.BUILD_FOLDER,
|
158 | constants.AST_FOLDER,
|
159 | constants.AST_FILENAME
|
160 | );
|
161 | if (!fs.existsSync(pathToAST)) {
|
162 | throw Error('Cannot find AST');
|
163 | }
|
164 | fullAST = JSON.parse(fs.readFileSync(pathToAST));
|
165 | } else {
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | if (!data.ast) {
|
172 | throw Error('AST currently not supported in browser mode');
|
173 | }
|
174 | fullAST = data.ast;
|
175 | }
|
176 |
|
177 |
|
178 | const ast = Object.entries(fullAST)
|
179 | .filter(([astEntryKey]) => match.test(astEntryKey))
|
180 | .reduce((memo, [key, val]) => {
|
181 | memo[key] = val;
|
182 | return memo;
|
183 | }, {});
|
184 |
|
185 | if (source && source in ast) {
|
186 | return ast[source];
|
187 | } else if (source) {
|
188 |
|
189 | const [key, entry] =
|
190 | Object.entries(ast).find(([astEntryKey]) => astEntryKey.includes('/' + source)) || [];
|
191 | if (!key || !entry) {
|
192 | throw Error(`Cannot find AST entry for source: ${source}`);
|
193 | }
|
194 | return { [key]: entry };
|
195 | } else {
|
196 | return ast;
|
197 | }
|
198 | };
|
199 |
|
200 | const getFeeds = ({ network, path, fs, deploymentPath, useOvm = false } = {}) => {
|
201 | let feeds;
|
202 |
|
203 | if (!deploymentPath && network !== 'local' && (!path || !fs)) {
|
204 | feeds = data[getFolderNameForNetwork({ network, useOvm })].feeds;
|
205 | } else {
|
206 | const pathToFeeds = deploymentPath
|
207 | ? path.join(deploymentPath, constants.FEEDS_FILENAME)
|
208 | : getPathToNetwork({
|
209 | network,
|
210 | path,
|
211 | useOvm,
|
212 | file: constants.FEEDS_FILENAME,
|
213 | });
|
214 | if (!fs.existsSync(pathToFeeds)) {
|
215 | throw Error(`Cannot find feeds file.`);
|
216 | }
|
217 | feeds = JSON.parse(fs.readFileSync(pathToFeeds));
|
218 | }
|
219 |
|
220 | const synths = getSynths({ network, useOvm, path, fs, deploymentPath, skipPopulate: true });
|
221 |
|
222 |
|
223 | return Object.entries(feeds).reduce((memo, [asset, entry]) => {
|
224 | memo[asset] = Object.assign(
|
225 |
|
226 |
|
227 |
|
228 | { standalone: !synths.find(synth => synth.asset === asset) || asset === 'ETH' },
|
229 | assets[asset],
|
230 | entry
|
231 | );
|
232 | return memo;
|
233 | }, {});
|
234 | };
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | const getSynths = ({
|
241 | network = 'mainnet',
|
242 | path,
|
243 | fs,
|
244 | deploymentPath,
|
245 | useOvm = false,
|
246 | skipPopulate = false,
|
247 | } = {}) => {
|
248 | let synths;
|
249 |
|
250 | if (!deploymentPath && network !== 'local' && (!path || !fs)) {
|
251 | synths = data[getFolderNameForNetwork({ network, useOvm })].synths;
|
252 | } else {
|
253 | const pathToSynthList = deploymentPath
|
254 | ? path.join(deploymentPath, constants.SYNTHS_FILENAME)
|
255 | : getPathToNetwork({ network, useOvm, path, file: constants.SYNTHS_FILENAME });
|
256 | if (!fs.existsSync(pathToSynthList)) {
|
257 | throw Error(`Cannot find synth list.`);
|
258 | }
|
259 | synths = JSON.parse(fs.readFileSync(pathToSynthList));
|
260 | }
|
261 |
|
262 | if (skipPopulate) {
|
263 | return synths;
|
264 | }
|
265 |
|
266 | const feeds = getFeeds({ network, useOvm, path, fs, deploymentPath });
|
267 |
|
268 |
|
269 | return synths.map(synth => {
|
270 |
|
271 | synth = Object.assign({}, assets[synth.asset], synth);
|
272 |
|
273 | if (feeds[synth.asset]) {
|
274 | const { feed } = feeds[synth.asset];
|
275 |
|
276 | synth = Object.assign({ feed }, synth);
|
277 | }
|
278 |
|
279 | if (synth.inverted) {
|
280 | synth.description = `Inverse ${synth.description}`;
|
281 | }
|
282 |
|
283 | if (typeof synth.index === 'string') {
|
284 | const { index } = synths.find(({ name }) => name === synth.index) || {};
|
285 | if (!index) {
|
286 | throw Error(
|
287 | `While processing ${synth.name}, it's index mapping "${synth.index}" cannot be found - this is an error in the deployment config and should be fixed`
|
288 | );
|
289 | }
|
290 | synth = Object.assign({}, synth, { index });
|
291 | }
|
292 |
|
293 | if (synth.index) {
|
294 | synth.index = synth.index.map(indexEntry => {
|
295 | return Object.assign({}, assets[indexEntry.asset], indexEntry);
|
296 | });
|
297 | }
|
298 |
|
299 | return synth;
|
300 | });
|
301 | };
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | const getStakingRewards = ({
|
307 | network = 'mainnet',
|
308 | useOvm = false,
|
309 | path,
|
310 | fs,
|
311 | deploymentPath,
|
312 | } = {}) => {
|
313 | if (!deploymentPath && network !== 'local' && (!path || !fs)) {
|
314 | return data[getFolderNameForNetwork({ network, useOvm })].rewards;
|
315 | }
|
316 |
|
317 | const pathToStakingRewardsList = deploymentPath
|
318 | ? path.join(deploymentPath, constants.STAKING_REWARDS_FILENAME)
|
319 | : getPathToNetwork({
|
320 | network,
|
321 | path,
|
322 | useOvm,
|
323 | file: constants.STAKING_REWARDS_FILENAME,
|
324 | });
|
325 | if (!fs.existsSync(pathToStakingRewardsList)) {
|
326 | return [];
|
327 | }
|
328 | return JSON.parse(fs.readFileSync(pathToStakingRewardsList));
|
329 | };
|
330 |
|
331 |
|
332 |
|
333 |
|
334 | const getUsers = ({ network = 'mainnet', user, useOvm = false } = {}) => {
|
335 | const testnetOwner = '0xB64fF7a4a33Acdf48d97dab0D764afD0F6176882';
|
336 | const base = {
|
337 | owner: testnetOwner,
|
338 | deployer: testnetOwner,
|
339 | marketClosure: testnetOwner,
|
340 | oracle: '0xac1e8B385230970319906C03A1d8567e3996d1d5',
|
341 | fee: '0xfeEFEEfeefEeFeefEEFEEfEeFeefEEFeeFEEFEeF',
|
342 | zero: '0x' + '0'.repeat(40),
|
343 | };
|
344 |
|
345 | const map = {
|
346 | mainnet: Object.assign({}, base, {
|
347 | owner: '0xEb3107117FEAd7de89Cd14D463D340A2E6917769',
|
348 | deployer: '0xDe910777C787903F78C89e7a0bf7F4C435cBB1Fe',
|
349 | marketClosure: '0xC105Ea57Eb434Fbe44690d7Dec2702e4a2FBFCf7',
|
350 | oracle: '0xaC1ED4Fabbd5204E02950D68b6FC8c446AC95362',
|
351 | }),
|
352 | kovan: Object.assign({}, base),
|
353 | rinkeby: Object.assign({}, base),
|
354 | ropsten: Object.assign({}, base),
|
355 | goerli: Object.assign({}, base),
|
356 | 'goerli-ovm': Object.assign({}, base),
|
357 | local: Object.assign({}, base, {
|
358 |
|
359 | owner: '0xc783df8a850f42e7F7e57013759C285caa701eB6',
|
360 | }),
|
361 | };
|
362 |
|
363 | const users = Object.entries(
|
364 | map[getFolderNameForNetwork({ network, useOvm })]
|
365 | ).map(([key, value]) => ({ name: key, address: value }));
|
366 |
|
367 | return user ? users.find(({ name }) => name === user) : users;
|
368 | };
|
369 |
|
370 | const getVersions = ({
|
371 | network = 'mainnet',
|
372 | path,
|
373 | fs,
|
374 | deploymentPath,
|
375 | useOvm,
|
376 | byContract = false,
|
377 | } = {}) => {
|
378 | let versions;
|
379 |
|
380 | if (!deploymentPath && network !== 'local' && (!path || !fs)) {
|
381 | versions = data[getFolderNameForNetwork({ network, useOvm })].versions;
|
382 | } else {
|
383 | const pathToVersions = deploymentPath
|
384 | ? path.join(deploymentPath, constants.VERSIONS_FILENAME)
|
385 | : getPathToNetwork({ network, useOvm, path, file: constants.VERSIONS_FILENAME });
|
386 | if (!fs.existsSync(pathToVersions)) {
|
387 | throw Error(`Cannot find versions for network.`);
|
388 | }
|
389 | versions = JSON.parse(fs.readFileSync(pathToVersions));
|
390 | }
|
391 |
|
392 | if (byContract) {
|
393 |
|
394 | return Object.values(versions).reduce((memo, entry) => {
|
395 | for (const [contract, contractEntry] of Object.entries(entry.contracts)) {
|
396 | memo[contract] = memo[contract] || [];
|
397 | memo[contract].push(contractEntry);
|
398 | }
|
399 | return memo;
|
400 | }, {});
|
401 | }
|
402 | return versions;
|
403 | };
|
404 |
|
405 | const getSuspensionReasons = ({ code = undefined } = {}) => {
|
406 | const suspensionReasonMap = {
|
407 | 1: 'System Upgrade',
|
408 | 2: 'Market Closure',
|
409 | 55: 'Circuit Breaker (Phase one)',
|
410 | 65: 'Decentralized Circuit Breaker (Phase two)',
|
411 | 99999: 'Emergency',
|
412 | };
|
413 |
|
414 | return code ? suspensionReasonMap[code] : suspensionReasonMap;
|
415 | };
|
416 |
|
417 |
|
418 |
|
419 |
|
420 | const getTokens = ({ network = 'mainnet', path, fs, useOvm = false } = {}) => {
|
421 | const synths = getSynths({ network, useOvm, path, fs });
|
422 | const targets = getTarget({ network, useOvm, path, fs });
|
423 | const feeds = getFeeds({ network, useOvm, path, fs });
|
424 |
|
425 | return [
|
426 | Object.assign(
|
427 | {
|
428 | symbol: 'SNX',
|
429 | asset: 'SNX',
|
430 | name: 'Synthetix',
|
431 | address: targets.ProxyERC20.address,
|
432 | decimals: 18,
|
433 | },
|
434 | feeds['SNX'].feed ? { feed: feeds['SNX'].feed } : {}
|
435 | ),
|
436 | ].concat(
|
437 | synths
|
438 | .filter(({ category }) => category !== 'internal')
|
439 | .map(synth => ({
|
440 | symbol: synth.name,
|
441 | asset: synth.asset,
|
442 | name: synth.description,
|
443 | address: targets[`Proxy${synth.name === 'sUSD' ? 'ERC20sUSD' : synth.name}`].address,
|
444 | index: synth.index,
|
445 | inverted: synth.inverted,
|
446 | decimals: 18,
|
447 | feed: synth.feed,
|
448 | }))
|
449 | .sort((a, b) => (a.symbol > b.symbol ? 1 : -1))
|
450 | );
|
451 | };
|
452 |
|
453 | const decode = ({ network = 'mainnet', fs, path, data, target, useOvm = false } = {}) => {
|
454 | const sources = getSource({ network, path, fs, useOvm });
|
455 | for (const { abi } of Object.values(sources)) {
|
456 | abiDecoder.addABI(abi);
|
457 | }
|
458 | const targets = getTarget({ network, path, fs, useOvm });
|
459 | let contract;
|
460 | if (target) {
|
461 | contract = Object.values(targets).filter(
|
462 | ({ address }) => address.toLowerCase() === target.toLowerCase()
|
463 | )[0].name;
|
464 | }
|
465 | return { method: abiDecoder.decodeMethod(data), contract };
|
466 | };
|
467 |
|
468 | const wrap = ({ network, deploymentPath, fs, path, useOvm = false }) =>
|
469 | [
|
470 | 'decode',
|
471 | 'getAST',
|
472 | 'getPathToNetwork',
|
473 | 'getSource',
|
474 | 'getStakingRewards',
|
475 | 'getFeeds',
|
476 | 'getSynths',
|
477 | 'getTarget',
|
478 | 'getTokens',
|
479 | 'getUsers',
|
480 | 'getVersions',
|
481 | ].reduce((memo, fnc) => {
|
482 | memo[fnc] = (prop = {}) =>
|
483 | module.exports[fnc](Object.assign({ network, deploymentPath, fs, path, useOvm }, prop));
|
484 | return memo;
|
485 | }, {});
|
486 |
|
487 | module.exports = {
|
488 | constants,
|
489 | decode,
|
490 | defaults,
|
491 | getAST,
|
492 | getPathToNetwork,
|
493 | getSource,
|
494 | getStakingRewards,
|
495 | getSuspensionReasons,
|
496 | getFeeds,
|
497 | getSynths,
|
498 | getTarget,
|
499 | getTokens,
|
500 | getUsers,
|
501 | getVersions,
|
502 | networks,
|
503 | networkToChainId,
|
504 | toBytes32,
|
505 | wrap,
|
506 | ovmIgnored,
|
507 | };
|