Created
December 20, 2018 11:27
-
-
Save DonSHack/06e6acbb7c46f55ff14e7e7ea0b815e8 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.24+commit.e67f0147.js&optimize=false&gist=
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.4.24; | |
contract Election{ | |
address private owner; | |
struct Candidate{ | |
string name; | |
address acc_address; | |
uint256 num_of_votes; | |
} | |
struct Voter{ | |
string name; | |
address acc_address; | |
address vote_for; | |
uint256 vote; | |
} | |
struct Acc_addreses{ | |
address[] candidate_adds; | |
address[] voter_adds; | |
} | |
Acc_addreses private all_acc; | |
mapping(address => Candidate) candidates; | |
constructor () public { | |
owner = msg.sender; | |
} | |
modifier isOwner() { | |
require (owner == msg.sender); | |
_; | |
} | |
modifier isValidVote(uint256 votes) { | |
require (votes <= 5); | |
_; | |
} | |
function addCandidate(string memory _name, address _acc_address) public isOwner { | |
Candidate memory candidate; | |
candidate.name = _name; | |
candidate.acc_address = _acc_address; | |
candidate.num_of_votes = 0; | |
candidates[_acc_address] = candidate; | |
all_acc.candidate_adds.push(_acc_address); | |
} | |
function getAllCandidatesAccounts() public constant returns (address[] memory){ | |
return all_acc.candidate_adds; | |
} | |
function getCandidate(address _address) public constant returns (string memory name, uint256 votes, address add){ | |
return (candidates[_address].name, candidates[_address].num_of_votes, candidates[_address].acc_address); | |
} | |
function vote(string memory _name, uint256 _vote, address _vote_for) public isValidVote(_vote){ | |
Voter memory voter; | |
voter.name = _name; | |
voter.acc_address = msg.sender; | |
voter.vote_for = _vote_for; | |
voter.vote = _vote; | |
all_acc.candidate_adds.push(msg.sender); | |
candidates[_vote_for].num_of_votes += _vote; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment