· 6 years ago · Jul 16, 2019, 02:41 AM
1pragma solidity ^0.4.18;
2
3//////////////////////// Voting.sol - 1 ////////////////////////////////////////////
4
5// written for Solidity version 0.4.18 and above that doesn't break functionality
6contract Voting {
7
8 // an event that is called whenever a Candidate is added so the frontend could
9 // appropriately display the candidate with the right element id (it is used
10 // to vote for the candidate, since it is one of arguments for the function
11 //"vote")
12 event AddedCandidate(uint candidateID);
13
14 // describes a Voter, which has an id and the ID of the candidate they voted for
15 address owner;
16
17 function Voting() public {
18 owner=msg.sender;
19 }
20
21//////////////////////// Voting.sol - 2 ////////////////////////////////////////////
22
23 modifier onlyOwner {
24 // describes a Candidate
25 require(msg.sender == owner);
26 _;
27 }
28
29 struct Voter {
30 uint candidateIDVote; // this flag will help authorization of voter
31 bool hasVoted; // this flag will help to keep track of 1 voter - 1 vote
32 }
33
34 // describes a Candidate
35 struct Candidate {
36 string name;
37 string party;
38 uint noOFVotes;
39 bool doesExist; // "bool doesExist" is to check if this Struct exists
40 }
41
42 // This is so we can keep track of the candidates
43
44//////////////////////// Voting.sol - 3 ////////////////////////////////////////////
45
46 // These state variables are used keep track of the number of Candidates/Voters
47 // and used to as a way to index them
48 uint numCandidates; // declares a state variable - number Of Candidates
49 uint numVoters;
50 uint numOfVotes;
51
52 // Think of these as a hash table, with the key as a uint and value of
53 // the struct Candidate/Voter. These mappings will be used in the majority
54 // of our transactions/calls
55 // These mappings will hold all the candidates and Voters respectively
56 mapping (uint => Candidate) candidates;
57 mapping (address => Voter) voters;
58
59
60 //////////////////////// Voting.sol - 4 ////////////////////////////////////////////
61
62 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
63 *
64 These functions perform transactions, editing the mappings *
65 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
66
67 function addCandidate(string name, string party) onlyOwner public {
68 // candidateID is the return variable
69 uint candidateID = numCandidates++;
70 // Create new Candidate Struct with name and saves it to storage.
71 candidates[candidateID] = Candidate(name,party,0,true);
72 AddedCandidate(candidateID);
73 }
74
75 //////////////////////// Voting.sol - 5 ////////////////////////////////////////////
76 function vote(uint candidateID) public {
77 // checks if the struct exists for that candidate
78 require(!voters[msg.sender].hasVoted);// this statement is to check this voter is not already voted
79 if (candidates[candidateID].doesExist == true) {
80 voters[msg.sender] = Voter(candidateID, true);
81 candidates[candidateID].noOFVotes++;
82 numOfVotes++;
83 numVoters++;
84 }
85 }
86
87 //////////////////////// Voting.sol - 6 ////////////////////////////////////////////
88 /* * * * * * * * * * * * * * * * * * * * * * * * * *
89 *
90 Getter Functions, marked by the key word "view" *
91 * * * * * * * * * * * * * * * * * * * * * * * * * */
92 // finds the total amount of votes for a specific candidate by looping
93 //through voters
94 function totalVotes(uint candidateID) view public returns (uint) {
95 return candidates[candidateID].noOFVotes;
96 }
97
98 //////////////////////// Voting.sol - 7 ////////////////////////////////////////////
99
100 function getNumOfCandidates() public view returns(uint) {
101 return numCandidates;
102 }
103
104 function getNumOfVoters() public view returns(uint) {
105 return numVoters;
106 }
107
108 // returns candidate information, including its ID, name, and party
109 function getCandidate(uint candidateID) public view returns(uint,string,string,uint)
110 {
111 return (candidateID,candidates[candidateID].name,candidates[candidateID].party,candidates[candidateID].noOFVotes);
112 }
113
114}