daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Feb 17th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.5.0;
  2.    
  3. // storing the accounts that have already voted
  4. mapping(address=>bool) public voters;
  5. // store a candidate
  6. mapping(uint=>Candidate) public candidates; // creating the variable of that mapping
  7. // fetch candidate
  8.  
  9.  
  10. // get candidate count
  11. uint public candidatesCount;     // this is the counter
  12.  
  13. // voting event
  14. event votedEvent (
  15.     uint indexed _candidateId
  16. );
  17.  
  18. constructor() public{
  19.    // we dont want any one else to add the candidates therefore we call the addCandidate function in the constructor
  20.     addCandidates("Candidate 1");
  21.     addCandidates("Candidate 2");
  22.     addCandidates("Candidate 3");
  23.     // candidates count is 3
  24. }
  25. // adding the candidate to the mapping
  26. function addCandidates(string memory _name) private {
  27.     candidatesCount++; // this will increment the candidate counter
  28.     candidates[candidatesCount] = Candidate(candidatesCount, _name, 0); // assiging value to the candidate structure
  29. }
  30. function vote(uint _candidateId) public{
  31.     //check that the address has not voted before
  32.     require(!voters[msg.sender]);
  33.  
  34.     // require a valid candidate
  35.     require(_candidateId > 0 && _candidateId <= candidatesCount);
  36.     // record that voter has voted
  37.     voters[msg.sender] = true; // msg.sender has the account of the voter
  38.  
  39.     // update candidate vote count
  40.     candidates[_candidateId].voteCount++;
  41.  
  42.     emit votedEvent(_candidateId);
  43. }
  44.    
  45. pragma solidity ^0.4.2;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top