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