-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
90 lines (68 loc) · 2.47 KB
/
index.html
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
<html>
<head>
<title>MetaMask Connection Page</title>
</head>
<body>
<center>
<h1>Connect Your MetaMask Wallet Here!</h1>
<button id='connectWallet' onclick="">Connect Wallet</button>
<button id='getBalance' onclick="checkBalance()">Get Balance of Wallet</button>
<p id="walletAddress"></p>
<p id="walletBalance"></p>
<script type="text/javascript">
window.userWalletAddress = null
const connectWallet = document.getElementById('connectWallet')
const walletAddress = document.getElementById('walletAddress')
const walletBalance = document.getElementById('walletBalance')
function checkInstalled() {
if (typeof window.ethereum == 'undefined') {
connectWallet.innerText = 'MetaMask isnt installed, please install it'
connectWallet.classList.remove()
connectWallet.classList.add()
return false
}
connectWallet.addEventListener('click', connectWalletwithMetaMask)
}
async function connectWalletwithMetaMask() {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' })
.catch((e) => {
console.error(e.message)
return
})
if (!accounts) { return }
window.userWalletAddress = accounts[0]
walletAddress.innerText = window.userWalletAddress
connectWallet.innerText = 'Sign Out'
connectWallet.removeEventListener('click', connectWalletwithMetaMask)
setTimeout(() => {
connectWallet.addEventListener('click', signOutOfMetaMask)
}, 200)
}
function signOutOfMetaMask() {
window.userwalletAddress = null
walletAddress.innerText = ''
connectWallet.innerText = 'Connect Wallet'
connectWallet.removeEventListener('click', signOutOfMetaMask)
setTimeout(() => {
connectWallet.addEventListener('click', connectWalletwithMetaMask)
}, 200 )
}
async function checkBalance() {
let balance = await window.ethereum.request({ method: "eth_getBalance",
params: [
window.userWalletAddress,
'latest'
]
}).catch((err)=> {
console.log(err)
})
console.log(parseFloat((balance) / Math.pow(10,18)))
walletBalance.innerText = parseFloat((balance) / Math.pow(10,18))
}
window.addEventListener('DOMContentLoaded', () => {
checkInstalled()
})
</script>
</center>
</body>
</html>