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