From 5b719d3012930cf072b5963303769fde432243dc Mon Sep 17 00:00:00 2001 From: Antonio Guilherme Ferreira Viggiano Date: Thu, 11 Jan 2024 15:42:09 -0300 Subject: [PATCH] Add test for BORROW_01 --- src/libraries/YieldCurveLibrary.sol | 2 +- test/BaseTest.sol | 13 +++++++-- test/BorrowAsMarketOrder.t.sol | 22 +++++++++++++++ test/BorrowAsMarketOrderValidation.t.sol | 4 ++- test/LiquidateLoan.t.sol | 12 +++++--- test/MoveToVariablePoolValidation.t.sol | 4 ++- test/invariants/CryticToFoundry.t.sol | 36 ++++++++++++++++++++++++ test/invariants/TargetFunctions.sol | 30 ++++++++++---------- 8 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 test/invariants/CryticToFoundry.t.sol diff --git a/src/libraries/YieldCurveLibrary.sol b/src/libraries/YieldCurveLibrary.sol index 23114521..0e823907 100644 --- a/src/libraries/YieldCurveLibrary.sol +++ b/src/libraries/YieldCurveLibrary.sol @@ -53,4 +53,4 @@ library YieldCurveLibrary { } } } -} \ No newline at end of file +} diff --git a/test/BaseTest.sol b/test/BaseTest.sol index 0c050da7..79aa3ef0 100644 --- a/test/BaseTest.sol +++ b/test/BaseTest.sol @@ -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( @@ -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( @@ -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 { diff --git a/test/BorrowAsMarketOrder.t.sol b/test/BorrowAsMarketOrder.t.sol index 43555bc4..d8bb0196 100644 --- a/test/BorrowAsMarketOrder.t.sol +++ b/test/BorrowAsMarketOrder.t.sol @@ -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); + } } diff --git a/test/BorrowAsMarketOrderValidation.t.sol b/test/BorrowAsMarketOrderValidation.t.sol index ffbbe233..a7224c1b 100644 --- a/test/BorrowAsMarketOrderValidation.t.sol +++ b/test/BorrowAsMarketOrderValidation.t.sol @@ -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({ diff --git a/test/LiquidateLoan.t.sol b/test/LiquidateLoan.t.sol index d83eeb98..cc049eb3 100644 --- a/test/LiquidateLoan.t.sol +++ b/test/LiquidateLoan.t.sol @@ -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); } diff --git a/test/MoveToVariablePoolValidation.t.sol b/test/MoveToVariablePoolValidation.t.sol index 8a6dd383..b59a9ad2 100644 --- a/test/MoveToVariablePoolValidation.t.sol +++ b/test/MoveToVariablePoolValidation.t.sol @@ -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})); } diff --git a/test/invariants/CryticToFoundry.t.sol b/test/invariants/CryticToFoundry.t.sol new file mode 100644 index 00000000..5692c495 --- /dev/null +++ b/test/invariants/CryticToFoundry.t.sol @@ -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 + ); + } +} diff --git a/test/invariants/TargetFunctions.sol b/test/invariants/TargetFunctions.sol index 02b31989..300601ce 100644 --- a/test/invariants/TargetFunctions.sol +++ b/test/invariants/TargetFunctions.sol @@ -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(); @@ -104,10 +103,10 @@ 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( @@ -115,8 +114,7 @@ abstract contract TargetFunctions is Deploy, Helper, Properties, BaseTargetFunct lender: lender, amount: amount, dueDate: dueDate, - // exactAmountIn: exactAmountIn, - exactAmountIn: false, + exactAmountIn: exactAmountIn, virtualCollateralLoanIds: virtualCollateralLoanIds }) ); @@ -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); + } } }