This repository has been archived by the owner on Sep 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.js
131 lines (122 loc) · 4.47 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
import assert from 'assert';
import { mapValues, reduce, uniqBy } from 'lodash';
import { createCurrency, createCurrencyRatio } from '@makerdao/currency';
import testnetAddresses from '../contracts/addresses/testnet.json';
import kovanAddresses from '../contracts/addresses/kovan.json';
import abiMap from '../contracts/abiMap.json';
import CdpManager from './CdpManager';
import CdpTypeService from './CdpTypeService';
import AuctionService from './AuctionService';
import SystemDataService from './SystemDataService';
import { ServiceRoles as ServiceRoles_ } from './constants';
export const ServiceRoles = ServiceRoles_;
const { CDP_MANAGER, CDP_TYPE, SYSTEM_DATA, AUCTION } = ServiceRoles;
// look up contract ABIs using abiMap.
// if an exact match is not found, prefix-match against keys ending in *, e.g.
// MCD_JOIN_ETH_B matches MCD_JOIN_*
// this implementation assumes that all contracts in kovan.json are also in testnet.json
let addContracts = reduce(
testnetAddresses,
(result, testnetAddress, name) => {
let abiName = abiMap[name];
if (!abiName) {
const prefix = Object.keys(abiMap).find(
k =>
k.substring(k.length - 1) == '*' &&
k.substring(0, k.length - 1) == name.substring(0, k.length - 1)
);
if (prefix) abiName = abiMap[prefix];
}
if (abiName) {
result[name] = {
abi: require(`../contracts/abis/${abiName}.json`),
address: {
testnet: testnetAddress,
kovan: kovanAddresses[name]
}
};
}
return result;
},
{}
);
export const ETH = createCurrency('ETH');
export const MKR = createCurrency('MKR');
export const USD = createCurrency('USD');
export const USD_ETH = createCurrencyRatio(USD, ETH);
// these are prefixed with M so that they don't override their SCD versions--
// otherwise, adding the MCD plugin would break MCD. maybe there's a better way
// to work around this?
export const MWETH = createCurrency('MWETH');
export const MDAI = createCurrency('MDAI');
export const COL1 = createCurrency('COL1');
// export const COL2 = createCurrency('COL2');
// export const COL3 = createCurrency('COL3');
// export const COL4 = createCurrency('COL4');
// export const COL5 = createCurrency('COL5');
const defaultCdpTypes = [
{ currency: ETH, ilk: 'ETH-A' },
{ currency: ETH, ilk: 'ETH-B' },
{ currency: COL1, ilk: 'COL1-A' },
// { currency: COL1, ilk: 'COL1-B' },
// { currency: COL2, ilk: 'COL2-A' },
// { currency: COL3, ilk: 'COL3-A' },
// { currency: COL4, ilk: 'COL4-A' },
// { currency: COL5, ilk: 'COL5-A' },
// the CDP type below is not present in the testchain snapshot -- its purpose
// is to verify that the code does not throw an error if a CDP type is
// included that we don't have addresses for, so long as we never attempt to
// use it. This flexibility allows us to hardcode extra types for local
// testing even though they won't be present on Kovan or mainnet.
{ currency: ETH, ilk: 'ETH-C' }
];
export default {
addConfig: (
_,
{
cdpTypes = defaultCdpTypes,
network = 'testnet',
addressOverrides,
simplePriceFeeds
} = {}
) => {
if (addressOverrides) {
addContracts = mapValues(addContracts, (contractDetails, name) => ({
...contractDetails,
address: addressOverrides[name] || contractDetails.address[network]
}));
}
const tokens = uniqBy(cdpTypes, 'currency').map(
({ currency, address, abi }) => {
const data =
address && abi ? { address, abi } : addContracts[currency.symbol];
assert(data, `No address and ABI found for "${currency.symbol}"`);
return { currency, abi: data.abi, address: data.address[network] };
}
);
// remove contracts that don't have an address for the given network
for (let c of Object.keys(addContracts)) {
if (
typeof addContracts[c].address === 'object' &&
!addContracts[c].address[network]
) {
delete addContracts[c];
}
}
return {
smartContract: { addContracts },
token: {
erc20: [
{ currency: MDAI, address: addContracts.MCD_DAI.address[network] },
{ currency: MWETH, address: addContracts.ETH.address[network] },
...tokens
]
},
additionalServices: [CDP_MANAGER, CDP_TYPE, AUCTION, SYSTEM_DATA],
[CDP_TYPE]: [CdpTypeService, { cdpTypes, simplePriceFeeds }],
[CDP_MANAGER]: CdpManager,
[AUCTION]: AuctionService,
[SYSTEM_DATA]: SystemDataService
};
}
};