forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
482 lines (433 loc) · 14.2 KB
/
index.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
'use strict';
const w3utils = require('web3-utils');
const abiDecoder = require('abi-decoder');
// load the data in explicitly (not programmatically) so webpack knows what to bundle
const data = {
kovan: {
deployment: require('./publish/deployed/kovan/deployment.json'),
versions: require('./publish/deployed/kovan/versions.json'),
synths: require('./publish/deployed/kovan/synths.json'),
rewards: require('./publish/deployed/kovan/rewards.json'),
feeds: require('./publish/deployed/kovan/feeds.json'),
},
rinkeby: {
deployment: require('./publish/deployed/rinkeby/deployment.json'),
versions: require('./publish/deployed/rinkeby/versions.json'),
synths: require('./publish/deployed/rinkeby/synths.json'),
rewards: require('./publish/deployed/rinkeby/rewards.json'),
feeds: require('./publish/deployed/rinkeby/feeds.json'),
},
ropsten: {
deployment: require('./publish/deployed/ropsten/deployment.json'),
versions: require('./publish/deployed/ropsten/versions.json'),
synths: require('./publish/deployed/ropsten/synths.json'),
rewards: require('./publish/deployed/ropsten/rewards.json'),
feeds: require('./publish/deployed/ropsten/feeds.json'),
},
mainnet: {
deployment: require('./publish/deployed/mainnet/deployment.json'),
versions: require('./publish/deployed/mainnet/versions.json'),
synths: require('./publish/deployed/mainnet/synths.json'),
rewards: require('./publish/deployed/mainnet/rewards.json'),
feeds: require('./publish/deployed/mainnet/feeds.json'),
},
};
const assets = require('./publish/assets.json');
const networks = ['local', 'kovan', 'rinkeby', 'ropsten', 'mainnet'];
const networkToChainId = {
mainnet: 1,
ropsten: 3,
rinkeby: 4,
kovan: 42,
};
const constants = {
BUILD_FOLDER: 'build',
CONTRACTS_FOLDER: 'contracts',
COMPILED_FOLDER: 'compiled',
FLATTENED_FOLDER: 'flattened',
AST_FOLDER: 'ast',
CONFIG_FILENAME: 'config.json',
PARAMS_FILENAME: 'params.json',
SYNTHS_FILENAME: 'synths.json',
STAKING_REWARDS_FILENAME: 'rewards.json',
OWNER_ACTIONS_FILENAME: 'owner-actions.json',
DEPLOYMENT_FILENAME: 'deployment.json',
VERSIONS_FILENAME: 'versions.json',
FEEDS_FILENAME: 'feeds.json',
AST_FILENAME: 'asts.json',
ZERO_ADDRESS: '0x' + '0'.repeat(40),
inflationStartTimestampInSecs: 1551830400, // 2019-03-06T00:00:00Z
};
// The solidity defaults are managed here in the same format they will be stored, hence all
// numbers are converted to strings and those with 18 decimals are also converted to wei amounts
const defaults = {
WAITING_PERIOD_SECS: (60 * 5).toString(), // 5 mins
PRICE_DEVIATION_THRESHOLD_FACTOR: w3utils.toWei('3'),
TRADING_REWARDS_ENABLED: false,
ISSUANCE_RATIO: w3utils
.toBN(1)
.mul(w3utils.toBN(1e18))
.div(w3utils.toBN(6))
.toString(), // 1/6 = 0.16666666667
FEE_PERIOD_DURATION: (3600 * 24 * 7).toString(), // 1 week
TARGET_THRESHOLD: '1', // 1% target threshold (it will be converted to a decimal when set)
LIQUIDATION_DELAY: (3600 * 24 * 3).toString(), // 3 days
LIQUIDATION_RATIO: w3utils.toWei('0.5'), // 200% cratio
LIQUIDATION_PENALTY: w3utils.toWei('0.1'), // 10% penalty
RATE_STALE_PERIOD: (3600 * 25).toString(), // 25 hours
EXCHANGE_FEE_RATES: {
forex: w3utils.toWei('0.003'),
commodity: w3utils.toWei('0.003'),
equities: w3utils.toWei('0.003'),
crypto: w3utils.toWei('0.003'),
index: w3utils.toWei('0.003'),
},
MINIMUM_STAKE_TIME: (3600 * 24).toString(), // 1 days
AGGREGATOR_WARNING_FLAGS: {
mainnet: '0x4A5b9B4aD08616D11F3A402FF7cBEAcB732a76C6',
kovan: '0x6292aa9a6650ae14fbf974e5029f36f95a1848fd',
},
INITIAL_ISSUANCE: w3utils.toWei(`${100e6}`),
};
/**
* Converts a string into a hex representation of bytes32, with right padding
*/
const toBytes32 = key => w3utils.rightPad(w3utils.asciiToHex(key), 64);
const getPathToNetwork = ({ network = 'mainnet', file = '', path } = {}) =>
path.join(__dirname, 'publish', 'deployed', network, file);
// Pass in fs and path to avoid webpack wrapping those
const loadDeploymentFile = ({ network, path, fs, deploymentPath }) => {
if (!deploymentPath && network !== 'local' && (!path || !fs)) {
return data[network].deployment;
}
const pathToDeployment = deploymentPath
? path.join(deploymentPath, constants.DEPLOYMENT_FILENAME)
: getPathToNetwork({ network, path, file: constants.DEPLOYMENT_FILENAME });
if (!fs.existsSync(pathToDeployment)) {
throw Error(`Cannot find deployment for network: ${network}.`);
}
return JSON.parse(fs.readFileSync(pathToDeployment));
};
/**
* Retrieve the list of targets for the network - returning the name, address, source file and link to etherscan
*/
const getTarget = ({ network = 'mainnet', contract, path, fs, deploymentPath } = {}) => {
const deployment = loadDeploymentFile({ network, path, fs, deploymentPath });
if (contract) return deployment.targets[contract];
else return deployment.targets;
};
/**
* Retrieve the list of solidity sources for the network - returning the abi and bytecode
*/
const getSource = ({ network = 'mainnet', contract, path, fs, deploymentPath } = {}) => {
const deployment = loadDeploymentFile({ network, path, fs, deploymentPath });
if (contract) return deployment.sources[contract];
else return deployment.sources;
};
/**
* Retrieve the ASTs for the source contracts
*/
const getAST = ({ source, path, fs, match = /^contracts\// } = {}) => {
let fullAST;
if (path && fs) {
const pathToAST = path.resolve(
__dirname,
constants.BUILD_FOLDER,
constants.AST_FOLDER,
constants.AST_FILENAME
);
if (!fs.existsSync(pathToAST)) {
throw Error('Cannot find AST');
}
fullAST = JSON.parse(fs.readFileSync(pathToAST));
} else {
// Note: The below cannot be required as the build folder is not stored
// in code (only in the published module).
// The solution involves tracking these after each commit in another file
// somewhere persisted in the codebase - JJM
// data.ast = require('./build/ast/asts.json'),
if (!data.ast) {
throw Error('AST currently not supported in browser mode');
}
fullAST = data.ast;
}
// remove anything not matching the pattern
const ast = Object.entries(fullAST)
.filter(([astEntryKey]) => match.test(astEntryKey))
.reduce((memo, [key, val]) => {
memo[key] = val;
return memo;
}, {});
if (source && source in ast) {
return ast[source];
} else if (source) {
// try to find the source without a path
const [key, entry] =
Object.entries(ast).find(([astEntryKey]) => astEntryKey.includes('/' + source)) || [];
if (!key || !entry) {
throw Error(`Cannot find AST entry for source: ${source}`);
}
return { [key]: entry };
} else {
return ast;
}
};
const getFeeds = ({ network, path, fs, deploymentPath } = {}) => {
let feeds;
if (!deploymentPath && network !== 'local' && (!path || !fs)) {
feeds = data[network].feeds;
} else {
const pathToFeeds = deploymentPath
? path.join(deploymentPath, constants.FEEDS_FILENAME)
: getPathToNetwork({
network,
path,
file: constants.FEEDS_FILENAME,
});
if (!fs.existsSync(pathToFeeds)) {
throw Error(`Cannot find feeds file.`);
}
feeds = JSON.parse(fs.readFileSync(pathToFeeds));
}
const synths = getSynths({ network, path, fs, deploymentPath, skipPopulate: true });
// now mix in the asset data
return Object.entries(feeds).reduce((memo, [asset, entry]) => {
memo[asset] = Object.assign(
// standalone feeds are those without a synth using them
// Note: ETH still used as a rate for Depot, can remove the below once the Depot uses sETH rate or is
// removed from the system
{ standalone: !synths.find(synth => synth.asset === asset) || asset === 'ETH' },
assets[asset],
entry
);
return memo;
}, {});
};
/**
* Retrieve ths list of synths for the network - returning their names, assets underlying, category, sign, description, and
* optional index and inverse properties
*/
const getSynths = ({
network = 'mainnet',
path,
fs,
deploymentPath,
skipPopulate = false,
} = {}) => {
let synths;
if (!deploymentPath && network !== 'local' && (!path || !fs)) {
synths = data[network].synths;
} else {
const pathToSynthList = deploymentPath
? path.join(deploymentPath, constants.SYNTHS_FILENAME)
: getPathToNetwork({ network, path, file: constants.SYNTHS_FILENAME });
if (!fs.existsSync(pathToSynthList)) {
throw Error(`Cannot find synth list.`);
}
synths = JSON.parse(fs.readFileSync(pathToSynthList));
}
if (skipPopulate) {
return synths;
}
const feeds = getFeeds({ network, path, fs, deploymentPath });
// copy all necessary index parameters from the longs to the corresponding shorts
return synths.map(synth => {
// mixin the asset details
synth = Object.assign({}, assets[synth.asset], synth);
if (feeds[synth.asset]) {
const { feed } = feeds[synth.asset];
synth = Object.assign({ feed }, synth);
}
if (synth.inverted) {
synth.desc = `Inverse ${synth.desc}`;
}
// replace an index placeholder with the index details
if (typeof synth.index === 'string') {
const { index } = synths.find(({ name }) => name === synth.index) || {};
if (!index) {
throw Error(
`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`
);
}
synth = Object.assign({}, synth, { index });
}
if (synth.index) {
synth.index = synth.index.map(indexEntry => {
return Object.assign({}, assets[indexEntry.asset], indexEntry);
});
}
return synth;
});
};
/**
* Retrieve the list of staking rewards for the network - returning this names, stakingToken, and rewardToken
*/
const getStakingRewards = ({ network = 'mainnet', path, fs, deploymentPath } = {}) => {
if (!deploymentPath && network !== 'local' && (!path || !fs)) {
return data[network].rewards;
}
const pathToStakingRewardsList = deploymentPath
? path.join(deploymentPath, constants.STAKING_REWARDS_FILENAME)
: getPathToNetwork({
network,
path,
file: constants.STAKING_REWARDS_FILENAME,
});
if (!fs.existsSync(pathToStakingRewardsList)) {
return [];
}
return JSON.parse(fs.readFileSync(pathToStakingRewardsList));
};
/**
* Retrieve the list of system user addresses
*/
const getUsers = ({ network = 'mainnet', user } = {}) => {
const testnetOwner = '0xB64fF7a4a33Acdf48d97dab0D764afD0F6176882';
const base = {
owner: testnetOwner,
deployer: testnetOwner,
marketClosure: testnetOwner,
oracle: '0xac1e8B385230970319906C03A1d8567e3996d1d5',
fee: '0xfeEFEEfeefEeFeefEEFEEfEeFeefEEFeeFEEFEeF',
zero: '0x' + '0'.repeat(40),
};
const map = {
mainnet: Object.assign({}, base, {
owner: '0xEb3107117FEAd7de89Cd14D463D340A2E6917769',
deployer: '0xDe910777C787903F78C89e7a0bf7F4C435cBB1Fe',
marketClosure: '0xC105Ea57Eb434Fbe44690d7Dec2702e4a2FBFCf7',
oracle: '0xaC1ED4Fabbd5204E02950D68b6FC8c446AC95362',
}),
kovan: Object.assign({}, base),
rinkeby: Object.assign({}, base),
ropsten: Object.assign({}, base),
};
const users = Object.entries(map[network]).map(([key, value]) => ({ name: key, address: value }));
return user ? users.find(({ name }) => name === user) : users;
};
const getVersions = ({
network = 'mainnet',
path,
fs,
deploymentPath,
byContract = false,
} = {}) => {
let versions;
if (!deploymentPath && network !== 'local' && (!path || !fs)) {
versions = data[network].versions;
} else {
const pathToVersions = deploymentPath
? path.join(deploymentPath, constants.VERSIONS_FILENAME)
: getPathToNetwork({ network, path, file: constants.VERSIONS_FILENAME });
if (!fs.existsSync(pathToVersions)) {
throw Error(`Cannot find versions for network.`);
}
versions = JSON.parse(fs.readFileSync(pathToVersions));
}
if (byContract) {
// compile from the contract perspective
return Object.values(versions).reduce((memo, entry) => {
for (const [contract, contractEntry] of Object.entries(entry.contracts)) {
memo[contract] = memo[contract] || [];
memo[contract].push(contractEntry);
}
return memo;
}, {});
}
return versions;
};
const getSuspensionReasons = ({ code = undefined } = {}) => {
const suspensionReasonMap = {
1: 'System Upgrade',
2: 'Market Closure',
55: 'Circuit Breaker (Phase one)', // https://sips.synthetix.io/SIPS/sip-55
65: 'Decentralized Circuit Breaker (Phase two)', // https://sips.synthetix.io/SIPS/sip-65
99999: 'Emergency',
};
return code ? suspensionReasonMap[code] : suspensionReasonMap;
};
/**
* Retrieve the list of tokens used in the Synthetix protocol
*/
const getTokens = ({ network = 'mainnet', path, fs } = {}) => {
const synths = getSynths({ network, path, fs });
const targets = getTarget({ network, path, fs });
const feeds = getFeeds({ network, path, fs });
return [
Object.assign(
{
symbol: 'SNX',
asset: 'SNX',
name: 'Synthetix',
address: targets.ProxyERC20.address,
decimals: 18,
},
feeds['SNX'].feed ? { feed: feeds['SNX'].feed } : {}
),
].concat(
synths
.filter(({ category }) => category !== 'internal')
.map(synth => ({
symbol: synth.name,
asset: synth.asset,
name: synth.desc,
address: targets[`Proxy${synth.name === 'sUSD' ? 'ERC20sUSD' : synth.name}`].address,
index: synth.index,
inverted: synth.inverted,
decimals: 18,
feed: synth.feed,
}))
.sort((a, b) => (a.symbol > b.symbol ? 1 : -1))
);
};
const decode = ({ network = 'mainnet', fs, path, data, target } = {}) => {
const sources = getSource({ network, path, fs });
for (const { abi } of Object.values(sources)) {
abiDecoder.addABI(abi);
}
const targets = getTarget({ network, path, fs });
let contract;
if (target) {
contract = Object.values(targets).filter(
({ address }) => address.toLowerCase() === target.toLowerCase()
)[0].name;
}
return { method: abiDecoder.decodeMethod(data), contract };
};
const wrap = ({ network, fs, path }) =>
[
'decode',
'getAST',
'getPathToNetwork',
'getSource',
'getStakingRewards',
'getFeeds',
'getSynths',
'getTarget',
'getTokens',
'getUsers',
'getVersions',
].reduce((memo, fnc) => {
memo[fnc] = (prop = {}) => module.exports[fnc](Object.assign({ network, fs, path }, prop));
return memo;
}, {});
module.exports = {
constants,
decode,
defaults,
getAST,
getPathToNetwork,
getSource,
getStakingRewards,
getSuspensionReasons,
getFeeds,
getSynths,
getTarget,
getTokens,
getUsers,
getVersions,
networks,
networkToChainId,
toBytes32,
wrap,
};