From a4cbc89bcda104414bfc3cf6bcf1f7c0b55f2343 Mon Sep 17 00:00:00 2001 From: "nicoacosta.eth" Date: Sat, 12 Oct 2024 02:29:09 -0300 Subject: [PATCH] question: update contracto to match interface --- src/voting/Question.sol | 62 ++++++++--------------------------------- 1 file changed, 12 insertions(+), 50 deletions(-) diff --git a/src/voting/Question.sol b/src/voting/Question.sol index 2d686a4..25e0d64 100644 --- a/src/voting/Question.sol +++ b/src/voting/Question.sol @@ -3,56 +3,17 @@ pragma solidity ^0.8.20; import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import { IQuestion } from "./interfaces/IQuestion.sol"; +import { IPoints } from "../points/interfaces/IPoints.sol"; -abstract contract Question is Ownable { - // Type declarations - enum Status { - Null, - Active, - Ended - } - - struct Option { - string title; - string description; - address proposer; - } - - struct OptionView { - string title; - string description; - address proposer; - uint256 voteCount; - uint256 pointsAccrued; - bool userVoted; - } - - struct QuestionView { - string title; - string description; - uint256 deadline; - uint256 totalVoteCount; - OptionView[] options; - Status status; - } - +abstract contract Question is Ownable, IQuestion { // State variables string public title; string public description; uint256 public deadline; - IERC20 public immutable points; + IPoints public immutable points; Option[] private options; - // Events - event QuestionUpdated(string newTitle, string newDescription, uint256 newDeadline); - 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(); - // Constructor constructor( address initialOwner, @@ -64,14 +25,14 @@ abstract contract Question is Ownable { title = _title; description = _description; deadline = _deadline; - points = IERC20(_pointsAddress); + points = IPoints(_pointsAddress); // Add an empty option at index 0 options.push(Option("", "", msg.sender)); } // External functions - function vote(uint256 optionId) external virtual { + function vote(uint256 optionId) external { if (block.timestamp >= deadline) revert VotingEnded(); if (optionId == 0 || optionId > options.length) revert InvalidOption(); @@ -98,16 +59,16 @@ abstract contract Question is Ownable { } // External view functions - function getOptions() external view virtual returns (Option[] memory) { + function getOptions() external view returns (Option[] memory) { return options; } - function getOption(uint256 optionId) external view virtual returns (Option memory) { + function getOption(uint256 optionId) external view returns (Option memory) { if (optionId >= options.length) revert InvalidOption(); return options[optionId]; } - function getQuestionView(address user) external view virtual returns (QuestionView memory) { + function getQuestionView(address user) external view returns (QuestionView memory) { uint256 totalVotes = 0; OptionView[] memory optionViews = new OptionView[](options.length); @@ -131,7 +92,8 @@ abstract contract Question is Ownable { deadline: deadline, totalVoteCount: totalVotes, options: optionViews, - status: getStatus() + status: getStatus(), + owner: owner() }); } @@ -140,7 +102,7 @@ abstract contract Question is Ownable { function _vote(uint256 optionId) internal virtual; - function _addOption(string memory _title, string memory _description) internal virtual { + function _addOption(string memory _title, string memory _description) internal { uint256 optionId = options.length; options.push(Option(_title, _description, msg.sender)); emit NewOption(msg.sender, optionId, _title);