-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathVoting.sol
More file actions
33 lines (25 loc) · 866 Bytes
/
Voting.sol
File metadata and controls
33 lines (25 loc) · 866 Bytes
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
pragma solidity ^0.6.4;
contract Voting {
mapping(bytes32 => uint256) public votesReceived;
bytes32[] public candidateList;
constructor(bytes32[] memory candidateNames) public {
// Assigned While Deployment
candidateList = candidateNames;
}
function totalVotesFor(bytes32 candidate) public view returns (uint256) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate) public view returns (bool) {
for (uint256 i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}