-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (99 loc) · 3.62 KB
/
index.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
import { ethers } from "https://cdnjs.cloudflare.com/ajax/libs/ethers/6.7.0/ethers.min.js"
import { abi, contractAddress } from "./constants.js"
const connectButton = document.getElementById("connectButton")
const fundButton = document.getElementById("fundButton")
const balanceButton = document.getElementById("balanceButton")
const withdrawButton = document.getElementById("withdrawButton")
connectButton.onclick = connect
fundButton.onclick = fund
balanceButton.onclick = getBalance
withdrawButton.onclick = withdraw
async function connect() {
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({ method: "eth_requestAccounts" })
} catch (error) {
console.log(error)
}
connectButton.innerHTML = "MetaMask is connected"
// const accounts = await ethereum.request({ method: "eth_accounts" })
// console.log(accounts)
} else {
connectButton.innerHTML = "Please install MetaMask"
}
}
async function getBalance() {
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({ method: "eth_requestAccounts" })
} catch (error) {
console.log(error)
}
const provider = new ethers.BrowserProvider(window.ethereum)
try {
const balance = await provider.getBalance(contractAddress)
console.log(`Current balance is ${ethers.formatEther(balance)} ETH`)
} catch (error) {
console.log(error)
}
} else {
connectButton.innerHTML = "Please install MetaMask"
}
}
async function fund() {
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with amount ${ethAmount} ETH`)
if (typeof window.ethereum !== "undefined") {
await ethereum.request({ method: "eth_requestAccounts" })
}
const provider = new ethers.BrowserProvider(window.ethereum)
const signer = await provider.getSigner()
console.log(signer)
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.fund({
value: ethers.parseEther(ethAmount),
})
await listenForTransactionMined(transactionResponse, provider)
console.log("Transaction is done")
} catch (error) {
console.log(error)
}
}
async function withdraw() {
console.log("Withdrawing...")
if (typeof window.ethereum !== "undefined") {
await ethereum.request({ method: "eth_requestAccounts" })
}
const provider = new ethers.BrowserProvider(window.ethereum)
const signer = await provider.getSigner()
console.log(signer)
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMined(transactionResponse, provider)
console.log("The funds have been withdrawn")
} catch (error) {
console.log(error)
}
}
function listenForTransactionMined(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}`)
return new Promise((resolve, reject) => {
try {
provider.once(
transactionResponse.hash,
async (transactionReceipt) => {
const confirmations =
await transactionReceipt.confirmations()
console.log(
`Completed with ${confirmations} confirmations. `,
)
resolve()
},
)
} catch (error) {
reject(error)
}
})
}