Skip to content

Commit

Permalink
save gas by not doing double checks
Browse files Browse the repository at this point in the history
  • Loading branch information
waynehoover committed Nov 15, 2023
1 parent e6ef614 commit f2a7072
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 22 deletions.
26 changes: 14 additions & 12 deletions contracts/Quest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,25 +98,26 @@ contract Quest is ReentrancyGuardUpgradeable, PausableUpgradeable, Ownable, IQue
/*//////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
/// @notice Prevents reward withdrawal until the Quest has ended
modifier onlyWithdrawAfterEnd() {
if (block.timestamp < endTime) revert NoWithdrawDuringClaim();
/// @notice Checks if the quest end time is passed
modifier onlyEnded() {
if (block.timestamp < endTime) revert NotEnded();
_;
}

/// @notice Checks if quest has started both at the function level and at the start time
modifier onlyQuestActive() {
if (block.timestamp < startTime) revert ClaimWindowNotStarted();
modifier onlyQuestFactory() {
if (msg.sender != address(questFactoryContract)) revert NotQuestFactory();
_;
}

modifier onlyProtocolFeeRecipientOrOwner() {
if (msg.sender != protocolFeeRecipient && msg.sender != owner()) revert AuthOwnerRecipient();
/// @notice Checks if the quest start time is passed
modifier onlyStarted() {
if (block.timestamp < startTime) revert NotStarted();
_;
}

modifier onlyQuestFactory() {
if (msg.sender != address(questFactoryContract)) revert NotQuestFactory();
/// @notice Checks if the quest end time has not passed
modifier whenNotEnded() {
if (block.timestamp > endTime) revert QuestEnded();
_;
}

Expand All @@ -141,7 +142,8 @@ contract Quest is ReentrancyGuardUpgradeable, PausableUpgradeable, Ownable, IQue
external
virtual
nonReentrant
onlyQuestActive
onlyStarted
whenNotEnded
whenNotPaused
onlyQuestFactory
{
Expand All @@ -152,7 +154,7 @@ contract Quest is ReentrancyGuardUpgradeable, PausableUpgradeable, Ownable, IQue

/// @notice Function to withdraw the remaining tokens in the contract, distributes the protocol fee and returns remaining tokens to owner
/// @dev Can only be called after the quest has ended
function withdrawRemainingTokens() external onlyWithdrawAfterEnd {
function withdrawRemainingTokens() external onlyEnded {
if (hasWithdrawn) revert AlreadyWithdrawn();
hasWithdrawn = true;

Expand Down
1 change: 0 additions & 1 deletion contracts/Quest1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ contract Quest1155 is ERC1155Holder, ReentrancyGuardUpgradeable, PausableUpgrade
function singleClaim(address account_)
external
virtual
nonReentrant
whenNotPaused
whenNotEnded
onlyStarted
Expand Down
6 changes: 0 additions & 6 deletions contracts/QuestFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -557,9 +557,6 @@ contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFacto
{
Quest storage currentQuest = quests[claimData_.questId];
IQuest1155Ownable questContract_ = IQuest1155Ownable(currentQuest.questAddress);
if (!questContract_.queued()) revert QuestNotQueued();
if (block.timestamp < questContract_.startTime()) revert QuestNotStarted();
if (block.timestamp > questContract_.endTime()) revert QuestEnded();

currentQuest.addressMinted[claimData_.claimer] = true;
++currentQuest.numberMinted;
Expand Down Expand Up @@ -605,9 +602,6 @@ contract QuestFactory is Initializable, LegacyStorage, OwnableRoles, IQuestFacto
{
Quest storage currentQuest = quests[claimData_.questId];
IQuestOwnable questContract_ = IQuestOwnable(currentQuest.questAddress);
if (!questContract_.queued()) revert QuestNotQueued();
if (block.timestamp < questContract_.startTime()) revert QuestNotStarted();
if (block.timestamp > questContract_.endTime()) revert QuestEnded();

currentQuest.addressMinted[claimData_.claimer] = true;
++currentQuest.numberMinted;
Expand Down
4 changes: 2 additions & 2 deletions contracts/interfaces/IQuest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ interface IQuest {
error AlreadyClaimed();
error AlreadyWithdrawn();
error AmountExceedsBalance();
error ClaimWindowNotStarted();
error EndTimeInPast();
error EndTimeLessThanOrEqualToStartTime();
error InvalidRefundToken();
error MustImplementInChild();
error NotQuestFactory();
error NoWithdrawDuringClaim();
error NotStarted();
error TotalAmountExceedsBalance();
error AuthOwnerRecipient();
error QuestEnded();
error NotEnded();

function initialize(
address rewardTokenAddress_,
Expand Down
2 changes: 1 addition & 1 deletion test/QuestFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ contract TestQuestFactory is Test, Errors, Events, TestUtils {
bytes32 msgHash = keccak256(abi.encodePacked(participant, "questId"));
bytes memory signature = signHash(msgHash, claimSignerPrivateKey);

vm.expectRevert(abi.encodeWithSelector(QuestNotStarted.selector));
vm.expectRevert(abi.encodeWithSelector(NotStarted.selector));
vm.startPrank(participant);
questFactory.claim{value: MINT_FEE}("questId", msgHash, signature, address(0));
}
Expand Down

0 comments on commit f2a7072

Please sign in to comment.