-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoting.sol
83 lines (71 loc) · 2.39 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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.8.2 <0.9.0;
contract VotingSystem{
struct vote{
address voterAddress;
bool choice;
}
struct voter{
string voterName;
bool voted;
}
uint private countVotes = 0;
uint public finalVotes = 0;
uint public totalVoters = 0;
uint public totalVote = 0;
address public adminAddress;
string public adminName;
string public proposal;
mapping(uint => vote) private votes; //no of votes
mapping(address => voter) public voterRegister; //it tells the address of people registered for voting
enum Situation{registered , voting , declined}
Situation public situation;
modifier condition(bool _condition){
require(_condition);
_;
}
modifier onlyAdmin(){
require(msg.sender == adminAddress);
_;
}
modifier inSituation(Situation _situation){
require(situation == _situation);
_;
}
constructor(string memory _adminName , string memory _proposal) {
adminAddress = msg.sender;
adminName = _adminName;
proposal = _proposal;
situation = Situation.registered;
}
function voterRegistration(address _voterAddress , string memory _voterName) public inSituation(Situation.registered) onlyAdmin{
voter memory v;
v.voterName = _voterName;
v.voted = false;
voterRegister[_voterAddress] = v;
totalVoters++;
}
function beginVoting() public inSituation(Situation.registered) onlyAdmin{
situation = Situation.voting;
}
function doVote(bool _choice) public inSituation(Situation.voting) returns(bool voted){
bool isPresent = false;
if(bytes(voterRegister[msg.sender].voterName).length != 0 && voterRegister[msg.sender].voted == false){
voterRegister[msg.sender].voted = true;
vote memory v;
v.voterAddress = msg.sender;
v.choice = _choice;
if(_choice){
countVotes++;
}
votes[totalVote] = v;
totalVote++;
isPresent = true;
}
return isPresent;
}
function endVote() public inSituation(Situation.voting) onlyAdmin{
situation = Situation.declined;
finalVotes = countVotes;
}
}