-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizeElection.sol
69 lines (49 loc) · 1.88 KB
/
organizeElection.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// SPDX-License-Identifier: GPL-3.0
pragma solidity >0.5.0 <0.9.0;
contract Election{
struct candidate{
string name;
uint voteCount;
}
struct voter{
bool authorized;
bool voted;
uint vote;
}
address public owner; // owner who deploy the smart contract
string public electionName;
mapping(address => voter) public voters;
candidate[] public candidates;
uint public totalVoters;
constructor(string memory name){ // it s constructor function when SC deploy its excuted automatically
owner = msg.sender;
electionName = name;
}
modifier onlyOwner(){ // we use modifier at those place were we need that this method is only called by the owner
require(msg.sender == owner , "you are not the contract owner");
_;
}
function addCandidate(string memory _name) public onlyOwner{
candidates.push(candidate(_name,0));
}
// in the blockchain reading the blockChain is free
function getNumberOfCandidates()view public returns(uint){
return candidates.length;
}
// owner of the contract otherized the any person to cost the vote
function authorizedVoter(address _voterPerson)public onlyOwner{
voters[_voterPerson].authorized = true;
}
// now any person who autherized he do vot for any person
function doVote(uint voteIndex)public{
require(voters[msg.sender].voted == false, "you already voted");
require(voters[msg.sender].authorized == true , "you are not athorized by the owner");
voters[msg.sender].voted == true;
voters[msg.sender].vote = voteIndex;
candidates[voteIndex].voteCount += 1;
totalVoters += 1;
}
// function destroy() onlyOwner public {
// selfdestruct(owner);
// }
}