Skip to content

Commit

Permalink
add DailyLimit testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
davidinsuomi committed Oct 31, 2024
1 parent 5480bac commit b5531ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
7 changes: 1 addition & 6 deletions contracts/hooks/spendLimit/DailyERC20SpendingLimitHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,6 @@ contract DailyERC20SpendingLimitHook is IHook {
tokenLimit.lastResetTime = block.timestamp;
}

// Reset daily spent if 24 hours have passed
if (block.timestamp >= tokenLimit.lastResetTime + ONE_DAY) {
tokenLimit.dailySpent = 0;
tokenLimit.lastResetTime = block.timestamp;
}

// Check if the new spending would exceed the daily limit
require(tokenLimit.dailySpent + spent <= tokenLimit.dailyLimit, "Daily spending limit exceeded");

Expand All @@ -180,6 +174,7 @@ contract DailyERC20SpendingLimitHook is IHook {
}

function initiateSetLimit(address token, uint256 newLimit) external {
require(newLimit > 0, "newLimit must be greater than 0");
SpendingLimit storage limit = walletSpendingLimits[msg.sender];
require(limit.initialized, "not initialized");

Expand Down
12 changes: 6 additions & 6 deletions test/hooks/dailyLimit/DailyLimitHookTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -252,10 +252,10 @@ contract DailyLimitHookTest is Test, UserOpHelper {

// First day transactions
_executeERC20Transaction(0.5 ether, false);
vm.warp(block.timestamp + 12 hours);
_executeERC20Transaction(0.4 ether, false);

// Move to next day
vm.warp(block.timestamp + 24 hours);
vm.warp(block.timestamp + 13 hours);

// Should work as limit is reset
_executeERC20Transaction(0.5 ether, false);
Expand Down Expand Up @@ -371,9 +371,9 @@ contract DailyLimitHookTest is Test, UserOpHelper {
}

function returnDummyHookAndData() private view returns (bytes memory) {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(0x1, bytes32(0));
bytes memory hookSignatureData = abi.encodePacked(r, s, v);
bytes4 hookSignatureLength = bytes4(uint32(hookSignatureData.length));
return abi.encodePacked(address(dailyLimitHook), hookSignatureLength, hookSignatureData);
// just using dummy data for placeholder
bytes memory hookData = hex"aa";
bytes4 hookSignatureLength = bytes4(uint32(hookData.length));
return abi.encodePacked(address(dailyLimitHook), hookSignatureLength, hookData);
}
}

0 comments on commit b5531ae

Please sign in to comment.