Skip to content

Commit

Permalink
Add test for BORROW_01
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggiano committed Jan 11, 2024
1 parent eb56726 commit 5b719d3
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/libraries/YieldCurveLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ library YieldCurveLibrary {
}
}
}
}
}
13 changes: 10 additions & 3 deletions test/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,16 @@ contract BaseTest is Test, Deploy, AssertsHelper {
function _borrowAsMarketOrder(address borrower, address lender, uint256 amount, uint256 dueDate)
internal
returns (uint256)
{
return _borrowAsMarketOrder(borrower, lender, amount, dueDate, false);
}

function _borrowAsMarketOrder(address borrower, address lender, uint256 amount, uint256 dueDate, bool exactAmountIn)
internal
returns (uint256)
{
uint256[] memory virtualCollateralLoanIds;
return _borrowAsMarketOrder(borrower, lender, amount, dueDate, false, virtualCollateralLoanIds);
return _borrowAsMarketOrder(borrower, lender, amount, dueDate, exactAmountIn, virtualCollateralLoanIds);
}

function _borrowAsMarketOrder(
Expand Down Expand Up @@ -208,7 +215,7 @@ contract BaseTest is Test, Deploy, AssertsHelper {
virtualCollateralLoanIds: virtualCollateralLoanIds
})
);
return size.activeLoans() - 1;
return size.activeLoans() > 0 ? size.activeLoans() - 1 : type(uint256).max;
}

function _borrowAsLimitOrder(
Expand Down Expand Up @@ -245,7 +252,7 @@ contract BaseTest is Test, Deploy, AssertsHelper {
size.lendAsMarketOrder(
LendAsMarketOrderParams({borrower: borrower, amount: amount, dueDate: dueDate, exactAmountIn: exactAmountIn})
);
return size.activeLoans() - 1;
return size.activeLoans() > 0 ? size.activeLoans() - 1 : type(uint256).max;
}

function _borrowerExit(address user, uint256 loanId, address borrowerToExitTo) internal {
Expand Down
22 changes: 22 additions & 0 deletions test/BorrowAsMarketOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,26 @@ contract BorrowAsMarketOrderTest is BaseTest {
vm.expectRevert(abi.encodeWithSelector(Errors.INSUFFICIENT_COLLATERAL.selector, 0, 150e18));
size.borrowAsMarketOrder(params);
}

function test_BorrowAsMarketOrder_borrowAsMarketOrder_does_not_create_loans_if_dust_amount() public {
_deposit(alice, 100e18, 100e18);
_deposit(bob, 100e18, 100e18);
_lendAsLimitOrder(alice, 100e18, 12, 0.1e18, 12);
LoanOffer memory offerBefore = size.getUserView(alice).user.loanOffer;

Vars memory _before = _state();

uint256 amount = 1;
uint256 dueDate = 12;

_borrowAsMarketOrder(bob, alice, amount, dueDate, true);

Vars memory _after = _state();

assertEq(_after.alice, _before.alice);
assertEq(_after.bob, _before.bob);
assertEq(_after.bob.debtAmount, 0);
assertEq(_after.protocolCollateralAmount, _before.protocolCollateralAmount);
assertEq(size.activeLoans(), 0);
}
}
4 changes: 3 additions & 1 deletion test/BorrowAsMarketOrderValidation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ contract BorrowAsMarketOrderValidationTest is BaseTest {
);

vm.expectRevert(
abi.encodeWithSelector(Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT.selector, 1.03e18, size.config().minimumCredit)
abi.encodeWithSelector(
Errors.CREDIT_LOWER_THAN_MINIMUM_CREDIT.selector, 1.03e18, size.config().minimumCredit
)
);
size.borrowAsMarketOrder(
BorrowAsMarketOrderParams({
Expand Down
12 changes: 8 additions & 4 deletions test/LiquidateLoan.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,24 @@ contract LiquidateLoanTest is BaseTest {
_before.feeRecipientCollateralAmount
+ Math.mulDivDown(collateralRemainder, size.config().collateralPercentagePremiumToProtocol, PERCENT)
);
uint256 collateralPercentagePremiumToBorrower = PERCENT - size.config().collateralPercentagePremiumToProtocol - size.config().collateralPercentagePremiumToLiquidator;
uint256 collateralPercentagePremiumToBorrower = PERCENT - size.config().collateralPercentagePremiumToProtocol
- size.config().collateralPercentagePremiumToLiquidator;
assertEq(
_after.bob.collateralAmount,
_before.bob.collateralAmount - (debt * 5)
- Math.mulDivDown(
collateralRemainder,
(size.config().collateralPercentagePremiumToProtocol + size.config().collateralPercentagePremiumToLiquidator),
(
size.config().collateralPercentagePremiumToProtocol
+ size.config().collateralPercentagePremiumToLiquidator
),
PERCENT
),
_before.bob.collateralAmount - (debt * 5) - collateralRemainder
+ Math.mulDivDown(collateralRemainder, collateralPercentagePremiumToBorrower, PERCENT)
);
uint256 liquidatorProfitAmount =
(debt * 5) + Math.mulDivDown(collateralRemainder, size.config().collateralPercentagePremiumToLiquidator, PERCENT);
uint256 liquidatorProfitAmount = (debt * 5)
+ Math.mulDivDown(collateralRemainder, size.config().collateralPercentagePremiumToLiquidator, PERCENT);
assertEq(_after.liquidator.collateralAmount, _before.liquidator.collateralAmount + liquidatorProfitAmount);
assertEq(liquidatorProfit, liquidatorProfitAmount);
}
Expand Down
4 changes: 3 additions & 1 deletion test/MoveToVariablePoolValidation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ contract MoveToVariablePoolValidationTest is BaseTest {
vm.warp(block.timestamp + 12);

vm.expectRevert(
abi.encodeWithSelector(Errors.INSUFFICIENT_COLLATERAL.selector, 130e18, 100e18 * size.config().crOpening / 1e18)
abi.encodeWithSelector(
Errors.INSUFFICIENT_COLLATERAL.selector, 130e18, 100e18 * size.config().crOpening / 1e18
)
);
size.moveToVariablePool(MoveToVariablePoolParams({loanId: loanId}));
}
Expand Down
36 changes: 36 additions & 0 deletions test/invariants/CryticToFoundry.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.20;

import {TargetFunctions} from "./TargetFunctions.sol";
import {FoundryAsserts} from "@chimera/FoundryAsserts.sol";
import {Test} from "forge-std/Test.sol";

contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts {
function setUp() public {
vm.deal(address(USER1), 100e18);
vm.deal(address(USER2), 100e18);
vm.deal(address(USER3), 100e18);

setup();
}

modifier getSender() override {
sender = uint160(msg.sender) % 3 == 0
? address(USER1)
: uint160(msg.sender) % 3 == 1 ? address(USER2) : address(USER3);
_;
}

function test_BORROW_02() public {
deposit(address(0xdeadbeef), 0);
lendAsLimitOrder(0, 10667226, 451124);
borrowAsMarketOrder(
address(0x1e),
1,
299999999999999999,
true,
14221329489769958708126347564797299640365746048626527781107915342306360762091,
47700905178432190716842576859681767948209730775316858409394951552214610302274
);
}
}
30 changes: 15 additions & 15 deletions test/invariants/TargetFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,10 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
function borrowAsMarketOrder(
address lender,
uint256 amount,
uint256 dueDate
// ,
// bool exactAmountIn,
// uint256 n,
// uint256 seedVirtualCollateralLoanIds
uint256 dueDate,
bool exactAmountIn,
uint256 n,
uint256 seedVirtualCollateralLoanIds
) public getSender {
__before();

Expand All @@ -104,19 +103,18 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
dueDate = between(dueDate, block.timestamp, block.timestamp + MAX_DURATION);

uint256[] memory virtualCollateralLoanIds;
// if (_before.activeLoans > 0) {
// n = between(n, 1, _before.activeLoans);
// virtualCollateralLoanIds = _getRandomVirtualCollateralLoanIds(n, seedVirtualCollateralLoanIds);
// }
if (_before.activeLoans > 0) {
n = between(n, 1, _before.activeLoans);
virtualCollateralLoanIds = _getRandomVirtualCollateralLoanIds(n, seedVirtualCollateralLoanIds);
}

hevm.prank(sender);
size.borrowAsMarketOrder(
BorrowAsMarketOrderParams({
lender: lender,
amount: amount,
dueDate: dueDate,
// exactAmountIn: exactAmountIn,
exactAmountIn: false,
exactAmountIn: exactAmountIn,
virtualCollateralLoanIds: virtualCollateralLoanIds
})
);
Expand All @@ -129,10 +127,12 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct
gt(_after.sender.borrowAmount, _before.sender.borrowAmount, BORROW_01);
}

if (virtualCollateralLoanIds.length > 0) {
gte(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
} else {
eq(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
if (amount > size.config().minimumCredit) {
if (virtualCollateralLoanIds.length > 0) {
gte(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
} else {
eq(_after.activeLoans, _before.activeLoans + 1, BORROW_02);
}
}
}

Expand Down

0 comments on commit 5b719d3

Please sign in to comment.