-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voting2.sol
67 lines (55 loc) · 2.03 KB
/
Voting2.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
pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with
contract Voting {
struct voter {
address voterAddress; // The address of the voter
bool hasVoted;
}
mapping (bytes32 => uint8) public votesReceived;
/* Maps an Ethereum address to their voter profile*/
mapping (address => voter) public voterInfo;
/* List of candidates */
bytes32[] public candidateList;
address[] public voterList;
/* This is the constructor which will be called once when you
deploy the contract to the blockchain. When we deploy the contract,
we will pass an array of candidates who will be contesting in the election
*/
function Voting(bytes32[] candidateNames, address[] voters) public {
candidateList = candidateNames;
voterList = voters;
// for(uint i = 0; i < voters.length; i++) {
// voterInfo[voters[i]] = voter(voters[i], false);
// }
}
// This function returns the total votes a candidate has received so far
function totalVotesFor(bytes32 candidate) view public returns (uint8) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
// This function increments the vote count for the specified candidate. This
// is equivalent to casting a vote
function voteForCandidate(bytes32 candidate) public {
if(validCandidate(candidate) && validVoter(msg.sender)){
votesReceived[candidate] += 1;
voterInfo[msg.sender].hasVoted = true;
}
}
function validCandidate(bytes32 candidate) view public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
function validVoter(address voterAddress) view public returns (bool) {
return voterInfo[voterAddress].voterAddress == address(0x0) && !voterInfo[voterAddress].hasVoted;
}
function getVoters() view public returns (address[]) {
return voterList;
}
function getCandidates() view public returns (bytes32[]) {
return candidateList;
}
}