Skip to content

Commit

Permalink
feat(quests): implement cancel operation for quests [BOOST-3960]
Browse files Browse the repository at this point in the history
  • Loading branch information
ccashwell committed May 13, 2024
1 parent d0e7c24 commit 03f2b04
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 77 deletions.
14 changes: 5 additions & 9 deletions contracts/Quest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,12 @@ contract Quest is ReentrancyGuardUpgradeable, PausableUpgradeable, Ownable, IQue
/*//////////////////////////////////////////////////////////////
EXTERNAL UPDATE
//////////////////////////////////////////////////////////////*/
/// @notice Pauses the Quest
/// @dev Only the owner of the Quest can call this function. Also requires that the Quest has started (not by date, but by calling the start function)
function pause() external onlyOwner {
_pause();
}

/// @notice Unpauses the Quest
/// @dev Only the owner of the Quest can call this function. Also requires that the Quest has started (not by date, but by calling the start function)
function unPause() external onlyOwner {
_unpause();
/// @notice Cancels the Quest by setting the end time to 15 minutes from the current time and pausing the Quest. If the Quest has not yet started, it will end immediately.
/// @dev Only the owner of the Quest can call this function.
function cancel() external onlyOwner whenNotPaused whenNotEnded {
_pause();
endTime = startTime > block.timestamp ? block.timestamp : block.timestamp + 15 minutes;
}

/// @dev transfers rewards to the account, can only be called once per account per quest and only by the quest factory
Expand Down
14 changes: 5 additions & 9 deletions contracts/Quest1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ contract Quest1155 is ERC1155Holder, ReentrancyGuardUpgradeable, PausableUpgrade
/*//////////////////////////////////////////////////////////////
EXTERNAL UPDATE
//////////////////////////////////////////////////////////////*/
/// @notice Pauses the Quest
/// @dev Only the owner of the Quest can call this function. Also requires that the Quest has started (not by date, but by calling the start function)
function pause() external onlyOwner onlyStarted {

/// @notice Cancels the Quest by setting the end time to 15 minutes from the current time and pausing the Quest. If the Quest has not yet started, it will end immediately.
/// @dev Only the owner of the Quest can call this function.
function cancel() external onlyOwner whenNotPaused whenNotEnded {
_pause();
endTime = startTime > block.timestamp ? block.timestamp : block.timestamp + 15 minutes;
}

/// @notice Queues the quest by marking it ready to start at the contract level. Marking a quest as queued does not mean that it is live. It also requires that the start time has passed
Expand Down Expand Up @@ -142,12 +144,6 @@ contract Quest1155 is ERC1155Holder, ReentrancyGuardUpgradeable, PausableUpgrade
if (questFee > 0) protocolFeeRecipient.safeTransferETH(questFee);
}

/// @notice Unpauses the Quest
/// @dev Only the owner of the Quest can call this function. Also requires that the Quest has started (not by date, but by calling the start function)
function unPause() external onlyOwner onlyStarted {
_unpause();
}

/// @dev Function that transfers all 1155 tokens in the contract to the owner (creator), and eth to the protocol fee recipient and the owner
/// @notice This function can only be called after the quest end time.
function withdrawRemainingTokens() external onlyEnded {
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IQuest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface IQuest {
function startTime() external view returns (uint256);
function endTime() external view returns (uint256);
function singleClaim(address account) external;
function cancel() external;
function rewardToken() external view returns (address);
function rewardAmountInWei() external view returns (uint256);
function totalTransferAmount() external view returns (uint256);
Expand Down
3 changes: 1 addition & 2 deletions contracts/interfaces/IQuest1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ interface IQuest1155 {
function rewardToken() external view returns (address);

// Update Functions
function pause() external;
function cancel() external;
function queue() external;
function singleClaim(address account_) external;
function unPause() external;
function withdrawRemainingTokens() external;
}
40 changes: 12 additions & 28 deletions test/Quest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,26 @@ contract TestQuest is Test, TestUtils, Errors, Events {
}

/*//////////////////////////////////////////////////////////////
PAUSE
CANCEL
//////////////////////////////////////////////////////////////*/
function test_pause() public {
function test_cancel() public {
vm.prank(questFactoryMock);
quest.pause();
quest.cancel();
assertTrue(quest.paused(), "paused should be true");
assertEq(quest.endTime(), block.timestamp, "endTime should be now (quest not started)");
}

function test_RevertIf_pause_Unauthorized() public {
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));
quest.pause();
}

/*//////////////////////////////////////////////////////////////
UNPAUSE
//////////////////////////////////////////////////////////////*/
function test_unpause() public {
vm.startPrank(questFactoryMock);
quest.pause();
quest.unPause();
assertFalse(quest.paused(), "paused should be false");
vm.stopPrank();
function test_cancel_afterStarted() public {
vm.warp(START_TIME);
vm.prank(questFactoryMock);
quest.cancel();
assertTrue(quest.paused(), "paused should be true");
assertEq(quest.endTime(), block.timestamp + 15 minutes, "endTime should be 15 minutes from now");
}

function test_RevertIf_unpause_Unauthorized() public {
function test_RevertIf_cancel_Unauthorized() public {
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));
quest.unPause();
quest.cancel();
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -253,15 +246,6 @@ contract TestQuest is Test, TestUtils, Errors, Events {
quest.singleClaim(participant);
}

function test_RevertIf_singleClaim_whenNotPaused() public {
vm.startPrank(questFactoryMock);
quest.pause();
vm.warp(START_TIME);
vm.expectRevert("Pausable: paused");
quest.singleClaim(participant);
vm.stopPrank();
}

/*//////////////////////////////////////////////////////////////
WITHDRAWREMAININGTOKENS
//////////////////////////////////////////////////////////////*/
Expand Down
37 changes: 8 additions & 29 deletions test/Quest1155.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,31 +109,22 @@ contract TestQuest1155 is Test, Errors, Events, TestUtils {
/*//////////////////////////////////////////////////////////////
PAUSE
//////////////////////////////////////////////////////////////*/
function test_pause() public {
function test_cancel() public {
vm.prank(questFactoryMock);
quest.pause();
quest.cancel();
assertTrue(quest.paused(), "paused should be true");
assertEq(quest.endTime(), block.timestamp + 15 minutes, "endTime should be 15 minutes from now");
}

function test_RevertIf_pause_Unauthorized() public {
function test_cancel_notStarted() public {
vm.warp(START_TIME - 1);
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));
quest.pause();
}

/*//////////////////////////////////////////////////////////////
UNPAUSE
//////////////////////////////////////////////////////////////*/
function test_unpause() public {
vm.startPrank(questFactoryMock);
quest.pause();
quest.unPause();
assertFalse(quest.paused(), "paused should be false");
vm.stopPrank();
quest.cancel();
}

function test_RevertIf_unpause_Unauthorized() public {
function test_RevertIf_cancel_Unauthorized() public {
vm.expectRevert(abi.encodeWithSelector(Unauthorized.selector));
quest.unPause();
quest.cancel();
}

// /*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -199,18 +190,6 @@ contract TestQuest1155 is Test, Errors, Events, TestUtils {
quest.singleClaim(participant);
}

function test_RevertIf_singleClaim_whenNotPaused() public {
vm.deal(address(quest), LARGE_ETH_AMOUNT);
vm.prank(questFactoryMock);
quest.queue();
vm.startPrank(questFactoryMock);
quest.pause();
vm.warp(START_TIME);
vm.expectRevert("Pausable: paused");
quest.singleClaim(participant);
vm.stopPrank();
}

// /*//////////////////////////////////////////////////////////////
// WITHDRAWREMAININGTOKENS
// //////////////////////////////////////////////////////////////*/
Expand Down

0 comments on commit 03f2b04

Please sign in to comment.