-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElectionManager.sol
131 lines (102 loc) · 3.5 KB
/
ElectionManager.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
*Submitted for verification at Etherscan.io on 2020-11-24
*/
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
contract ElectionManager{
struct Election{
uint id;
uint total;
string name;
uint optionsCount;
string[] options;
string json;
uint[] optionVotes;
mapping(address => uint) userVotes;
}
struct VoteOption{
string name;
uint id;
}
mapping(address => bool) public voters;
mapping(address => bool) public admins;
address[] public adminsList;
mapping(uint => Election) public elections;
uint public lastElectionId = 1;
uint public lastAdminId = 1;
uint256 public voteFee = 0.01 ether;
event NewElection(string name, uint id);
event UserVoteEvent(address user, uint electionId,
string electionName, uint vote, string voteName);
constructor(){
admins[msg.sender] = true;
adminsList.push(msg.sender);
}
receive () external payable {
}
modifier isAdmin(){
require(admins[msg.sender], "You are not admin");
_;
}
modifier isVoter(){
require(voters[msg.sender], "You are not voter");
_;
}
function adminAddVoter(address _addr) isAdmin public payable{
voters[_addr] = true;
require(msg.value > 0, "The transaction amount should be over 0.");
require(msg.value == voteFee, "The transaction amount is not matched with contract amount.");
payable(_addr).transfer(msg.value);
}
function adminRemoveVoter(address _addr) isAdmin public{
voters[_addr] = false;
}
function adminAddAdmin(address _addr) isAdmin public{
admins[_addr] = true;
lastAdminId++;
adminsList.push(_addr);
}
function adminSetVoteFee(uint256 _fee) isAdmin public{
voteFee = _fee;
}
function adminAddElection(string memory _name, string[] memory _options, string memory _json) isAdmin public{
Election storage e = elections[lastElectionId++];
e.name = _name;
e.id = lastElectionId;
e.json = _json;
for(uint i = 0; i< _options.length; i++){
e.options.push(_options[i]);
e.optionVotes.push(0);
}
emit NewElection(_name, lastElectionId-1);
}
function vote(uint _electionId, uint _vote) isVoter public{
require(elections[_electionId].id > 0, "Invalid Election");
require(elections[_electionId].userVotes[msg.sender] <= 0, "Already voted");
elections[_electionId].total += 1;
elections[_electionId].optionVotes[_vote] += 1;
elections[_electionId].userVotes[msg.sender] = _vote;
emit UserVoteEvent(msg.sender, _electionId,
elections[_electionId].name,
_vote, elections[_electionId].options[_vote-1]);
}
function userVote(address _addr, uint _electionId) public view returns(uint _vote){
_vote = elections[_electionId].userVotes[_addr];
}
function getAdmins() public view returns(address[] memory _admins){
_admins = adminsList;
}
function electionInfo(uint _id) public view returns(
string[] memory _options,
uint[] memory _optionVotes,
uint _total,
string memory _name,
string memory _json
){
_options = elections[_id].options;
_total = elections[_id].total;
_name = elections[_id].name;
_optionVotes = elections[_id].optionVotes;
_json = elections[_id].json;
}
}