-
Notifications
You must be signed in to change notification settings - Fork 0
/
Election.sol
57 lines (43 loc) · 1.22 KB
/
Election.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
pragma solidity ^0.8.3;
// SPDX-License-Identifier: MIT
contract Election {
struct Candidate {
string name;
uint voteCount;
}
struct Voter {
bool authorised;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public totalVote;
constructor(string memory _name) {
owner = msg.sender;
electionName = _name;
}
modifier ownerOnly() {
require(msg.sender == owner);
_;
}
function addCandidate(string memory _name) ownerOnly public {
candidates.push(Candidate(_name, 0));
}
function getCandidateCount() public view returns(uint) {
return candidates.length;
}
function authorise(address _person) ownerOnly public {
voters[_person].authorised = true;
}
function vote(uint _candidateIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorised);
voters[msg.sender].vote = _candidateIndex;
voters[msg.sender].voted = true;
candidates[_candidateIndex].voteCount += 1;
totalVote += 1;
}
}