-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
110 lines (102 loc) · 3.51 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Imports
import { ethers } from "./ethers-5.2.esm.min.js";
import { abi, contractAddress } from "./constents.js";
//Buttens
//connect
const connect_btn = document.getElementById("connect");
connect_btn.onclick = Connect;
//fund
const fund_btn = document.getElementById("fund");
fund_btn.onclick = fund;
//balance
const balance_btn = document.getElementById("balance");
balance_btn.onclick = balance;
//withdraw
const withdraw_btn = document.getElementById("withdraw");
withdraw_btn.onclick = withdraw;
// Connect the wallet
async function Connect() {
if (typeof window.ethereum !== "undefined") {
try {
await ethereum.request({ method: "eth_requestAccounts" });
} catch (error) {
console.log(error);
}
connect_btn.innerHTML = "Connected!";
} else {
alert("please install matamask");
connect_btn.innerHTML = "install matamask!";
}
}
//Fund
async function fund() {
const ethAmount = document.getElementById("ethAmount").value;
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum); //Provider : node(connectin to the blockchain)
const signer = provider.getSigner(); //Signer : wallet
const contract = new ethers.Contract(contractAddress, abi, signer); //contract : ABI / Address
try {
const transactionResponse = await contract.fund({
value: ethers.utils.parseEther(ethAmount),
});
await alert(`You funded ${ethAmount} ETHs, Thank you!`);
// listen for the the transaction tobe mine
await listenFortransactionMine(transactionResponse, provider);
console.log(`Done.`);
} catch (error) {
console.log(error);
}
connect_btn.innerHTML = "Connected!";
} else {
alert("please install matamask");
connect_btn.innerHTML = "install matamask!";
}
}
// transaction mining
function listenFortransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}...`);
return new Promise((resolve, reject) => {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`completed ${transactionReceipt.confirmations} confirmations`
);
resolve();
});
});
}
// Contract balance
async function balance() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum); //Provider : node(connectin to the blockchain)
const balance = await provider.getBalance(contractAddress);
alert(
`The balance of the conract is: ${ethers.utils.formatEther(
balance
)} ETHs.`
);
} else {
alert("please install matamask");
connect_btn.innerHTML = "install matamask!";
}
}
//Withdraw
async function withdraw() {
console.log("withdrawing..");
const ethAmount = document.getElementById("ethAmount").value;
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum); //Provider : node(connectin to the blockchain)
const signer = provider.getSigner(); //Signer : wallet
const contract = new ethers.Contract(contractAddress, abi, signer); //contract : ABI / Address
try {
const transactionResponse = await contract.withdraw();
await listenFortransactionMine(transactionResponse, provider);
await alert(`${ethAmount} ETHs has been withdrawed...`);
} catch (error) {
console.log(error);
}
connect_btn.innerHTML = "Connected!";
} else {
alert("please install matamask");
connect_btn.innerHTML = "install matamask!";
}
}