forked from renproject/command-center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3Container.tsx
320 lines (281 loc) · 10.8 KB
/
web3Container.tsx
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
import * as Sentry from "@sentry/browser";
import LedgerTransportU2F from "@ledgerhq/hw-transport-u2f";
import LedgerTransportWebUSB from "@ledgerhq/hw-transport-webusb";
import {
renLocalnet,
renMainnet,
RenNetwork,
RenNetworkDetails,
renTestnet,
} from "@renproject/contracts";
import Onboard from "bnc-onboard";
import { API as OnboardInstance } from "bnc-onboard/dist/src/interfaces";
import React, { useCallback, useEffect, useState } from "react";
import { createContainer } from "unstated-next";
import Web3 from "web3";
import { toChecksumAddress } from "web3-utils";
import BigNumber from "bignumber.js";
import Notify, { API as NotifyInstance } from "bnc-notify";
import { LoggedOut } from "../controllers/common/popups/LoggedOut";
import { getWeb3BrowserName, Web3Browser } from "../lib/ethereum/browsers";
import { getReadOnlyWeb3 } from "../lib/ethereum/getWeb3";
import {
BLOCKNATIVE_KEY,
EXTRA_WALLETS_INFURA_KEY,
INFURA_KEY,
PORTIS_KEY,
} from "../lib/react/environmentVariables";
import { PopupContainer } from "./popupContainer";
import useStorageState from "./useStorageState/useStorageState";
const stringToNetwork = (network: RenNetwork): RenNetworkDetails => {
switch (network.toLowerCase()) {
case RenNetwork.Mainnet:
return renMainnet;
case RenNetwork.Testnet:
return renTestnet;
case RenNetwork.Localnet: //subgraphs not working
return renLocalnet;
}
throw new Error(`Unknown network ${network}.`);
};
const useOnboard = (networkID: number) => {
const [onboard, setOnboard] = useState<OnboardInstance | undefined>();
const [walletAddress, setWalletAddress] = useState<string | null>(null);
const [balance, setBalance] = useState<BigNumber | null>(null);
const onBalance = (newBalance: string) =>
setBalance(new BigNumber(newBalance));
useEffect(() => {
(async () => {
const appName = "Command Center";
const appUrl =
networkID === 1
? "mainnet.renproject.io"
: "testnet.renproject.io";
const supportEmail = "[email protected]";
const rpcUrl = EXTRA_WALLETS_INFURA_KEY;
const isWebUSBSupported = navigator.platform.includes("Win")
? false
: await LedgerTransportWebUSB.isSupported().catch(() => false);
const LedgerTransport = isWebUSBSupported
? LedgerTransportWebUSB
: LedgerTransportU2F;
setOnboard(
Onboard({
dappId: BLOCKNATIVE_KEY,
networkId: networkID,
darkMode: true,
subscriptions: {
address: setWalletAddress,
balance: onBalance,
},
walletSelect: {
wallets: [
{ walletName: "coinbase", preferred: true },
{
walletName: "trust",
preferred: true,
rpcUrl: rpcUrl,
},
{ walletName: "metamask", preferred: true },
{
walletName: "trezor",
appUrl: appUrl,
email: supportEmail,
rpcUrl: rpcUrl,
},
{
walletName: "ledger",
rpcUrl,
LedgerTransport,
},
{
walletName: "ledger",
label: "Ledger (legacy)",
rpcUrl,
},
{ walletName: "gnosis" },
{ walletName: "authereum" },
{
walletName: "lattice",
rpcUrl: rpcUrl,
appName: appName,
},
{
walletName: "cobovault",
rpcUrl: rpcUrl,
appName: appName,
},
// {
// walletName: "fortmatic",
// apiKey: FORTMATIC_KEY,
// preferred: true
// },
{
walletName: "portis",
apiKey: PORTIS_KEY,
label: "Login with Email",
},
{
walletName: "walletConnect",
preferred: true,
infuraKey: INFURA_KEY,
},
{ walletName: "opera" },
{ walletName: "operaTouch" },
{ walletName: "torus" },
{ walletName: "status" },
{
walletName: "walletLink",
rpcUrl: rpcUrl,
appName: appName,
},
{ walletName: "imToken", rpcUrl: rpcUrl },
{ walletName: "meetone" },
{ walletName: "mykey", rpcUrl: rpcUrl },
{ walletName: "huobiwallet", rpcUrl: rpcUrl },
{ walletName: "hyperpay" },
{ walletName: "wallet.io", rpcUrl: rpcUrl },
{ walletName: "atoken" },
{ walletName: "frame" },
{ walletName: "ownbit" },
{ walletName: "alphawallet" },
{ walletName: "xdefi" },
{ walletName: "bitpie" },
],
},
walletCheck: [
{ checkName: "accounts" },
{ checkName: "connect" },
{ checkName: "network" },
],
}),
);
})().catch(console.error);
}, [networkID]);
return { onboard, walletAddress, balance };
};
const useNotify = (networkID: number) => {
const [notify, setNotify] = useState<NotifyInstance | undefined>();
useEffect(() => {
setNotify(
Notify({
dappId: BLOCKNATIVE_KEY,
networkId: networkID,
darkMode: true,
desktopPosition: "topRight",
}),
);
}, [networkID]);
return { notify };
};
const useWeb3Container = (initialState = RenNetwork.Testnet) => {
const { setPopup, clearPopup } = PopupContainer.useContainer();
const [renNetwork] = useState<RenNetworkDetails>(
stringToNetwork(initialState),
);
const { onboard, balance, walletAddress } = useOnboard(
renNetwork.networkID,
);
const { notify } = useNotify(renNetwork.networkID);
const rpcUrl = `${renNetwork.infura}/v3/${INFURA_KEY}`;
const [readonlyWeb3] = useState<Web3>(getReadOnlyWeb3(rpcUrl));
const [web3, setWeb3] = useState<Web3>(readonlyWeb3);
// Login data
const [address, setAddress] = useState<string | null>(null);
const [web3BrowserName, setWeb3BrowserName] = useState(
Web3Browser.Web3Browser,
);
// The following are almost opposites - except that they are both
// initialized as false. LoggedInBefore means that we try to re-login again
// when the page is loaded. Logged out means that we don't try to re-login.
const [loggedInBefore, setLoggedInBefore] = useStorageState(
localStorage,
"loggedInBefore",
false,
);
const [loggedOut, setLoggedOut] = useState(false);
const logout = useCallback(() => {
setWeb3(readonlyWeb3);
setLoggedOut(true);
setLoggedInBefore(false);
if (onboard) {
onboard.walletReset();
}
}, [onboard, readonlyWeb3, setLoggedInBefore]);
const login = ({ newWeb3 }: { newWeb3: Web3 }) => {
setWeb3(newWeb3);
setLoggedOut(false);
setLoggedInBefore(true);
};
const promptLogin = async (options: { manual: boolean }) => {
if ((loggedOut && !options.manual) || !onboard) {
return;
}
const walletSelected = await onboard.walletSelect();
if (!walletSelected) {
return;
}
const walletChecked = await onboard.walletCheck();
if (!walletChecked) {
return;
}
const newWeb3 = new Web3(onboard.getState().wallet.provider);
// For now we use first account
const defaultAddress = toChecksumAddress(onboard.getState().address);
Sentry.configureScope((scope) => {
// scope.setUser({ id: address });
scope.setExtra("loggedIn", true);
});
// Store address in the store (and in local storage)
login({ newWeb3 });
const newWeb3BrowserName = getWeb3BrowserName(newWeb3.currentProvider);
setWeb3BrowserName(newWeb3BrowserName);
return defaultAddress;
};
useEffect(() => {
if (!address) {
setAddress(walletAddress);
return;
}
if (walletAddress !== address) {
const onClick = () => {
setAddress(walletAddress);
clearPopup();
};
const onCancel = () => {
logout();
clearPopup();
};
setPopup({
popup: (
<LoggedOut
onConnect={onClick}
onCancel={onCancel}
newAddress={walletAddress}
/>
),
onCancel,
dismissible: false,
overlay: true,
});
}
}, [walletAddress, address, clearPopup, logout, setPopup]);
return {
renNetwork,
web3,
setWeb3,
address,
balance,
web3BrowserName,
setWeb3BrowserName,
loggedInBefore,
setLoggedInBefore,
loggedOut,
setLoggedOut,
notify,
logout,
promptLogin,
// lookForLogout,
};
};
export const Web3Container = createContainer(useWeb3Container);