Skip to content

Instantly share code, notes, and snippets.

@teslaji

teslaji/vote.sol Secret

Created November 29, 2017 16:40
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 teslaji/d8146def92f19c8cb2e6014c591c5bd1 to your computer and use it in GitHub Desktop.
Save teslaji/d8146def92f19c8cb2e6014c591c5bd1 to your computer and use it in GitHub Desktop.
voting contract
pragma solidity ^0.4.17;
contract Owned {
address public owner;
function owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
interface Token{
function transferFrom (address _from, address _to, uint256 _value) public returns ( bool success);
}
contract Ballot {
int public majorityVotes;
Candidate[] public candidates;
mapping (address => uint) public voterId;
Voter[] public voters;
event CandidateAdded (uint candidateId, uint amount, string description);
event Voted (uint candidateId, int result, address voter);
event Tallyvotes (uint candidateId, int result, uint tally, bool active);
struct Candidate {
address candidateAddr;
string name;
uint timestamp;
uint amount;
string description;
uint votingDeadline;
bool executed;
bool candidateWin;
uint noOfVotes;
int currentResult;
bytes32 candidateHash;
Vote[] votes;
mapping (address => bool) voted;
}
struct Vote {
string name;
address voter;
bool inSupport;
}
function addVoter(string voterName, address voter) onlyOwner public {
uint id = voterId[voter];
if (id == 0) {
voterId[voter] = voters.length;
id = voters.length++;
}
voters[id] = Vote({name: voterName, voter: voter, timestamp: now});
}
function addCandidate (
address benificiary,
uint weiAmount,
string name,
string jobdescription,
bytes transactionBytecode
)
onlyOwners public
returns (uint candidateId)
{
candidateId = candidates.length++;
Candidate storage c = candidates[candidateId];
c.recipient = benificiary;
c.amount = weiAmount;
c.description = jobdescription;
c.candidateHash = keccak256(benificiary, weiAmount, transactionBytecode);
c.executed = false;
c.candidateWin = false;
c.noOfVotes = 0;
CandidatesAdded(candidateId, benificairy, weiamount, transactionBytecode);
numCandidates = candidateId + 1;
return candidateId;
}
function giveVote(
uint candidateId,
bool inSupport
) public returns (uint voteId)
{
Candidate storage c = candidates[candidateId];
require(!c.voted[msg.sender]);
c.voted[msg.sender] = true;
c.noOfVotes++;
if (insupport) {
c.currentResult++;
} else {
c.currentResult;
}
Voted(candidateId, insupport, msg.sender);
return c.noOfVotes;
}
function winnerCandidate (uint candidateId, bytes transactionBytecode) public {
Candidate storage c = candidates[candidateId];
if (c.currentResult > c.noOfVotes) {
c.executed = true;
require(c.recipient.call.value(c.amount)(transactionBytecode));
c.candidateWin = true;
} else {
c.candidateWin = false;
}
Tallyvotes(candidateId, c.currentResult, c.noOfVotes, c.candidateWin);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment