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