-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelection.sol
54 lines (44 loc) · 1.36 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
pragma solidity ^0.4.26;
contract Election {
address private _owner;
struct Candidate {
uint id;
string name;
uint voteCount;
}
// map candidatesCount to the Candidate
mapping(uint => Candidate) public candidates;
mapping(address => bool) public hasVoted;
uint public candidatesCount;
constructor() public {
_owner = msg.sender;
_addCandidate("Bob");
_addCandidate("Alice");
_addCandidate("@web3pastel");
}
function isOwner() public view returns(bool) {
return msg.sender == _owner;
}
function ownerAddCandidate(string _name) public {
require(msg.sender == _owner);
_addCandidate(_name);
}
function _addCandidate(string _name) private {
candidatesCount++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}
function msgSenderVote(uint _candidateId) public {
_vote(_candidateId);
}
function _vote(uint _candidateId) private {
if(msg.sender == _owner) {
candidates[_candidateId].voteCount++;
hasVoted[msg.sender] = true;
} else {
require(!hasVoted[msg.sender]);
require(_candidateId>0 && _candidateId<= candidatesCount);
candidates[_candidateId].voteCount++;
hasVoted[msg.sender] = true;
}
}
}