-
Notifications
You must be signed in to change notification settings - Fork 0
/
Election.sol
45 lines (31 loc) · 1003 Bytes
/
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
pragma solidity 0.5.16;
contract Election {
struct Candidate {
uint id;
string name;
uint voteCount;
}
uint256 lastRun=now;
mapping(address => bool) public voters;
mapping(uint => Candidate) public candidates;
uint public candidatesCount;
event votedEvent (
uint indexed _candidateId
);
constructor () public {
addCandidate("Candidate 1");
addCandidate("Candidate 2");
}
function addCandidate (string memory _name) private {
candidatesCount ++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}
function vote (uint _candidateId) public {
require(block.timestamp - lastRun < 2 minutes, 'Time up');
require(!voters[msg.sender]);
require(_candidateId > 0 && _candidateId <= candidatesCount);
voters[msg.sender] = true;
candidates[_candidateId].voteCount ++;
emit votedEvent(_candidateId);
}
}