Skip to content

Instantly share code, notes, and snippets.

@zastrin
Last active March 10, 2018 03:41
Show Gist options
  • Save zastrin/cae97522c2f00e7ee2babb40b6c8f6f6 to your computer and use it in GitHub Desktop.
Save zastrin/cae97522c2f00e7ee2babb40b6c8f6f6 to your computer and use it in GitHub Desktop.
Voting
pragma solidity ^0.4.17;
contract Voting {
// 1. Initialize a few candidates
// 2. Vote for a candidate
// 3. Lookup vote count for each candidate
bytes32[] public candidateNames;
mapping (bytes32 => uint8) public votesReceived;
address public owner;
function Voting(bytes32[] _candidateNames) public {
candidateNames = _candidateNames;
owner = msg.sender;
}
function voteForCandidate(bytes32 _candidateName) public {
require(validCandidate(_candidateName));
votesReceived[_candidateName] += 1;
}
function totalVotesFor(bytes32 _candidateName) view public returns (uint8) {
return votesReceived[_candidateName];
}
function validCandidate(bytes32 _candidateName) view public returns (bool) {
for(uint i=0; i < candidateNames.length; i++) {
if (candidateNames[i] == _candidateName) {
return true;
}
}
return false;
}
function addCandidate(bytes32 _candidateName) public {
require(owner == msg.sender);
// Implement the logic
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment