-
Notifications
You must be signed in to change notification settings - Fork 0
/
VotingContract.sol
82 lines (64 loc) · 2.46 KB
/
VotingContract.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting{
struct Candidate{
uint256 candidateID;
address candidateAddress;
string candidateName;
uint256 candidateVoteCount;
}
struct Voter{
uint256 voterID;
address voterAddress;
bool hasVoted;
}
uint256 public numVoters;
uint256 public numCandidates;
address public administrator;
mapping (uint256 => Candidate) public Candidates;
mapping(address => Voter) public Voters;
event CandidateAdded(address candidateaddr);
event VoteCast(address voter);
constructor(){
administrator = msg.sender;
numVoters = 0;
numCandidates = 0;
}
function addCandidate(string memory candidatename, address candidateaddr ) public {
require(msg.sender==administrator, "Only the Administrator can add this contract");
require(candidateaddr != address(0), "Invalid candidate address");
numCandidates++;
Candidate memory newCandidate = Candidate({
candidateID: numCandidates,
candidateAddress: candidateaddr,
candidateName:candidatename,
candidateVoteCount:0
});
Candidates[numCandidates] = newCandidate;
emit CandidateAdded(candidateaddr);
}
function castVote(uint256 candidateid) public {
require(candidateid <= numCandidates && candidateid>0, "Invalid candidate ID!");
require(!Voters[msg.sender].hasVoted, "You have already voted!");
Candidates[candidateid].candidateVoteCount++;
Voters[msg.sender].hasVoted =true;
emit VoteCast(msg.sender);
}
function getCandidate(uint256 candidateid) public view returns (address,string memory,uint256) {
require(candidateid <= numCandidates && candidateid>0, "Invalid candidate ID!");
Candidate memory thisCandidate = Candidates[candidateid];
return (thisCandidate.candidateAddress,thisCandidate.candidateName,thisCandidate.candidateVoteCount);
}
function getWinner() public view returns(uint256) {
uint256 max_votes = Candidates[1].candidateVoteCount;
uint256 winner_candidate = 1;
for (uint256 i = 2; i <= numCandidates; i++)
{
if(Candidates[i].candidateVoteCount > max_votes){
max_votes = Candidates[i].candidateVoteCount;
winner_candidate = i;
}
}
return winner_candidate;
}
}