-
Notifications
You must be signed in to change notification settings - Fork 0
/
USElections.sol
72 lines (59 loc) · 1.92 KB
/
USElections.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
72
/// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
pragma abicoder v2;
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
contract USElections is Ownable(msg.sender) {
uint8 public constant BIDEN = 1;
uint8 public constant TRUMP = 2;
bool public electionEnded;
mapping(uint8 => uint8) public seats;
mapping(string => bool) public resultSubmitted;
mapping(uint8 => string) public currentLeaderToString;
struct StateResult {
string name;
uint votesBiden;
uint votesTrump;
uint8 stateSeats;
}
event LogStateResults(uint8 winner, uint8 stateSeats, string state);
event LogElectionEnded(uint winner);
modifier onlyActiveElection() {
require(!electionEnded, "The election has ended already!");
_;
}
function submitStateResult(
StateResult calldata result
) public onlyOwner onlyActiveElection {
require(result.stateSeats > 0, "States must have at least 1 seat!");
require(
result.votesBiden != result.votesTrump,
"There can not be a tie!"
);
require(
!resultSubmitted[result.name],
"This state result has been already submitted!"
);
uint8 winner;
if (result.votesBiden > result.votesTrump) {
winner = BIDEN;
} else {
winner = TRUMP;
}
seats[winner] += result.stateSeats;
resultSubmitted[result.name] = true;
emit LogStateResults(winner, result.stateSeats, result.name);
}
function currentLeader() public view returns (uint8) {
if (seats[BIDEN] > seats[TRUMP]) {
return BIDEN;
}
if (seats[BIDEN] < seats[TRUMP]) {
return TRUMP;
}
return 0;
}
function endElection() public onlyOwner {
electionEnded = true;
emit LogElectionEnded(currentLeader());
}
}