Skip to content

Commit

Permalink
fix: enforce non-zero extension (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrusowsky authored Sep 15, 2023
1 parent 3f4380a commit e4c8f35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Cooler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ contract Cooler is Clone {
error OnlyApproved();
error Deactivated();
error Default();
error NoDefault();
error NotExpired();
error NotExtension();
error NotCoolerCallback();

// --- DATA STRUCTURES -------------------------------------------
Expand Down Expand Up @@ -266,6 +267,7 @@ contract Cooler is Clone {

if (msg.sender != loan.lender) revert OnlyApproved();
if (block.timestamp > loan.expiry) revert Default();
if (times_ == 0) revert NotExtension();

// Update loan terms to reflect the extension.
loan.expiry += loan.request.duration * times_;
Expand All @@ -288,7 +290,7 @@ contract Cooler is Clone {
function claimDefaulted(uint256 loanID_) external returns (uint256, uint256, uint256, uint256) {
Loan memory loan = loans[loanID_];

if (block.timestamp <= loan.expiry) revert NoDefault();
if (block.timestamp <= loan.expiry) revert NotExpired();

loans[loanID_].principal = 0;
loans[loanID_].interestDue = 0;
Expand Down
19 changes: 18 additions & 1 deletion src/test/Cooler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ contract CoolerTest is Test {

vm.prank(lender);
// can't default a non-expired loan
vm.expectRevert(Cooler.NoDefault.selector);
vm.expectRevert(Cooler.NotExpired.selector);
cooler.claimDefaulted(loanID);
}

Expand Down Expand Up @@ -861,6 +861,7 @@ contract CoolerTest is Test {

function testFuzz_extendLoanTerms_severalTimes(uint256 amount_, uint8 times_) public {
// test inputs
times_ = uint8(bound(times_, 1, type(uint8).max));
amount_ = bound(amount_, 0, MAX_DEBT / 2);
bool directRepay = true;
bool callbackRepay = false;
Expand Down Expand Up @@ -917,6 +918,22 @@ contract CoolerTest is Test {
cooler.extendLoanTerms(loanID, 1);
}

function testRevertFuzz_extendLoanTerms_zeroTimes(uint256 amount_) public {
// test inputs
amount_ = bound(amount_, 0, MAX_DEBT);
bool directRepay = true;
bool callbackRepay = false;
// test setup
cooler = _initCooler();
(uint256 reqID, ) = _requestLoan(amount_);
uint256 loanID = _clearLoan(reqID, amount_, directRepay, callbackRepay);

vm.prank(lender);
// only extendable by the lender
vm.expectRevert(Cooler.NotExtension.selector);
cooler.extendLoanTerms(loanID, 0);
}

function testRevertFuzz_extendLoanTerms_defaulted(uint256 amount_) public {
// test inputs
amount_ = bound(amount_, 0, MAX_DEBT);
Expand Down

0 comments on commit e4c8f35

Please sign in to comment.