-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
112 lines (94 loc) · 2.75 KB
/
connect.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
import { useState, useEffect } from "react";
import { ethers } from "ethers";
import Web3Modal from "web3modal";
import WalletConnectProvider from "@walletconnect/web3-provider";
const RINKEBY_NETWORK_ID = 4;
let _provider = null;
let _signer = null;
const providerOptions = {
walletconnect: {
package: WalletConnectProvider, // required
options: {
infuraId: "b10ee952e53944bda93d5ec291b03594", // required
},
},
};
const web3Modal = new Web3Modal({
network: "rinkeby", // optional
providerOptions, // required
theme: "dark",
});
export default function Connect() {
const [userWallet, setUserWallet] = useState("");
const [networkError, setNetworkError] = useState(false);
const [count, setCount] = useState(0);
useEffect(() => {
setNetworkError(
!(
window.ethereum.chainId == "0x4" &&
window.ethereum.selectedAddress != null
)
);
}, []);
const fetchAccountData = async () => {
const _web3 = new ethers.providers.Web3Provider(_provider);
_signer = _web3.getSigner();
const accounts = await _web3.listAccounts();
const { chainId } = await _web3.getNetwork();
console.log(chainId);
// MetaMask does not give you all accounts, only the selected account
console.log("Got accounts", accounts);
const selectedAccount = accounts[0];
setUserWallet(selectedAccount);
_checkNetwork(chainId);
};
const _checkNetwork = (chainId) => {
if (
chainId === RINKEBY_NETWORK_ID &&
window.ethereum.selectedAddress != null
) {
setNetworkError(false);
return true;
}
setNetworkError(true);
return false;
};
const connectWallet = async () => {
web3Modal.clearCachedProvider();
console.log("Opening a dialog", web3Modal);
try {
_provider = await web3Modal.connect();
} catch (e) {
console.log("Could not get a wallet connection", e);
return;
}
// Subscribe to accounts change
_provider.on("accountsChanged", (accounts) => {
fetchAccountData();
});
// Subscribe to chainId change
_provider.on("chainChanged", (chainId) => {
fetchAccountData();
});
// Subscribe to networkId change
_provider.on("networkChanged", (networkId) => {
fetchAccountData();
});
await fetchAccountData();
};
return (
<div className="flex justify-between">
{networkError && (
<p className="self-center text-red-500 font-black text-xl mr-4 mt-4">
Please connect wallet to the Rinkeby Testnet
</p>
)}
<button
className="h-16 w-52 self-end border-2 border-grey-600 solid ml-auto mt-4 bg-green-600 text-white shadow-xl rounded-xl"
onClick={connectWallet}
>
Connect Wallet
</button>
</div>
);
}