-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (83 loc) · 3 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
//in node js
//require()
//in front-end jsyou can not use require
//import
import { ethers } from "./ethers-5.6.esm.min.js"
import { abi, contractadress } from "./consts.js"
const connectButton = document.getElementById("connectButton")
const fundButton = document.getElementById("fundButton")
const balancebtn = document.getElementById("getbalance")
const withdrawbtn = document.getElementById("withdrawButton")
balancebtn.onclick = getBalance
connectButton.onclick = connect
fundButton.onclick = fund
withdrawbtn.onclick = withdraw
async function connect() {
if (window.ethereum !== "undefined") {
window.ethereum.request({ method: "eth_requestAccounts" })
console.log("Conected!")
connectButton.innerHTML = "Conected!"
} else {
connectButton.innerHTML = "Install Metamask"
}
}
//fund function
async function fund() {
const amountfund = document.getElementById("ethamount").value
console.log(`Funding with ${amountfund}...`)
if (window.ethereum !== "undefined") {
//provider/connection to the blockchain
//signer/ wallet/ someone with some gas
//contract that we are interacting with
//ABI
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractadress, abi, signer)
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(amountfund),
})
//lstned for th to be mined
//wait for this tx to finish
await listenForTransactionMine(transactionResponse, provider)
console.log("Done")
} catch (error) {
console.log(error)
}
} else {
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`)
//listen for this transaction to finish
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations`
)
resolve()
})
})
}
async function getBalance() {
if (window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum)
const balance = await provider.getBalance(contractadress)
console.log(ethers.utils.formatEther(balance))
}
}
//withdraw
async function withdraw() {
if (window.ethereum !== "undefined") {
console.log("Withdarwing...")
const provider = new ethers.providers.Web3Provider(window.ethereum)
const signer = provider.getSigner()
const contract = new ethers.Contract(contractadress, abi, signer)
try {
const transactionResponse = await contract.withdraw()
await listenForTransactionMine(transactionResponse, provider)
} catch (error) {
console.log(error)
}
}
}