From 6b40b20e66a1c2a484ab7db3c8f0234a8b06462f Mon Sep 17 00:00:00 2001 From: "nicoacosta.eth" Date: Sat, 12 Oct 2024 01:59:28 -0300 Subject: [PATCH] question: status --- src/voting/Question.sol | 55 +++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/src/voting/Question.sol b/src/voting/Question.sol index 10dafa6..d867920 100644 --- a/src/voting/Question.sol +++ b/src/voting/Question.sol @@ -5,10 +5,17 @@ import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; abstract contract Question is Ownable { - // State variables - string public title; - string public description; - uint256 public deadline; + // Errors + error VotingEnded(); + error AlreadyVoted(); + error InvalidOption(); + + // Enums + enum Status { + Null, + Active, + Ended + } // Structs struct Option { @@ -16,8 +23,14 @@ abstract contract Question is Ownable { string description; } + // State variables + string public title; + string public description; + uint256 public deadline; + IERC20 public immutable points; + // Arrays - Option[] public options; + Option[] private options; // Mappings mapping(address => uint256) public voterToOptionId; @@ -28,14 +41,6 @@ abstract contract Question is Ownable { event Voted(address indexed voter, uint256 indexed optionId, uint256 timestamp); event NewOption(address indexed proposer, uint256 indexed optionId, string title); - // Errors - error VotingEnded(); - error AlreadyVoted(); - error InvalidOption(); - - // IERC20 points variable - IERC20 public points; - // Constructor constructor( address initialOwner, @@ -54,7 +59,7 @@ abstract contract Question is Ownable { } // External functions - function vote(uint256 optionId) public virtual { + function vote(uint256 optionId) external virtual { if (block.timestamp >= deadline) revert VotingEnded(); if (optionId == 0 || optionId > options.length) revert InvalidOption(); @@ -65,18 +70,18 @@ abstract contract Question is Ownable { emit Voted(msg.sender, optionId, timestamp); } - function getOptions() public view virtual returns (Option[] memory) { + function getOptions() external view virtual returns (Option[] memory) { return options; } - function getOption(uint256 optionId) public view virtual returns (Option memory) { - if (optionId == 0 || optionId > options.length) revert InvalidOption(); - return options[optionId - 1]; + function getOption(uint256 optionId) external view virtual returns (Option memory) { + if (optionId >= options.length) revert InvalidOption(); + return options[optionId]; } - function getOptionVoteCount(uint256 optionId) public view virtual returns (uint256); + function getOptionVoteCount(uint256 optionId) external view virtual returns (uint256); - function getOptionPointsAccrued(uint256 optionId) public view virtual returns (uint256); + function getOptionPointsAccrued(uint256 optionId) external view virtual returns (uint256); // External functions (onlyOwner) function updateTitle(string memory _title) external onlyOwner { @@ -100,8 +105,16 @@ abstract contract Question is Ownable { function _vote(uint256 optionId) internal virtual; function _addOption(string memory _title, string memory _description) internal virtual { - uint256 optionId = options.length + 1; + uint256 optionId = options.length; options.push(Option(_title, _description)); emit NewOption(msg.sender, optionId, _title); } + + function getStatus() external view returns (Status) { + if (block.timestamp < deadline) { + return Status.Active; + } else { + return Status.Ended; + } + } }