-
Notifications
You must be signed in to change notification settings - Fork 45
/
Poll.sol
38 lines (32 loc) · 1.07 KB
/
Poll.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
// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
contract Poll {
// The block at which the poll ends and votes can no longer be submitted.
uint256 public endBlock;
// Vote is emitted when an account submits a vote with 'choiceID'.
// This event can be indexed to tally all votes for each choiceID
event Vote(address indexed voter, uint256 choiceID);
modifier isActive() {
require(block.number <= endBlock, "poll is over");
_;
}
constructor(uint256 _endBlock) {
endBlock = _endBlock;
}
/**
* @dev Vote for the poll's proposal.
* Reverts if the poll period is over.
* @param _choiceID the ID of the option to vote for
*/
function vote(uint256 _choiceID) external isActive {
emit Vote(msg.sender, _choiceID);
}
/**
* @dev Destroy the Poll contract after the poll has finished
* Reverts if the poll is still active
*/
function destroy() external {
require(block.number > endBlock, "poll is active");
selfdestruct(payable(msg.sender));
}
}