Skip to content

Instantly share code, notes, and snippets.

@ashokslsk
Last active February 21, 2020 06:48
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 ashokslsk/84bc77473ed80eb076a45ded6f21f0cb to your computer and use it in GitHub Desktop.
Save ashokslsk/84bc77473ed80eb076a45ded6f21f0cb to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.21+commit.dfe3193c.js&optimize=true&gist=
pragma solidity ^0.4.21;
contract Election{
struct Canditate{
string name;
uint voteCount;
}
struct Voter{
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => Voter) public Voters;
Canditate[] public Canditates;
uint public totalVotes;
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
function Election(string _name) public {
owner = msg.sender;
electionName = _name;
}
function getNumberCandidates() public view returns(uint) {
return Canditates.length;
}
function addCandidate(string _name) ownerOnly public {
Canditates.push(Canditate(_name,0));
}
function authorize(address _person) ownerOnly public {
Voters[_person].authorized = true;
}
function vote(uint _voterIndex) public {
require(!Voters[msg.sender].voted);
require(Voters[msg.sender].authorized);
Voters[msg.sender].vote = _voterIndex;
Voters[msg.sender].voted = true;
Canditates[_voterIndex].voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment