-
Notifications
You must be signed in to change notification settings - Fork 52
/
maker.js
125 lines (113 loc) · 3.42 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
import Maker from '@makerdao/dai';
import McdPlugin, {
ETH,
BAT,
USDC,
USD,
DAI,
SAI,
defaultCdpTypes
} from '@makerdao/dai-plugin-mcd';
import trezorPlugin from '@makerdao/dai-plugin-trezor-web';
import ledgerPlugin from '@makerdao/dai-plugin-ledger-web';
import walletLinkPlugin from '@makerdao/dai-plugin-walletlink';
import mewconnectPlugin from '@myetherwallet/dai-plugin-mewconnect';
import walletConnectPlugin from '@makerdao/dai-plugin-walletconnect';
import dcentPlugin from 'dai-plugin-dcent-web';
import portisPlugin from '@makerdao/dai-plugin-portis';
import configPlugin from '@makerdao/dai-plugin-config';
import networkConfig from './references/config';
import { networkNameToId } from './utils/network';
import { getQueryParamByName } from './utils/dev';
import rinkebyAddresses from './references/contracts/rinkeby';
import goerliAddresses from './references/contracts/goerli';
import ropstenAddresses from './references/contracts/ropsten';
let _maker;
const otherNetworksOverrides = [
{
network: 'rinkeby',
contracts: rinkebyAddresses
},
{ network: 'goerli', contracts: goerliAddresses },
{ network: 'ropsten', contracts: ropstenAddresses }
].reduce((acc, { network, contracts }) => {
for (const [contractName, contractAddress] of Object.entries(contracts)) {
if (!acc[contractName]) acc[contractName] = {};
acc[contractName][network] = contractAddress;
}
return acc;
}, {});
export function getMaker() {
if (_maker === undefined) throw new Error('Maker has not been instatiated');
return _maker;
}
export async function instantiateMaker({
rpcUrl,
network,
testchainId,
backendEnv
}) {
const addressOverrides = ['rinkeby', 'ropsten', 'goerli'].some(
networkName => networkName === network
)
? otherNetworksOverrides
: {};
const mcdPluginConfig = {
defaultCdpTypes,
prefetch: false,
addressOverrides
};
const walletLinkPluginConfig = {
rpcUrl: networkConfig.rpcUrls[networkNameToId(network)]
};
const config = {
log: false,
plugins: [
trezorPlugin,
ledgerPlugin,
[walletLinkPlugin, walletLinkPluginConfig],
mewconnectPlugin,
walletConnectPlugin,
dcentPlugin,
portisPlugin,
[McdPlugin, mcdPluginConfig]
],
smartContract: {
addressOverrides
},
provider: {
url: rpcUrl,
type:
network === 'testnet'
? 'HTTP'
: getQueryParamByName('ws') === '0'
? 'HTTP'
: 'WEBSOCKET'
},
web3: {
pollingInterval: network === 'testnet' ? 100 : null
},
gas: {
apiKey: '3e722dd73e76ba3d2eb7507e316727db8a71d1fbc960ed1018e999a53f75'
},
multicall: true
};
// Use the config plugin, if we have a testchainConfigId
if (testchainId) {
delete config.provider;
config.plugins.push([configPlugin, { testchainId, backendEnv }]);
} else if (!rpcUrl) {
if (config.provider.type === 'HTTP')
rpcUrl = networkConfig.rpcUrls[networkNameToId(network)];
else if (config.provider.type === 'WEBSOCKET')
rpcUrl = networkConfig.wsRpcUrls[networkNameToId(network)];
else throw new Error(`Unsupported provider type: ${config.provider.type}`);
if (!rpcUrl) throw new Error(`Unsupported network: ${network}`);
config.provider.url = rpcUrl;
}
const maker = await Maker.create('http', config);
// for debugging
window.maker = maker;
return maker;
}
export { USD, DAI, ETH, BAT, SAI, USDC };