forked from ossamamehmood/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vote.sol
57 lines (48 loc) · 1.74 KB
/
Vote.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
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.7;
contract Vote{
mapping(address=>bool) public voters;
address public Admin; //0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
uint public noOfVoters;
uint public deadline;
// address public voters;
struct Candidate{
string name;
uint noOfVotes;
mapping(address=>bool) voters;
}
mapping(uint=>Candidate) public CandidatesList;
uint public numOfCandidates;
constructor(uint _noOfVoters, uint _deadline){
noOfVoters = _noOfVoters;
deadline = block.timestamp + _deadline;
Admin = msg.sender;
}
modifier onlyAdmin(){
require(msg.sender==Admin, "not the Admin");
_;
}
function addCandidate(string memory _name) public onlyAdmin{
Candidate storage newCan = CandidatesList[numOfCandidates];
numOfCandidates++;
newCan.noOfVotes = 0;
newCan.name = _name;
}
function voteCandidate(uint _CandidateNo) public {
require(msg.sender!=Admin,"Admin! fuck off");
require(block.timestamp < deadline, "Voting has ended");
Candidate storage newVote = CandidatesList[_CandidateNo];
require(newVote.voters[msg.sender] == false, "you have already voted");
newVote.noOfVotes++;
newVote.voters[msg.sender] = true;
}
function winnyyer() public view returns (uint winningProposal_){
uint winningVoteCount = 0;
for (uint p = 0; p < numOfCandidates; p++) {
if (CandidatesList[p].noOfVotes > winningVoteCount) {
winningVoteCount = CandidatesList[p].noOfVotes;
winningProposal_ = p;
}
}
}
}