-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElection.sol
71 lines (51 loc) · 1.65 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//this is the solidity file that has the election smart contract and the functions that are called in the dart files
//SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.5.0 < 0.9.0;
contract Election{
struct Candidate{
string name;
uint numVotes;
}
struct Voter{
string name;
bool authorised;
uint whom;
bool voted;
}
address public owner;
string public electionName;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
function startElection(string memory _electionName) public{
owner = msg.sender;
electionName = _electionName;
}
function addCandidate(string memory _candidateName )ownerOnly public{
candidates.push(Candidate(_candidateName, 0));
}
function authorizeVoter(address _voterAddress) ownerOnly public{
voters[_voterAddress].authorised= true;
}
function getNumCandidates() public view returns(uint){
return candidates.length;
}
function vote(uint candidateIndex) public{
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorised);
voters[msg.sender].whom = candidateIndex;
voters[msg.sender].voted = true;
candidates[candidateIndex].numVotes++;
totalVotes++;
}
function getTotalVotes()public view returns(uint) {
return totalVotes;
}
function candidateInfo(uint index) public view returns(Candidate memory){
return candidates[index];
}
}