-
Notifications
You must be signed in to change notification settings - Fork 6
/
ethers.js
94 lines (80 loc) · 2.61 KB
/
ethers.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
import React, { useEffect, useState } from "react";
import { singletonHook } from "react-singleton-hook";
import { init, useConnectWallet } from "@web3-onboard/react";
import injectedModule from "@web3-onboard/injected-wallets";
import walletConnectModule from "@web3-onboard/walletconnect";
import ledgerModule from "@web3-onboard/ledger";
import ls from "local-storage";
import { EthersProviderContext } from "near-social-vm";
import chains from "./chains.json";
const web3onboardKey = "web3-onboard:connectedWallets";
const wcV2InitOptions = {
version: 2,
projectId: "f5f4d212d26f7cf2a7546a5bc3afeb40",
dappUrl: window.location.origin,
};
const walletConnect = walletConnectModule(wcV2InitOptions);
const ledger = ledgerModule(wcV2InitOptions);
const injected = injectedModule();
// initialize Onboard
const onboard = init({
wallets: [injected, walletConnect, ledger],
chains,
appMetadata: {
name: "NEAR Social",
description: "NEAR Social",
},
theme: "dark",
containerElements: {
// connectModal: '#near-social-navigation-bar',
// accountCenter: "#near-social-web3-account",
},
});
const defaultEthersProviderContext = {
useConnectWallet,
setChain: onboard.setChain,
};
const useEthersProviderContext = singletonHook(
defaultEthersProviderContext,
() => {
const [{ wallet }] = useConnectWallet();
const [ethersProvider, setEthersProvider] = useState(
defaultEthersProviderContext
);
useEffect(() => {
(async () => {
const walletsSub = onboard.state.select("wallets");
const { unsubscribe } = walletsSub.subscribe((wallets) => {
const connectedWallets = wallets.map(({ label }) => label);
ls.set(web3onboardKey, connectedWallets);
});
const previouslyConnectedWallets = ls.get(web3onboardKey) || [];
if (previouslyConnectedWallets) {
// You can also auto connect "silently" and disable all onboard modals to avoid them flashing on page load
await onboard.connectWallet({
autoSelect: {
label: previouslyConnectedWallets[0],
disableModals: true,
},
});
}
})();
}, []);
useEffect(() => {
setEthersProvider({
provider: wallet?.provider,
useConnectWallet,
setChain: onboard.setChain,
});
}, [wallet]);
return ethersProvider;
}
);
export const EthersProvider = ({ children }) => {
const ethersProviderContext = useEthersProviderContext();
return (
<EthersProviderContext.Provider value={ethersProviderContext}>
{children}
</EthersProviderContext.Provider>
);
};