-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (97 loc) · 3.98 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
107
108
109
110
111
112
113
import { ethers } from "./ethers-5.6.esm.min.js"
import { abi, address } 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
console.log(ethers)
async function connect() {
if (typeof window.ethereum !== "undefined") {
console.log("I see a Metamask!")
await window.ethereum.request({ method: "eth_requestAccounts" })
connectButton.innerHTML = "Connected!"
console.log("Connected to Metamask")
} else {
connectButton.innerHTML = "Please install metamask!"
}
}
async function fund() {
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with ${ethAmount}...`)
if (typeof window.ethereum !== "undefined") {
// provider / connection to the blockchain
// signer / wallet with gas
// provider is our metamask
const provider = new ethers.providers.Web3Provider(window.ethereum)
console.log(provider)
// signer is the account connected with this website via metamask
const signer = provider.getSigner()
console.log(signer)
// Contract we're interacting with, we need:
// 1. ABI:
// - copy ABI from backend's artifacts folder
// - paste it in `constants.js` and export as a const
// - import { abi } from "./constants.js"
// 2. Address:
// - spin up a node (yarn hardhat node)
// - connect metamask to that node (Add New Network > via RPC)
// - obtain the address from terminal logs (FundMe deployed at address 0x...)
// - paste it in `constant.js` and export it as a string const
const contract = new ethers.Contract(address, abi, signer)
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(ethAmount),
})
// Two ways of getting feedback when the transaction gets through:
// (Option A) listen for the tx to be mined
// Hey, wait for this transaction to finish...
await listenForTransactionMine(transactionResponse, provider)
console.log("done!")
// (Option B) listen for an event
// provider.once( )
} catch (error) {
console.log(error)
}
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`)
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations.`
)
resolve()
})
})
}
async function getBalance() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const balance = await provider.getBalance(address)
console.log(ethers.utils.formatEther(balance))
}
}
async function withdraw() {
if (typeof window.ethereum !== "undefined") {
// provider is our metamask
const provider = new ethers.providers.Web3Provider(window.ethereum)
console.log(provider)
// signer is the account connected with this website via metamask
const signer = provider.getSigner()
console.log(signer)
const contract = new ethers.Contract(address, abi, signer)
console.log("Withdrawing...")
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
console.log("Withdrawn!")
} catch (error) {
console.log(error)
}
}
}