-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (61 loc) · 2.6 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
import { ethers } from "./ethers-5.2.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 = balance;
withdrawButton.onclick = withdraw;
async function connect() {
if(window.ethereum.isMetaMask) {
await window.ethereum.request({method: "eth_requestAccounts"});
document.getElementById("connectInfo").innerHTML = "Connected";
} else {
document.getElementById("connectInfo").innerHTML = "You need to install Metamask";
}
}
async function fund() {
const ethAmount = ethers.utils.parseEther(document.getElementById("ethAmount").value);
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
try {
const txResponse = await contract.fund({value: ethAmount});
console.log(await listenForTransactionMine(txResponse, provider));
}
catch(error) {
console.log(error);
}
}
async function balance() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const userAddress = await signer.getAddress();
const fundedAmount = await contract.s_addressToAmountFunded(userAddress);
document.getElementById("balanceInfo").innerHTML = `You funded: ${fundedAmount}`
}
async function withdraw() {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
try {
const txResponse = await contract.withdraw();
await listenForTransactionMine(txResponse, provider);
document.getElementById("withdrawInfo").innerHTML = "Withdraw done!";
} catch(error) {
console.log(error);
}
}
function listenForTransactionMine(txResponse, provider) {
console.log(`Mining ${txResponse.hash}...`);
return new Promise((resolve, reject) => {
provider.once(txResponse.hash, (txReceipt) => {
console.log(`Completed with ${txReceipt.confirmations} Confirmations`);
document.getElementById("fundInfo").innerHTML = "Funding Done!";
resolve("Done");
});
});
}