-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
51 lines (42 loc) · 1.28 KB
/
app.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
function final(){
window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.request({ method: 'eth_requestAccounts' });
// Acccounts now exposed
console.log("MetaMask is connected");
} catch (error) {
console.error("User denied account access");
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
console.log("MetaMask is connected");
}
// Non-dapp browsers...
else {
console.log('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});
document.getElementById('connectButton').addEventListener('click', async () => {
if (window.ethereum) {
try {
await ethereum.request({ method: 'eth_requestAccounts' });
console.log("MetaMask is connected");
} catch (error) {
console.error("User denied account access");
}
} else {
console.log('MetaMask not detected');
}
});
async function getAccount() {
const accounts = await ethereum.request({ method: 'eth_accounts' });
console.log(accounts[0]);
}
}
getAccount();