-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElection.sol
81 lines (66 loc) · 1.96 KB
/
Election.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
pragma solidity 0.4.25;
contract Election {
enum StateType {Setup, Voting, Result}
StateType public State;
// Model a Candidate
struct Candidate {
int id;
string name;
int voteCount;
}
address public ElectionCommission;
address public Voter;
// Store accounts that have voted
mapping(address => bool) public voters;
// Store Candidates
// Fetch Candidate
mapping(int => Candidate) public candidates;
// Store Candidates Count
int public CandidatesCount;
string public winner;
string public cname;
int public candidateId;
string public Title;
/*
// voted event
event votedEvent (
uint indexed _candidateId
);
*/
constructor (string title) public {
Title = title;
CandidatesCount = -1;
AddCandidate("NOTA");
State = StateType.Setup;
}
function vote (int candidateId) public {
// require that they haven't voted before
require(!voters[msg.sender], "You have already voted before");
// require a valid candidate
require(candidateId > 0 && candidateId <= CandidatesCount, "Invalid Candidate");
// record that voter has voted
voters[msg.sender] = true;
// update candidate vote Count
candidates[candidateId].voteCount ++;
// trigger voted event
// emit votedEvent(_candidateId);
State = StateType.Voting;
}
function AddCandidate (string cname) public {
CandidatesCount ++;
candidates[CandidatesCount] = Candidate(CandidatesCount, cname, 0);
}
function rescalc () public {
int mx = 0;
for (int i = 0; i <= CandidatesCount; i++){
if (candidates[i].voteCount > mx){
winner = candidates[i].name;
mx = candidates[i].voteCount;
}
}
State = StateType.Result;
}
function AllowVoting () public {
State = StateType.Voting;
}
}