-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting.sol
69 lines (58 loc) · 2.31 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Voting {
address private immutable owner;
uint private conclude;
uint public candidatesCount;
uint public voteTotal;
uint public winnerId;
struct Candidate {
uint id;
string name;
uint voteCount;
}
mapping(uint => Candidate) public candidates;
mapping(address => bool) private voters;
mapping(address => bool) private rights;
constructor() {
owner = msg.sender;
}
function setVoters(address addr) public {
require(msg.sender == owner, "Only the contract creator can set the voters");
rights[addr] = true;
}
function addCandidate(string memory name) public {
require(msg.sender == owner, "Only the contract creator can set the candidates");
require(voteTotal == 0, "Cannot submit candidate after voting started");
candidatesCount++;
candidates[candidatesCount] = Candidate(candidatesCount, name, 0);
}
function vote(uint candidateId) public {
require(rights[msg.sender], "Voter doesn't have the rights to vote");
require(msg.sender != owner, "The contract creator cannot participate in the voting");
require(!voters[msg.sender], "Vote already cast from this address");
require(candidateId > 0 && candidateId <= candidatesCount, "Invalid candidate ID");
require(candidatesCount >= 2, "Must be at least 2 candidates before votes can be cast");
require(conclude == 0, "Voting concluded");
voters[msg.sender] = true;
candidates[candidateId].voteCount++;
voteTotal++;
}
function concludeVoting() public {
require(msg.sender == owner, "Only the contract creator can conclude the voting");
uint maxVote = 0;
for(uint i=1; i<=candidatesCount; i++) {
if(candidates[i].voteCount > maxVote) {
winnerId = i;
maxVote = candidates[i].voteCount;
}
}
conclude += 1;
}
function returnWinner()public view returns (string memory winner) {
return(candidates[winnerId].name);
}
function showName(uint n) public view returns (string memory name) {
return(candidates[n].name);
}
}