Advertisement
Guest User

VOTE

a guest
Nov 22nd, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. pragma solidity ^0.4.25;
  2. pragma experimental ABIEncoderV2;
  3. contract VoteFactory {
  4. address[] public deployedVotes; //เอาไว้เก็บaddressของsmart contract
  5.  
  6. function createVote(string namevote, string description,bytes32[] candidate) public {
  7. address newVote = new Vote(namevote, description, candidate, msg.sender);
  8. deployedVotes.push(newVote);
  9. }
  10. function getDeployedVotes() public view returns (address[]) {
  11. return deployedVotes;
  12. }
  13. }
  14.  
  15. contract Vote {
  16. struct Candidate {
  17. bytes32 candidate_name;
  18. uint candidate_score;
  19. }
  20. Candidate[] public candidate_list;
  21. address public manager;
  22. mapping (address => bool) public elector;
  23. string namevote;
  24. string description;
  25. bool complete;
  26.  
  27. modifier restricted(){
  28. require(msg.sender == manager);
  29. _;
  30. }
  31.  
  32. constructor (string name, string desc, bytes32[] cand,address creator) public {
  33. namevote = name;
  34. description = desc;
  35. manager = creator;
  36. complete = false;
  37. uint index=cand.length;
  38. for(uint i=0;i<index;i++){
  39. Candidate memory newcandidate = Candidate({
  40. candidate_name : cand[i],
  41. candidate_score : 0
  42. });
  43. candidate_list.push(newcandidate);
  44. }
  45. }
  46.  
  47. function setCandidate(bytes32[] cand) public restricted {
  48. uint index=cand.length;
  49. for(uint i=0;i<index;i++){
  50. createCandidate(cand[i]);
  51. }
  52. }
  53.  
  54. function createCandidate(bytes32 name) public restricted {
  55. Candidate memory newcandidate = Candidate({
  56. candidate_name : name,
  57. candidate_score : 0
  58. });
  59. candidate_list.push(newcandidate);
  60. }
  61.  
  62. function vote_candidate(uint index) public {
  63. Candidate storage candidate = candidate_list[index];
  64. require(complete == false);
  65. require(elector[msg.sender] == false);
  66. elector[msg.sender] = true;
  67. candidate.candidate_score++;
  68. }
  69.  
  70. function close_vote() public restricted {
  71. require(!complete);
  72. complete = true;
  73. }
  74.  
  75. function getScore(uint index) public view returns (uint) {
  76. Candidate storage candidate = candidate_list[index];
  77. return candidate.candidate_score;
  78. }
  79.  
  80. function getCandidate() public view returns (Candidate[]) {
  81. return candidate_list;
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement