-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase-contract-voting.sol
42 lines (31 loc) · 1.16 KB
/
base-contract-voting.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Voting {
mapping(address => uint256) private votes;
mapping(uint256 => uint256) private voteCount;
event VoteCast(address indexed voter, uint256 indexed option);
event VoteCounted(uint256 indexed option, uint256 count);
function vote(uint256 _option) external {
require(_option > 0, "Invalid option");
votes[msg.sender] = _option;
voteCount[_option]++;
emit VoteCast(msg.sender, _option);
}
function getVote(address _voter) external view returns (uint256) {
return votes[_voter];
}
function getCount(uint256 _option) external view returns (uint256) {
return voteCount[_option];
}
function countVotes(uint256 _option) external {
require(_option > 0, "Invalid option");
uint256 count = 0;
address[] memory voters = new address[](address(this).balance);
for (uint256 i = 0; i < voters.length; i++) {
if (votes[voters[i]] == _option) {
count++;
}
}
emit VoteCounted(_option, count);
}
}