-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
118 lines (87 loc) · 2.35 KB
/
Copy pathapp.js
File metadata and controls
118 lines (87 loc) · 2.35 KB
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
111
112
113
114
115
116
117
118
import { CONFIG } from "./config.js";
import { ABI } from "./contract.js";
let provider, signer, contract;
let mining = false;
let seconds = 0;
let timerInterval;
// =========================
// INIT RPC
// =========================
async function initRPC() {
provider = new ethers.JsonRpcProvider(CONFIG.RPC_URL);
console.log("RPC READY");
}
// =========================
// CONNECT WALLET
// =========================
async function connectWallet() {
if (!window.ethereum) return alert("Install MetaMask");
provider = new ethers.BrowserProvider(window.ethereum);
await provider.send("eth_requestAccounts", []);
signer = await provider.getSigner();
contract = new ethers.Contract(
CONFIG.CONTRACT_ADDRESS,
ABI,
signer
);
document.getElementById("wallet").innerText =
await signer.getAddress();
console.log("Wallet connected");
}
// =========================
// START MINING
// =========================
async function startMining() {
if (!contract) return alert("Connect wallet first");
await contract.startMining();
mining = true;
timerInterval = setInterval(() => {
seconds++;
const h = String(Math.floor(seconds / 3600)).padStart(2, "0");
const m = String(Math.floor((seconds % 3600) / 60)).padStart(2, "0");
const s = String(seconds % 60).padStart(2, "0");
document.getElementById("timer").innerText =
`${h}:${m}:${s}`;
}, 1000);
}
// =========================
// STOP MINING
// =========================
async function stopMining() {
if (!contract) return;
await contract.stopMining();
mining = false;
clearInterval(timerInterval);
}
// =========================
// CLAIM REWARD
// =========================
async function claimReward() {
if (!contract) return;
const tx = await contract.claimReward();
await tx.wait();
alert("Reward Claimed");
}
// =========================
// MINT NFT
// =========================
async function mintNFT() {
if (!contract) return;
await contract.mint(
await signer.getAddress(),
"ipfs://metadata.json"
);
alert("NFT Minted");
}
// =========================
// WITHDRAW
// =========================
async function withdraw(amountEth) {
if (!signer) return;
const tx = await signer.sendTransaction({
to: CONFIG.CONTRACT_ADDRESS,
value: ethers.parseEther(amountEth)
});
await tx.wait();
alert("Withdraw Success");
}