-
Notifications
You must be signed in to change notification settings - Fork 6
/
maker.js
182 lines (163 loc) · 6.03 KB
/
maker.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
import Maker from '@makerdao/dai';
import mcdPlugin, { DAI as DAI_ } from '@makerdao/dai-plugin-mcd';
import scdPlugin from '@makerdao/dai-plugin-scd';
import migrationPlugin from '@makerdao/dai-plugin-migrations';
import ledgerPlugin from '@makerdao/dai-plugin-ledger-web';
import walletLinkPlugin from '@makerdao/dai-plugin-walletlink';
import walletConnectPlugin from '@makerdao/dai-plugin-walletconnect';
import { checkEthereumProvider } from './utils/ethereum';
import { createCurrency } from '@makerdao/currency';
import assert from 'assert';
import ilkList from './references/ilkList';
export const SAI = createCurrency('SAI');
export const PETH = Maker.PETH;
export const ETH = Maker.ETH;
export const USD = Maker.USD;
export const DAI = DAI_;
let maker;
export function getMaker() {
assert(maker, 'Maker has not been instantiated');
return maker;
}
const INFURA_KEY = '5a5a9f036c0c4db0ad00a6545a5f4fc0';
const scdESTestAddressOverrides = {
SAI_GEM: '0xd0A1E359811322d97991E03f863a0C30C2cF029C',
SAI_GOV: '0xAaF64BFCC32d0F15873a02163e7E500671a4ffcD',
SAI_PIP: '0xd65e5e0A6F601d41221eB6Fd3AFA85854EdCa2F9',
SAI_PEP: '0x2c2ceD47D68dfe54920a920c94B941F793aD6793',
SAI_PIT: '0xbd747742B0F1f9791d3e6B85f8797A0cf4fbf10b',
SAI_SAI: '0xEf0a998a09E7AC683bD5b5c35721646456C6b284',
SAI_SIN: '0x67e805382683397124bAC22512E98a337fA2a732',
SAI_SKR: '0xb20a1f576055D05eFDA1dFAdc3d18999A49Efb3E',
SAI_DAD: '0xD60Ff198F1F89f05A434f64633970212f4f3800A',
SAI_MOM: '0x0aB8960161E03561938A8C861EA2A22b5f340d9B',
SAI_VOX: '0x5f3D74DAa9De09d512AC6D8D3F9f0B22974921f0',
SAI_TUB: '0x81c3dD4873Fb724D1bc6b562d3fe573Eeb5b9f64',
SAI_TAP: '0x97dED9E2F2D645b01b86FEc718b01cc5ecF4B476',
SAI_TOP: '0x50b7A3217Ff1dB43e4cacfd7F3c099ec301B61CA',
SAI_ADM: '0xdE6058CeBF6C5C2FE7aD791df862Ff683Cf3D7e9',
SAI_CAGEFREE: '0xf061b83a49d60bb722e589eac586e87694dde17d'
};
export async function instantiateMaker(network) {
const url =
network === 'test' || network === 'testnet'
? 'http://localhost:2000'
: network === 'mainnetfork' ? 'http://localhost:8545'
: `https://${network}.infura.io/v3/${INFURA_KEY}`;
// this is required here instead of being imported normally because it runs
// code that will break if run server-side
const trezorPlugin = require('@makerdao/dai-plugin-trezor-web').default;
const mcdPluginConfig = {
cdpTypes: [
//FIXME: the mcd plugin should be changed so that we're not required to add the default cdp types again here
...ilkList.filter(i => i.symbol !== 'PSM-USDC-A').map(i => {
return {
currency: i.currency,
ilk: i.key,
decimals: i.decimals
};
}),
]
};
let migrationPluginConfig = {};
let scdPluginConfig = {};
let daiAddressOverrides = {};
// if (network === 'kovan' && global.mcdESTest) {
// const addresses = require('./addresses-kovan-mcd-es.json');
// mcdPluginConfig.addressOverrides = addresses;
// migrationPluginConfig.addressOverrides = addresses;
// daiAddressOverrides = {...addresses,
// CDP_MANAGER: addresses.CDP_MANAGER_1,
// MCD_VAT: addresses.MCD_VAT_1,
// MCD_END: addresses.MCD_END_1,
// GET_CDPS_1: addresses.GET_CDPS_1
// };
// }
// if (network === 'testnet') {
// const addresses = require('./addresses-testnet.json');
// mcdPluginConfig.addressOverrides = addresses;
// migrationPluginConfig.addressOverrides = addresses;
// daiAddressOverrides = {...addresses,
// CDP_MANAGER: addresses.CDP_MANAGER_1,
// MCD_VAT: addresses.MCD_VAT_1,
// MCD_END: addresses.MCD_END_1,
// GET_CDPS_1: addresses.GET_CDPS_1
// };
// }
if (network === 'mainnetfork' || network === 'mainnet') {
const addressesMainnet = require('./addresses-mainnet.json');
mcdPluginConfig.addressOverrides = addressesMainnet;
migrationPluginConfig.addressOverrides = addressesMainnet;
daiAddressOverrides = {...addressesMainnet,
CDP_MANAGER_1: addressesMainnet.CDP_MANAGER,
MCD_VAT_1: addressesMainnet.MCD_VAT,
MCD_END_1: addressesMainnet.MCD_END,
GET_CDPS_1: addressesMainnet.GET_CDPS
};
}
const config = {
url,
log: false,
multicall: true,
plugins: [
trezorPlugin,
ledgerPlugin,
walletLinkPlugin,
walletConnectPlugin,
[mcdPlugin, mcdPluginConfig],
[migrationPlugin, migrationPluginConfig],
[scdPlugin, scdPluginConfig]
],
smartContract: {
addressOverrides: daiAddressOverrides
},
gas: {
price: 73474656578
}
};
if (global.scdESTest && !global.testnet) {
console.log('using custom SCD deployment');
Object.assign(config.token.addressOverrides, {
PETH: scdESTestAddressOverrides.SAI_SKR,
DAI: scdESTestAddressOverrides.SAI_SAI,
SAI: scdESTestAddressOverrides.SAI_SAI
});
Object.assign(
config.smartContract.addressOverrides,
scdESTestAddressOverrides
);
}
maker = await Maker.create('http', config);
window.maker = maker; // for debugging
return maker;
}
export async function connectBrowserProvider(maker) {
const networkId = maker.service('web3').networkId();
const browserProvider = await checkEthereumProvider();
assert(
browserProvider.networkId === networkId,
`Expected network ID ${networkId}, got ${browserProvider.networkId}.`
);
assert(
browserProvider.address &&
browserProvider.address.match(/^0x[a-fA-F0-9]{40}$/),
'Got an incorrect or nonexistent wallet address.'
);
const getMatchedAccount = address =>
maker
.listAccounts()
.find(acc => acc.address.toUpperCase() === address.toUpperCase());
let account;
if (maker.service('accounts').hasAccount()) {
const matchedAccount = getMatchedAccount(browserProvider.address);
if (!matchedAccount) {
account = await maker.addAccount({ type: 'browser', autoSwitch: true });
} else {
account = matchedAccount;
}
} else {
account = await maker.addAccount({ type: 'browser', autoSwitch: true });
}
maker.useAccountWithAddress(account.address);
return maker.currentAddress();
}