-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollingSystem.sol
More file actions
57 lines (37 loc) · 1.34 KB
/
Copy pathPollingSystem.sol
File metadata and controls
57 lines (37 loc) · 1.34 KB
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract PollingSystem {
struct Option {
string name;
uint256 voteCount;
}
struct Voter {
bool hasVoted;
}
mapping(address => Voter) public voters;
Option[] public options;
event Voted(address indexed voter, uint256 indexed optionIndex);
modifier hasNotVoted() {
require(!voters[msg.sender].hasVoted, "You have already voted");
_;
}
modifier validOption(uint256 _optionIndex) {
require(_optionIndex < options.length, "Invalid option");
_;
}
constructor() {
options.push(Option({ name: "Iyad Koteich", voteCount: 0 }));
options.push(Option({ name: "Justin Trudeau", voteCount: 0 }));
}
function vote(uint256 _optionIndex) external hasNotVoted validOption(_optionIndex) {
voters[msg.sender].hasVoted = true;
options[_optionIndex].voteCount++;
emit Voted(msg.sender, _optionIndex);
}
function getVoteCount(uint256 _optionIndex) external view validOption(_optionIndex) returns (uint256) {
return options[_optionIndex].voteCount;
}
function getOptionCount() external view returns (uint256) {
return options.length;
}
}