Skip to content

Instantly share code, notes, and snippets.

@andrejrakic
Created May 15, 2020 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrejrakic/16b77bba83e46984b04d39faf070f221 to your computer and use it in GitHub Desktop.
Save andrejrakic/16b77bba83e46984b04d39faf070f221 to your computer and use it in GitHub Desktop.
Solidity smart contract for Election on Blockchain
pragma solidity >= 0.5.0 < 0.6.0;
// @author Andrej
// @title Election
contract Election {
address payable public contractOwner;
struct Candidate {
string name;
uint256 voteCount;
bool exists; // default je false
}
mapping (string => Candidate) candidates;
mapping (address => bool) public voters;
event votedEvent (address _voter, string _candidate);
modifier isOwner {
require(msg.sender == contractOwner);
_;
}
constructor() public {
contractOwner = msg.sender;
}
function kill() isOwner public {
selfdestruct(contractOwner);
}
modifier voteOnce {
require(!voters[msg.sender], "You have already voted");
_;
}
function addCandidate (string memory _name) public {
candidates[_name].name = _name;
candidates[_name].voteCount = 0;
candidates[_name].exists = true;
}
function vote(string memory _name) public payable voteOnce {
require(candidates[_name].exists, "Candidate does not exist");
candidates[_name].voteCount++;
voters[msg.sender] = true;
emit votedEvent(msg.sender, _name);
}
function getVoteCount(string memory _name) public view returns(uint256) {
return candidates[_name].voteCount;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment