Skip to content

Instantly share code, notes, and snippets.

@ubinix-warun
Forked from kkdai/ballot.sol
Created November 1, 2020 10:29
Show Gist options
  • Save ubinix-warun/a61d02656b313e6548548d2f5837fda9 to your computer and use it in GitHub Desktop.
Save ubinix-warun/a61d02656b313e6548548d2f5837fda9 to your computer and use it in GitHub Desktop.

Revisions

  1. @kkdai kkdai created this gist Apr 21, 2019.
    52 changes: 52 additions & 0 deletions ballot.sol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    pragma solidity ^0.4.0;
    contract Ballot {

    struct Voter {
    uint weight;
    bool voted;
    uint8 vote;
    // address delegate;
    }
    struct Proposal {
    uint voteCount; // could add other data about proposal
    }

    address chairperson;
    mapping(address => Voter) voters;
    Proposal[] proposals;


    /// Create a new ballot with $(_numProposals) different proposals.
    function Ballot(uint8 _numProposals) public {
    chairperson = msg.sender;
    voters[chairperson].weight = 2;
    proposals.length = _numProposals;
    }

    /// Give $(toVoter) the right to vote on this ballot.
    /// May only be called by $(chairperson).
    function register(address toVoter) public {
    if (msg.sender != chairperson || voters[toVoter].voted) return;
    voters[toVoter].weight = 1;
    voters[toVoter].voted = false;
    }

    /// Give a single vote to proposal $(toProposal).
    function vote(uint8 toProposal) public {
    Voter storage sender = voters[msg.sender];
    if (sender.voted || toProposal >= proposals.length) return;
    sender.voted = true;
    sender.vote = toProposal;
    proposals[toProposal].voteCount += sender.weight;
    }

    function winningProposal() public constant returns (uint8 _winningProposal) {
    uint256 winningVoteCount = 0;
    for (uint8 prop = 0; prop < proposals.length; prop++)
    if (proposals[prop].voteCount > winningVoteCount) {
    winningVoteCount = proposals[prop].voteCount;
    _winningProposal = prop;
    }
    }
    }