Guest User

Untitled

a guest
Jan 19th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. //added cmt
  2. pragma solidity >=0.4.22 <0.6.0;
  3. contract Ballot {
  4.  
  5. struct Voter {
  6. uint weight;
  7. bool voted;
  8. uint8 vote;
  9. address delegate;
  10. }
  11. struct Proposal {
  12. uint voteCount;
  13. }
  14.  
  15. address chairperson;
  16. mapping(address => Voter) voters;
  17. Proposal[] proposals;
  18.  
  19. /// Create a new ballot with $(_numProposals) different proposals.
  20. constructor(uint8 _numProposals) public {
  21. chairperson = msg.sender;
  22. voters[chairperson].weight = 1;
  23. proposals.length = _numProposals;
  24. }
  25.  
  26. /// Give $(toVoter) the right to vote on this ballot.
  27. /// May only be called by $(chairperson).
  28. function giveRightToVote(address toVoter) public {
  29. if (msg.sender != chairperson || voters[toVoter].voted) return;
  30. voters[toVoter].weight = 1;
  31. }
  32.  
  33. /// Delegate your vote to the voter $(to).
  34. function delegate(address to) public {
  35. Voter storage sender = voters[msg.sender]; // assigns reference
  36. if (sender.voted) return;
  37. while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
  38. to = voters[to].delegate;
  39. if (to == msg.sender) return;
  40. sender.voted = true;
  41. sender.delegate = to;
  42. Voter storage delegateTo = voters[to];
  43. if (delegateTo.voted)
  44. proposals[delegateTo.vote].voteCount += sender.weight;
  45. else
  46. delegateTo.weight += sender.weight;
  47. }
  48.  
  49. /// Give a single vote to proposal $(toProposal).
  50. function vote(uint8 toProposal) public {
  51. Voter storage sender = voters[msg.sender];
  52. if (sender.voted || toProposal >= proposals.length) return;
  53. sender.voted = true;
  54. sender.vote = toProposal;
  55. proposals[toProposal].voteCount += sender.weight;
  56. }
  57.  
  58. function winningProposal() public view returns (uint8 _winningProposal) {
  59. uint256 winningVoteCount = 0;
  60. for (uint8 prop = 0; prop < proposals.length; prop++)
  61. if (proposals[prop].voteCount > winningVoteCount) {
  62. winningVoteCount = proposals[prop].voteCount;
  63. _winningProposal = prop;
  64. }
  65. }
  66. }
Add Comment
Please, Sign In to add comment