-
Notifications
You must be signed in to change notification settings - Fork 0
/
voting.sol
100 lines (83 loc) · 2.75 KB
/
voting.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
struct Candidate {
string name;
uint256 voteCount;
string avatarIpfsHash;
string program;
}
address public owner;
bool public votingOpen;
Candidate[] public candidates;
mapping(address => bool) public hasVoted;
uint256 public votingEndedAt;
constructor() {
owner = msg.sender;
votingOpen = false;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can call this function");
_;
}
modifier whenVotingOpen() {
require(votingOpen, "Voting is not open");
_;
}
function startVoting() public onlyOwner {
require(!votingOpen, "Voting is already open");
votingOpen = true;
}
function addCandidate(
string memory name,
string memory avatarIpfsHash,
string memory program
) public onlyOwner {
candidates.push(Candidate(name, 0, avatarIpfsHash, program));
}
function vote(uint256 candidateIndex) public whenVotingOpen {
require(!hasVoted[msg.sender], "You have already voted");
require(candidateIndex < candidates.length, "Invalid candidate index");
candidates[candidateIndex].voteCount += 1;
hasVoted[msg.sender] = true;
}
function endVoting() public onlyOwner {
require(votingOpen, "Voting is already closed");
votingOpen = false;
votingEndedAt = block.timestamp;
}
function getWinner() public view returns (string memory winnerName, uint256 winnerVoteCount) {
require(!votingOpen, "Voting is still open");
uint256 winningVoteCount = 0;
uint256 winningIndex = 0;
for (uint256 i = 0; i < candidates.length; i++) {
if (candidates[i].voteCount > winningVoteCount) {
winningVoteCount = candidates[i].voteCount;
winningIndex = i;
}
}
winnerName = candidates[winningIndex].name;
winnerVoteCount = candidates[winningIndex].voteCount;
}
function getCandidateCount() public view returns (uint256) {
return candidates.length;
}
function getCandidate(uint256 index) public view returns (
string memory name,
uint256 voteCount,
string memory avatarIpfsHash,
string memory program
) {
require(index < candidates.length, "Invalid candidate index");
Candidate storage candidate = candidates[index];
return (
candidate.name,
candidate.voteCount,
candidate.avatarIpfsHash,
candidate.program
);
}
function getVotingEndedAt() public view returns (uint256) {
return votingEndedAt;
}
}