diff --git a/src/voting/FixedQuestion.sol b/src/voting/FixedQuestion.sol index 3e7bfc3..1aa9e27 100644 --- a/src/voting/FixedQuestion.sol +++ b/src/voting/FixedQuestion.sol @@ -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; + } } diff --git a/src/voting/OpenQuestion.sol b/src/voting/OpenQuestion.sol index 971a39e..e8a2bc6 100644 --- a/src/voting/OpenQuestion.sol +++ b/src/voting/OpenQuestion.sol @@ -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); @@ -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; + } } diff --git a/src/voting/Question.sol b/src/voting/Question.sol index 9ae6c78..fe93c83 100644 --- a/src/voting/Question.sol +++ b/src/voting/Question.sol @@ -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) }); } @@ -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); } diff --git a/src/voting/interfaces/IQuestion.sol b/src/voting/interfaces/IQuestion.sol index 829d512..9a41f62 100644 --- a/src/voting/interfaces/IQuestion.sol +++ b/src/voting/interfaces/IQuestion.sol @@ -53,6 +53,7 @@ interface IQuestion { uint256 userOptionVoted; uint256 userPointsCurrent; uint256 userPointsDeadline; + bool userCanAddOption; } // Events