Skip to content

Commit

Permalink
question: update contracto to match interface
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoAcosta committed Oct 12, 2024
1 parent 0bf110b commit a4cbc89
Showing 1 changed file with 12 additions and 50 deletions.
62 changes: 12 additions & 50 deletions src/voting/Question.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();

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

Expand All @@ -131,7 +92,8 @@ abstract contract Question is Ownable {
deadline: deadline,
totalVoteCount: totalVotes,
options: optionViews,
status: getStatus()
status: getStatus(),
owner: owner()
});
}

Expand All @@ -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);
Expand Down

0 comments on commit a4cbc89

Please sign in to comment.