-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballot.sol
126 lines (87 loc) · 3.3 KB
/
ballot.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
121
122
123
124
125
126
pragma solidity ^0.4.24;
contract Ballot {
struct Voter {
address voterAddress;
bool isVoted;
bool isSigned;
bool isAdult;
}
struct Candidate {
address candidateAddress;
bytes32 name;
uint32 voteCount;
}
struct Nominee {
address nomineeAddress;
bytes32 name;
uint32 signCount;
bool isNomineed;
}
mapping(address => Voter) voters;
Nominee[] nominees;
Candidate[] candidates;
uint totalVoteCount;
// 06/24/2018 08.00 unix timestamp
uint constant startTime = 1529827200;
// 06/24/2018 17.00 unix timestamp
uint constant endTime = 1529859600;
function beNominee(string _origin, bytes32 _name, uint8 _age, bool _isGrad, uint _numOfPeriods){
require(keccak256(_origin) == keccak256("TC"),
"You should be a citizen of Republic of Turkey");
require(_age >= 40,
"You should be at least 40 years old.");
require(_isGrad,
"You should be graduated from a university.");
require(_numOfPeriods < 2,
"You can't be nominee for president if you've done the service for 2 periods.");
Nominee nom;
nom.name = _name;
nom.nomineeAddress = msg.sender;
nominees.push(nom);
}
function sign(uint _nomineeIndex) {
Voter storage signer = voters[msg.sender];
require(!signer.isSigned,
"You already signed");
require(signer.isAdult,
"Only adults allowed to vote.");
signer.isSigned = true;
Nominee nominee = nominees[_nomineeIndex];
nominee.signCount = nominee.signCount + 1;
}
function claimCandidate(uint _nomineeIndex) {
Nominee nominee = nominees[_nomineeIndex];
require(nominee.signCount > 100000,
"You should get at least 100000 votes to be candidate.");
Candidate candidate;
candidate.name = nominee.name;
candidate.candidateAddress = nominee.nomineeAddress;
candidates.push(candidate);
}
function vote(uint _candidateIndex) {
Voter storage sender = voters[msg.sender];
require(_candidateIndex < candidates.length,
"Not a valid candidate index.");
require(now > startTime && now < endTime,
"You should vote between start time and end time");
require(sender.isAdult,
"Only adults allowed to vote.");
require(!sender.isVoted,
"You already voted.");
sender.isVoted = true;
totalVoteCount = totalVoteCount + 1;
Candidate storage candidate = candidates[_candidateIndex];
candidate.voteCount = candidate.voteCount +1;
}
function announceWinner() returns (address) {
require(now > endTime,
"Wait until end time");
for(uint i = 0; i < candidates.length; i++) {
uint candidateVoteCount = candidates[i].voteCount;
if((totalVoteCount/2) < candidateVoteCount){
return candidates[i].candidateAddress;
break;
}
}
}
}