-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (88 loc) · 3.26 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
//if you are using frontend javascript and not nodejs require wont work
//const { ethers } = require("ethers"); this wont work, infront end just download ethers code and import it
import { ethers } from "./ethers5.2.esm.min.js";
import { abi, contractAddress } from "./constants.js";
//
const connectButton = document.getElementById("connectButton");
const fundButton = document.getElementById("fund");
const balanceButton = document.getElementById("balanceButton");
const withdrawButton = document.getElementById("withdraw");
connectButton.onclick = connect;
fundButton.onclick = fund;
balanceButton.onclick = getBalance;
withdrawButton.onclick = withdraw;
//
// connect
async function connect() {
if (typeof window.ethereum !== "undefined") {
await window.ethereum.request({ method: "eth_requestAccounts" });
console.log("metamask connected");
connectButton.innerHTML = "connected";
} else {
connectButton.innerHTML = "please install metamask";
}
}
//get balance
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));
} else {
connectButton.innerHTML = "please install metamask";
}
}
//fund function
async function fund() {
const ethAmount = document.getElementById("ethAmount").value;
console.log(`funding with .${ethAmount}.....`);
if (typeof window.ethereum !== "undefined") {
//to fund you would need
//provider/ connection to the blockchain
const provider = new ethers.providers.Web3Provider(window.ethereum);
//signer/wallet/someone with gas
const signer = provider.getSigner();
//contract that we are interacting with. you would need the ABI and adress
const contract = new ethers.Contract(contractAddress, abi, signer);
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(ethAmount),
});
// listen for it to finish, for tx to be mined
await listenForTransactionMine(transactionResponse, provider);
console.log("done...");
} catch (error) {
console.log(error);
}
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}.....`);
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReciept) => {
console.log(
`completed with ${transactionReciept.confirmations} confirmations`
);
resolve();
});
});
}
//withdraw
async function withdraw() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const balance = await provider.getBalance(contractAddress);
const withdrawBalance = ethers.utils.formatEther(balance);
console.log(`withdrawing...${withdrawBalance}`);
try {
const transactionResponse = await contract.withdraw();
await listenForTransactionMine(transactionResponse, provider);
} catch (error) {
console.log(error);
}
} else {
connectButton.innerHTML = "please install metamask";
}
}