-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
55 lines (50 loc) · 1.51 KB
/
script.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
window.onload = async () => {
document.querySelectorAll(".loginBtn").forEach((btn) => {
btn.addEventListener("click", async () => {
let isLoggedIn = window.userAddress ? true : false;
console.log("clicked", window.userAddress, isLoggedIn);
if (isLoggedIn) {
logOut();
} else {
login();
}
});
});
const getUserInfo = () => {
let main = document.getElementById("main");
let isLoggedIn = window.userAddress ? true : false;
document.querySelectorAll(".loginBtn").forEach((btn) => {
if (isLoggedIn) {
btn.innerText = "Logout";
main.classList.remove("hidden");
} else {
btn.innerText = "Login";
main.classList.add("hidden");
}
});
};
const login = async () => {
await window.ethereum.request({ method: "eth_requestAccounts" });
const accounts = await web3.eth.getAccounts();
const balance = await web3.eth.getBalance(accounts[0]);
window.userAddress = accounts[0];
document.getElementById("Address").innerText = accounts[0];
document.getElementById("balance").innerText = `${balance} ETH`;
getUserInfo();
};
const logOut = async () => {
window.userAddress = null;
document.getElementById("Address").innerText = "";
getUserInfo();
};
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
try {
getUserInfo();
} catch (error) {
console.log("Error");
}
} else {
alert("Please install MetaMask Extension in your browser");
}
};