-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (93 loc) · 3.64 KB
/
Copy pathindex.js
File metadata and controls
100 lines (93 loc) · 3.64 KB
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
// 在 nodejs,用 require() 引入
// 在 前端JavaScript,可以用 require,但用 import 更好
import { ethers } from "./ethers-5.1.esm.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
console.log(ethers)
async function connect() {
if (typeof window.ethereum !== undefined) {
try {
await window.ethereum.request({
method: "eth_requestAccounts",
})
} catch (error) {
console.log(error)
}
connectButton.innerHTML = "Connected!"
} else {
connectButton.innerHTML = "Please install Metamask!"
}
}
async function getBalance() {
if (typeof window.ethereum !== undefined) {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const balance = await provider.getBalance(contractAddress)
console.log(ethers.utils.formatEther(balance))
}
}
// fund function
async function fund() {
const ethAmount = document.getElementById("ethAmount").value
console.log(`Funding with ${ethAmount}...`)
if (typeof window.ethereum !== undefined) {
// 需要provider:连接到blockchain
// 需要signer:钱包
// 需要contract:交互对象
// ^ ABI & Address
const provider = new ethers.providers.Web3Provider(window.ethereum)
// signer 就是前端界面连接到的那个Metamask账号地址
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(ethAmount),
})
// Listen for the tx to be mined
// 或者说 listen for an event <- We haven't learned about yet!
// await 等待事情发生,该函数要返回一个Promise
await listenForTransactionMine(transactionResponse, provider)
console.log("Done!")
} catch (error) {
console.error(error)
}
}
}
// 不是一个async函数
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`)
/**
* 返回Promise是为了给区块链创建一个监听器,同时告诉JavaScript等待该事件发生。
* resolve是一个函数,当成功时执行;reject是一个函数,当失败时执行。
*/
return new Promise((resolve, reject) => {
// listen this transaction to finish. ()=>{}是listener函数。
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations`
)
resolve()
})
})
}
// withdraw function
async function withdraw() {
if (typeof window.ethereum !== undefined) {
console.log("Withdraw...")
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractAddress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
} catch (error) {
console.log(error)
}
}
}