· 6 years ago · Jul 16, 2019, 03:37 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 isAuthorized;
32 bool hasVoted; // this flag will help to keep track of 1 voter - 1 vote
33 }
34
35 // describes a Candidate
36 struct Candidate {
37 string name;
38 string party;
39 uint noOFVotes;
40 bool doesExist; // "bool doesExist" is to check if this Struct exists
41 }
42
43 // This is so we can keep track of the candidates
44
45//////////////////////// Voting.sol - 3 ////////////////////////////////////////////
46
47 // These state variables are used keep track of the number of Candidates/Voters
48 // and used to as a way to index them
49 uint numCandidates; // declares a state variable - number Of Candidates
50 uint numVoters;
51 uint numOfVotes;
52
53 // Think of these as a hash table, with the key as a uint and value of
54 // the struct Candidate/Voter. These mappings will be used in the majority
55 // of our transactions/calls
56 // These mappings will hold all the candidates and Voters respectively
57 mapping (uint => Candidate) candidates;
58 mapping (address => Voter) voters;
59
60
61//////////////////////// Voting.sol - 4 ////////////////////////////////////////////
62
63 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
64 *
65 These functions perform transactions, editing the mappings *
66 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
67
68 function addCandidate(string name, string party) onlyOwner public {
69 // candidateID is the return variable
70 uint candidateID = numCandidates++;
71 // Create new Candidate Struct with name and saves it to storage.
72 candidates[candidateID] = Candidate(name,party,0,true);
73 AddedCandidate(candidateID);
74 }
75
76//////////////////////// Voting.sol - 5 ////////////////////////////////////////////
77
78 function vote(uint candidateID) public {
79 // checks if the struct exists for that candidate
80 require(!voters[msg.sender].hasVoted);// this statement is to check this voter is not already voted
81 require(voters[msg.sender].isAuthorized);
82 if (candidates[candidateID].doesExist == true) {
83 voters[msg.sender] = Voter(candidateID, true, true);
84 candidates[candidateID].noOFVotes++;
85 numOfVotes++;
86 numVoters++;
87 }
88 }
89
90//////////////////////// Voting.sol - 6 ////////////////////////////////////////////
91
92 /* * * * * * * * * * * * * * * * * * * * * * * * * *
93 *
94 Getter Functions, marked by the key word "view" *
95 * * * * * * * * * * * * * * * * * * * * * * * * * */
96 // finds the total amount of votes for a specific candidate by looping
97 //through voters
98 function totalVotes(uint candidateID) view public returns (uint) {
99 return candidates[candidateID].noOFVotes;
100 }
101
102//////////////////////// Voting.sol - 7 ////////////////////////////////////////////
103
104 function getNumOfCandidates() public view returns(uint) {
105 return numCandidates;
106 }
107
108 function getNumOfVoters() public view returns(uint) {
109 return numVoters;
110 }
111
112 // returns candidate information, including its ID, name, and party
113 function getCandidate(uint candidateID) public view returns(uint,string,string,uint)
114 {
115 return (candidateID,candidates[candidateID].name,candidates[candidateID].party,candidates[candidateID].noOFVotes);
116 }
117
118//////////////////////// Voting - Assignment ////////////////////////////////////////////
119
120 function Authorize(address _Voter) onlyOwner public {
121 voters[_Voter].isAuthorized = true;
122 }
123
124}