-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
55 lines (48 loc) · 1.8 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
import { ethers } from "./ethers-5.1.esm.min.js";
import { abi, contractAddress } from "./constants.js";
const connectButton = document.getElementById("ConnectButton");
const donateButton = document.getElementById("DonateButton");
connectButton.onclick = connect;
donateButton.onclick = donate;
async function connect() {
if (typeof window.ethereum !== "undefined") {
await window.ethereum.request({ method: "eth_requestAccounts" });
connectButton.innerHTML = "Connected";
connectButton.style.justifyContent = "center";
connectButton.style.alignItems = "center";
} else {
alert("Metamask Not Found");
}
}
async function donate() {
const ethAmount = document.getElementById("ethamount").value;
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 address = await contract.signer.getAddress();
console.log(address);
try {
const tx = await contract.donate({
value: ethers.utils.parseEther(ethAmount),
});
await listenForTransactionMine(tx, provider);
} catch (error) {
console.log(error);
}
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`);
//return new Promise();
//listen for the transaction to be mined
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations`
);
resolve(); //THe promise ends when resolves or reject is called
//Since resolve is called the promise is resolved and function is returned
});
});
}