Created
May 15, 2020 13:56
-
-
Save andrejrakic/16b77bba83e46984b04d39faf070f221 to your computer and use it in GitHub Desktop.
Solidity smart contract for Election on Blockchain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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