-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVotingSystem.sol
120 lines (97 loc) · 3.02 KB
/
VotingSystem.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// 6 important points of any voting system
// ------------------------------
// 1. voting must be secret (use ether address)
// 2. one address = one vote
// 3. voters are eligable to vote. (person who deploys the contract dictates who gets to vote
// 4. transparency - rules must be transparent.
// 5. votes must be recorded and counted
// 6. reliable, no frauds.
contract VotingSystem {
// VARIABLES
struct vote {
address voterAddress;
// yay or nay
bool choice;
}
struct voter {
string voterName;
// did the voter voted or not
bool voted;
}
// count the votes
uint private countResult = 0;
uint public finalResult= 0;
uint public totalVoter = 0;
uint public totalVote = 0;
// address of ballot
address public ballotOfficialAddress;
string public ballotOfficialName;
string public proposal;
mapping(uint => vote) private votes;
mapping(address => voter) public voterRegister;
// states of ballot
enum State {
Created,
Voting,
Ended
}
State public state;
// MODIFIERS
modifier condition(bool _condition){
require(_condition);
_;
}
modifier onlyOfficial(){
require(msg.sender == ballotOfficialAddress);
_;
}
modifier inState(State _state){
require(state == _state);
_;
}
// EVENTS
// FUNCTIONS
constructor(string memory _ballotOfficialName, string memory _proposal){
ballotOfficialAddress = msg.sender;
ballotOfficialName = _ballotOfficialName;
proposal = _proposal;
state = State.Created;
}
// creator of ballot adds voter addresses one by one
function addVoter(address _voterAddress, string memory _voterName) public inState(State.Created) onlyOfficial {
voter memory v;
v.voterName = _voterName;
v.voted = false;
voterRegister[_voterAddress] = v;
totalVoter++;
}
// creator starts the ballot
function startVote() public inState(State.Created) onlyOfficial{
state = State.Voting;
}
// voter chooses, true or false
function doVote(bool _choice) public inState(State.Voting) returns(bool voted){
// first check if the voter is in the voter registry && voter hasnt voted yet
bool found = false;
if(bytes(voterRegister[msg.sender].voterName).length != 0 && !voterRegister[msg.sender].voted){
voterRegister[msg.sender].voted = true;
vote memory v;
v.voterAddress = msg.sender;
v.choice = _choice;
if(_choice){
// we only count true values (yay)
countResult++;
}
votes[totalVote] = v;
totalVote++;
found = true;
}
return found;
}
function endVote() public inState(State.Voting) onlyOfficial{
state = State.Ended;
finalResult = countResult;
}
}