Advertisement
Guest User

Solidity Voting Contract Practice

a guest
Sep 13th, 2022
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | Cryptocurrency | 0 0
  1. //SPDX-License-Identifier: GPL-3.0
  2. pragma solidity 0.8.7;
  3.  
  4. import "hardhat/console.sol";
  5.  
  6. contract Voting {
  7. address public owner;
  8. address[5] private allAddresses;
  9. uint private voteEnd;
  10. uint private totalVotes;
  11. string[5] private allVotes;
  12.  
  13. struct Voter {
  14. bool voted;
  15. string vote;
  16. }
  17.  
  18. constructor () {
  19. owner = msg.sender;
  20. voteEnd = block.timestamp + 20;
  21. totalVotes = 0;
  22. }
  23.  
  24. modifier notOwner {
  25. require(msg.sender != owner, "Owners cannot participate in the auction.");
  26. _;
  27. }
  28.  
  29. modifier votesDone {
  30. require(block.timestamp >= voteEnd, "The vote is not yet finished.");
  31. _;
  32. }
  33.  
  34. modifier onlyOwner {
  35. require(msg.sender == owner, "You are not the owner.");
  36. _;
  37. }
  38.  
  39. mapping(address => Voter) public voter;
  40.  
  41. function submitVote (address _voter, string memory _vote) public notOwner {
  42. require(voter[_voter].voted == false, "You've already voted.");
  43. voter[_voter].vote = _vote;
  44. voter[_voter].voted = true;
  45. allVotes[totalVotes] = _vote;
  46. allAddresses[totalVotes] = _voter;
  47. totalVotes++;
  48. }
  49.  
  50. function getVotes () public votesDone onlyOwner view {
  51. for (uint _x = 0; _x < totalVotes; _x++) {
  52. console.log(allAddresses[_x], allVotes[_x]);
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement