-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresults.js
54 lines (52 loc) · 1.79 KB
/
results.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
import { ethers } from "./ethers-5.6.esm.min.js";
import { abi, contractAddress } from "./constants.js";
const provider = new ethers.providers.Web3Provider(window.ethereum);
displayResult();
async function displayResult() {
if (typeof window.ethereum !== "undefined") {
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);
const start = await contract.getStart();
const end = await contract.getEnd();
let maxVotes = 0;
let winner = 0;
let count = await contract.getTotalCandidate();
const num = Number(count);
if (!start && end) {
for (let i = 1; i <= num; i++) {
const candidate = await contract.candidateDetails(i - 1);
if (candidate.voteCount > maxVotes) {
winner = candidate.header;
maxVotes = candidate.voteCount;
}
}
document.getElementById("notify").innerHTML = "Winner is ".concat(winner);
}
if (start && !end) {
document.getElementById("notify").innerHTML = "Election is Going on";
}
if (!start && !end) {
document.getElementById("notify").innerHTML =
"Election has not started yet";
}
} else {
withdrawButton.innerHTML = "Please install MetaMask";
}
}
function listenForTransactionMine(transactionResponse, provider) {
console.log(`Mining ${transactionResponse.hash}`);
return new Promise((resolve, reject) => {
try {
provider.once(transactionResponse.hash, (transactionReceipt) => {
console.log(
`Completed with ${transactionReceipt.confirmations} confirmations. `
);
resolve();
});
} catch (error) {
reject(error);
}
});
}