Skip to content

Commit

Permalink
question: status
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoAcosta committed Oct 12, 2024
1 parent a554c00 commit 6b40b20
Showing 1 changed file with 34 additions and 21 deletions.
55 changes: 34 additions & 21 deletions src/voting/Question.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,32 @@ 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 {
string title;
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;
Expand All @@ -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,
Expand All @@ -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();

Expand All @@ -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 {
Expand All @@ -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;
}
}
}

0 comments on commit 6b40b20

Please sign in to comment.