-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnectwallet.js
171 lines (161 loc) · 7.16 KB
/
connectwallet.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
// (a) on import useDispatch depuis react-redux
import { useDispatch, useSelector } from 'react-redux';
import { useEffect, useState } from 'react'
import { connectRequest, connectSuccess, connectFailed, updateAccount, updateChainId, updateChainHex } from './store';
import { contractChainId } from './config';
export function ConnectWallet() {
// (b) on utilise le hooks useDispatch dans notre composant
// pour récupérer la fonction dispatch de redux
const account = useSelector((state) => state.account);
const chainId = useSelector((state) => state.chainId);
// const errMsg = useSelector((state) => state.errorMsg);
console.log(chainId)
const dispatch = useDispatch();
useEffect(() => {
checkWallet()
}, [])
async function checkWallet(){
dispatch(connectRequest());
const { ethereum } = window;
const metamaskIsInstalled = ethereum && ethereum.isMetaMask;
if (metamaskIsInstalled) {
// Web3EthContract.setProvider(ethereum);
// let web3 = new Web3(ethereum);
try {
const accounts = await ethereum.request({
method: "eth_accounts",
});
if (accounts.length > 0){
dispatch(connectSuccess(accounts[0]));
// dispatch(updateAccount(accounts[0]))
// Add listeners start
ethereum.on("accountsChanged", (accounts) => {
dispatch(updateAccount(accounts[0]));
window.location.reload();
});
}
else {
dispatch(connectFailed("No account found."));
}
const chainId = await ethereum.request({
method: "net_version",
});
dispatch(updateChainId(chainId));
const chainHex = await ethereum.request({ method: 'eth_chainId' });
dispatch(updateChainHex(chainHex));
console.log(chainHex)
ethereum.on("chainChanged", async (chainId) => {
dispatch(updateChainId(chainId));
const _chainHex = await ethereum.request({ method: 'eth_chainId' });
dispatch(updateChainHex(_chainHex));
window.location.reload();
});
// Add listeners end
// } else {
// dispatch(connectFailed("Change network to Rinkeby."));
// }
} catch (err) {
dispatch(connectFailed("Something went wrong."));
}
} else {
dispatch(connectFailed("Install Metamask."));
}
}
const connect = async() => {
// return async (dispatch) => {
dispatch(connectRequest());
const { ethereum } = window;
const metamaskIsInstalled = ethereum && ethereum.isMetaMask;
if (metamaskIsInstalled) {
// Web3EthContract.setProvider(ethereum);
// let web3 = new Web3(ethereum);
try {
const accounts = await ethereum.request({
method: "eth_requestAccounts",
});
const chainId = await ethereum.request({ method: 'eth_chainId' });
// const networkId = await ethereum.request({
// method: "net_version",
// });
// const NetworkData = await SmartContract.networks[networkId];
dispatch(updateChainId(chainId));
// dispatch({type: "UPDATE_CHAINID", payload: chainId});
// if (networkId == 4) {
dispatch(connectSuccess(accounts[0]));
// dispatch({
// type: "CONNECTION_SUCCESS",
// payload: accounts[0],
// })
// Add listeners start
ethereum.on("accountsChanged", (accounts) => {
// dispatch({type: "UPDATE_ACCOUNT", payload: accounts[0]});
dispatch(updateAccount(accounts[0]));
window.location.reload();
});
const chainHex = await ethereum.request({ method: 'eth_chainId' });
dispatch(updateChainHex(chainHex));
ethereum.on("chainChanged", async (chainId) => {
dispatch(updateChainId(chainId));
const _chainHex = await ethereum.request({ method: 'eth_chainId' });
dispatch(updateChainHex(_chainHex));
window.location.reload();
});
window.location.reload();
// Add listeners end
// } else {
// dispatch(connectFailed("Change network to Rinkeby."));
// }
} catch (err) {
dispatch(connectFailed("Something went wrong."));
}
} else {
dispatch(connectFailed("Install Metamask."));
}
}
async function switchNetwork(){
const { ethereum } = window;
try {
await ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: '0xa869' }],//0xa86a AVAX 0x4 Rinkeby 0xa869 fuji 0x6a velas
});
} catch (switchError) {
// This error code indicates that the chain has not been added to MetaMask.
if (switchError.code === 4902) {
try {
await ethereum.request({
method: 'wallet_addEthereumChain',
params: [{ chainId: '0xa869', rpcUrl: 'https://...' /* ... */ }],//0xa86a AVAX 0x6a velas
});
} catch (addError) {
console.log("wtf")
}
}
// handle other "switch" errors
}
dispatch(updateChainId(chainId));
}
// };
// return <button className="px-12 py-2 font-bold text-white bg-pink-500 rounded" onClick={connect}>Connect</button>
if (account.length > 0){
if (chainId == contractChainId){
return <button className="px-4 py-3 text-sm text-white transition duration-300 ease-out transform bg-black border font-mlp md:px-4 hover:bg-pink-mekaverse hover:scale-110">{String(account).substring(0, 6) +
"..." +
String(account).substring(38)}</button>
} else {
return <button className="px-4 py-3 text-sm text-white transition duration-300 ease-out transform bg-black border font-mlp md:px-4 hover:scale-110 hover:border-opacity-0 hover:bg-pink-mekaverse" onClick={switchNetwork}>Switch to Avalanche</button>
}
} else {
return <button className="px-4 py-3 text-sm text-white transition duration-300 ease-out transform bg-black border font-mlp md:px-4 hover:scale-110 hover:border-opacity-0 hover:bg-pink-mekaverse" onClick={connect}><span>Connect Wallet</span></button>
}
// if (chainId != contractChainId && account.length > 0) return (<button className="px-12 py-2 font-bold text-white bg-pink-500 rounded" onClick={switchNetwork}>Switch to Avalanche</button>)
// return <button className="px-12 py-2 font-bold text-white bg-pink-500 rounded" onClick={connect}>
// {account.length > 0 ? (
// String(account).substring(0, 6) +
// "..." +
// String(account).substring(38)
// ) : (
// <span>Connect Wallet 🦊</span>
// )}
// </button>
}