Skip to content

Commit

Permalink
question view: add userCanAddOption
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoAcosta committed Oct 12, 2024
1 parent 414e367 commit 3bd7dc2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/voting/FixedQuestion.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ contract FixedQuestion is Question, IFixedQuestion {
) public view override(IQuestion, Question) returns (bool) {
return userVotes[voter] == optionId;
}

/// @inheritdoc Question
function canAddOption(address) public view override returns (bool) {
return false;
}
}
7 changes: 6 additions & 1 deletion src/voting/OpenQuestion.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ contract OpenQuestion is Question, IOpenQuestion {
/// @param _title The title of the new option
/// @param _description The description of the new option
function addOption(string memory _title, string memory _description) external override {
if (points.balanceAtTimestamp(msg.sender, deadline) < minPointsToAddOption) {
if (!canAddOption(msg.sender)) {
revert InsufficientPoints();
}
_addOption(_title, _description);
Expand Down Expand Up @@ -76,4 +76,9 @@ contract OpenQuestion is Question, IOpenQuestion {
minPointsToAddOption = _minPointsToAddOption;
emit MinPointsToAddOptionUpdated(_minPointsToAddOption);
}

/// @inheritdoc Question
function canAddOption(address user) public view override returns (bool) {
return points.balanceAtTimestamp(user, deadline) >= minPointsToAddOption;
}
}
8 changes: 7 additions & 1 deletion src/voting/Question.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ abstract contract Question is Ownable, IQuestion {
started: deploymentTime,
userOptionVoted: userOptionVoted,
userPointsCurrent: points.balanceOf(user),
userPointsDeadline: points.balanceAtTimestamp(user, deadline)
userPointsDeadline: points.balanceAtTimestamp(user, deadline),
userCanAddOption: canAddOption(user)
});
}

Expand Down Expand Up @@ -161,4 +162,9 @@ abstract contract Question is Ownable, IQuestion {

/// @inheritdoc IQuestion
function hasVoted(address voter, uint256 optionId) public view virtual returns (bool);

/// @dev Checks if a user can add an option
/// @param user The address of the user to check
/// @return bool True if the user can add an option, false otherwise
function canAddOption(address user) public view virtual returns (bool);
}
1 change: 1 addition & 0 deletions src/voting/interfaces/IQuestion.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface IQuestion {
uint256 userOptionVoted;
uint256 userPointsCurrent;
uint256 userPointsDeadline;
bool userCanAddOption;
}

// Events
Expand Down

0 comments on commit 3bd7dc2

Please sign in to comment.