Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. pragma solidity ^0.5.2;
  2. pragma experimental ABIEncoderV2;
  3.  
  4. contract voter {
  5. uint[] public votes;
  6. //store votes .. array of integers
  7. string[] public options;
  8. //option for which a user can cast a vote
  9. mapping (address => bool) hasVoted;
  10.  
  11.  
  12. constructor(string[] memory _options) public {
  13. options = _options;
  14. votes.length = options.length;
  15.  
  16. }
  17.  
  18. function vote(uint option) public {
  19. require(0 <= option && option < options.length, "Invalid option");
  20. require(!hasVoted[msg.sender], "account has already voted" );
  21. votes[option] = votes[option] + 1;
  22. hasVoted[msg.sender] = true;
  23.  
  24. }
  25.  
  26. function getOptions() public view returns (string[] memory ){
  27.  
  28. return options;
  29. }
  30. function getVotes() public view returns (uint[] memory) {
  31. return votes;
  32.  
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement