This repository has been archived by the owner on Jun 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (47 loc) · 1.83 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
'use strict';
const fs = require('fs');
const path = require('path');
const w3utils = require('web3-utils');
/**
* Converts a string into a hex representation of bytes32, with right padding
*/
const toBytes32 = key => w3utils.rightPad(w3utils.asciiToHex(key), 64);
const loadDeploymentFile = ({ network }) => {
const pathToDeployment = path.join(__dirname, 'publish', 'deployed', network, 'deployment.json');
if (!fs.existsSync(pathToDeployment)) {
throw Error(`Cannot find deployment for network: ${network}.`);
}
return JSON.parse(fs.readFileSync(pathToDeployment));
};
const getTarget = ({ network = 'mainnet', contract } = {}) => {
const deployment = loadDeploymentFile({ network });
if (contract) return deployment.targets[contract];
else return deployment.targets;
};
const getSource = ({ network = 'mainnet', contract } = {}) => {
const deployment = loadDeploymentFile({ network });
if (contract) return deployment.sources[contract];
else return deployment.sources;
};
const getSynths = ({ network = 'mainnet' } = {}) => {
const pathToSynthList = path.join(__dirname, 'publish', 'deployed', network, 'synths.json');
if (!fs.existsSync(pathToSynthList)) {
throw Error(`Cannot find synth list.`);
}
const synths = JSON.parse(fs.readFileSync(pathToSynthList));
// copy all necessary index parameters from the longs to the corresponding shorts
return synths.map(synth => {
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`
);
}
return Object.assign({}, synth, { index });
} else {
return synth;
}
});
};
module.exports = { getTarget, getSource, getSynths, toBytes32 };