Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

c4-tests #121

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions test/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ contract BaseTest is Test, Deploy, AssertsHelper {
address internal feeRecipient = address(0x70000);

function setUp() public virtual {
_labels();
setupLocal(address(this), feeRecipient);
_labels();
}

function _labels() internal {
Expand All @@ -73,13 +73,17 @@ contract BaseTest is Test, Deploy, AssertsHelper {
vm.label(liquidator, "liquidator");
vm.label(feeRecipient, "feeRecipient");

vm.label(address(proxy), "size-proxy");
vm.label(address(implementation), "size-implementation");
vm.label(address(size), "size");
vm.label(address(priceFeed), "priceFeed");
vm.label(address(usdc), "usdc");
vm.label(address(weth), "weth");
vm.label(address(variablePool), "variablePool");
vm.label(address(proxy), "Size-proxy");
vm.label(address(implementation), "Size-implementation");
vm.label(address(size), "Size");
vm.label(address(priceFeed), "PriceFeed");
vm.label(address(usdc), "USDC");
vm.label(address(weth), "WETH");
vm.label(address(variablePool), "VariablePool");

vm.label(address(size.data().collateralToken), "szWETH");
vm.label(address(size.data().borrowAToken), "szaUSDC");
vm.label(address(size.data().debtToken), "szDebtUSDC");

vm.label(address(0), "address(0)");
}
Expand Down Expand Up @@ -230,23 +234,33 @@ contract BaseTest is Test, Deploy, AssertsHelper {
return _sellCreditMarket(borrower, lender, RESERVED_ID, amount, tenor, exactAmountIn);
}

function _sellCreditLimit(address borrower, YieldCurve memory curveRelativeTime) internal {
function _sellCreditLimit(address borrower, uint256 maxDueDate, YieldCurve memory curveRelativeTime) internal {
vm.prank(borrower);
size.sellCreditLimit(SellCreditLimitParams({curveRelativeTime: curveRelativeTime}));
size.sellCreditLimit(SellCreditLimitParams({maxDueDate: maxDueDate, curveRelativeTime: curveRelativeTime}));
}

function _sellCreditLimit(address borrower, int256[1] memory ratesArray, uint256[1] memory tenorsArray) internal {
function _sellCreditLimit(
address borrower,
uint256 maxDueDate,
int256[1] memory ratesArray,
uint256[1] memory tenorsArray
) internal {
int256[] memory aprs = new int256[](1);
uint256[] memory tenors = new uint256[](1);
uint256[] memory marketRateMultipliers = new uint256[](1);
aprs[0] = ratesArray[0];
tenors[0] = tenorsArray[0];
YieldCurve memory curveRelativeTime =
YieldCurve({tenors: tenors, marketRateMultipliers: marketRateMultipliers, aprs: aprs});
return _sellCreditLimit(borrower, curveRelativeTime);
return _sellCreditLimit(borrower, maxDueDate, curveRelativeTime);
}

function _sellCreditLimit(address borrower, int256[2] memory ratesArray, uint256[2] memory tenorsArray) internal {
function _sellCreditLimit(
address borrower,
uint256 maxDueDate,
int256[2] memory ratesArray,
uint256[2] memory tenorsArray
) internal {
int256[] memory aprs = new int256[](2);
uint256[] memory tenors = new uint256[](2);
uint256[] memory marketRateMultipliers = new uint256[](2);
Expand All @@ -256,12 +270,12 @@ contract BaseTest is Test, Deploy, AssertsHelper {
tenors[1] = tenorsArray[1];
YieldCurve memory curveRelativeTime =
YieldCurve({tenors: tenors, marketRateMultipliers: marketRateMultipliers, aprs: aprs});
return _sellCreditLimit(borrower, curveRelativeTime);
return _sellCreditLimit(borrower, maxDueDate, curveRelativeTime);
}

function _sellCreditLimit(address borrower, int256 rate, uint256 tenor) internal {
function _sellCreditLimit(address borrower, uint256 maxDueDate, int256 rate, uint256 tenor) internal {
YieldCurve memory curveRelativeTime = YieldCurveHelper.pointCurve(tenor, rate);
return _sellCreditLimit(borrower, curveRelativeTime);
return _sellCreditLimit(borrower, maxDueDate, curveRelativeTime);
}

function _buyCreditMarket(address lender, uint256 creditPositionId, uint256 amount, bool exactAmountIn)
Expand Down
6 changes: 3 additions & 3 deletions test/Logger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import {
DebtPosition,
LoanLibrary
} from "@src/libraries/LoanLibrary.sol";
import {BorrowOffer, LoanOffer, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {LimitOrder, OfferLibrary} from "@src/libraries/OfferLibrary.sol";

import {console2 as console} from "forge-std/console2.sol";

abstract contract Logger {
using LoanLibrary for DebtPosition;
using OfferLibrary for LoanOffer;
using OfferLibrary for BorrowOffer;
using OfferLibrary for LimitOrder;
using OfferLibrary for LimitOrder;

function _log(UserView memory userView) internal pure {
console.log("account", userView.account);
Expand Down
1 change: 1 addition & 0 deletions test/invariants/Bounds.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ abstract contract Bounds {
uint256 internal MAX_PRICE = 20_000e18;
uint256 internal MAX_LIQUIDITY_INDEX_INCREASE_PERCENT = 1.1e18;
uint256 internal MAX_PERCENT = 10 * 1e18;
uint256 internal MAX_PERCENT_SWAP_FEE_APR = 1 * 1e18;
uint256 internal PERCENTAGE_OLD_CREDIT = 0.6e18;
}
33 changes: 27 additions & 6 deletions test/invariants/CryticToFoundry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {

function test_CryticToFoundry_09() public {
deposit(address(0xdeadbeef), 6650265435768735282694896341184603990418320173531048);
sellCreditLimit(0);
sellCreditLimit(block.timestamp + 365 days, 0);
deposit(address(0x0), 14110947487286576);
buyCreditMarket(
address(0x0),
Expand All @@ -135,7 +135,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {
deposit(address(0xdeadbeef), 486987982);
deposit(address(0x0), 542119798154271826);
updateConfig(3476689163798957933627285661408434842686568751617897946215078674744880122, 1);
sellCreditLimit(0);
sellCreditLimit(block.timestamp + 365 days, 0);
setLiquidityIndex(1, 0);
buyCreditMarket(
address(0x0),
Expand All @@ -148,7 +148,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {

function test_CryticToFoundry_12() public {
deposit(address(0xdeadbeef), 481797977);
sellCreditLimit(0);
sellCreditLimit(block.timestamp + 365 days, 0);
deposit(address(0x0), 542722934120754506);
buyCreditMarket(
address(0x0),
Expand All @@ -170,7 +170,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {
deposit(address(0xdeadbeef), 486987982);
deposit(address(0x0), 542119798154271826);
updateConfig(3476689163798957933627285661408434842686568751617897946215078674744880122, 1);
sellCreditLimit(0);
sellCreditLimit(block.timestamp + 365 days, 0);
setLiquidityIndex(1, 0);
buyCreditMarket(
address(0x0),
Expand All @@ -187,7 +187,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {
_setUp(0x0000000000000000000000000000000000030000, 599944 seconds, 44);
setPrice(53041679552025056960350439062206622489044312390915125528741382084316446848209);
_setUp(0x0000000000000000000000000000000000010000, 604796 seconds, 12382);
sellCreditLimit(120000001);
sellCreditLimit(block.timestamp + 365 days, 120000001);
_setUp(0x0000000000000000000000000000000000030000, 553872 seconds, 59522);
deposit(address(0x636f6e736f6c652e6c6f67), 1300000000000000000);
_setUp(0x0000000000000000000000000000000000010000, 122098 seconds, 8537);
Expand Down Expand Up @@ -237,7 +237,7 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {

function test_CryticToFoundry_18() public {
deposit(address(0xdeadbeef), 206844940486);
sellCreditLimit(0);
sellCreditLimit(block.timestamp + 365 days, 0);
deposit(address(0x0), 2096225315717814455);
buyCreditMarket(
address(0x0),
Expand Down Expand Up @@ -305,4 +305,25 @@ contract CryticToFoundry is Test, TargetFunctions, FoundryAsserts, Logger {
setPrice(0);
selfLiquidate(0);
}

function test_CryticToFoundry_23() public {
deposit(address(0xdeadbeef), 153061138);
deposit(address(0x0), 32206716064976759515506730950);
sellCreditLimit(block.timestamp + 365 days, 1158088527381210533895115363106426155);
buyCreditMarket(
address(0x0),
26189833411396725481776162706072748294814554872160036126875434055275,
10855877590960237497587309726937818818523821252845129895149409077587346889659,
346849952445745110224397552578749626750938035523378926500285586611475279057,
false
);
buyCreditLimit(1150971281321076252120372341636290708838077407, 732919526025001040686075401367603283679132813308);
updateConfig(44403998962614087378936458640045159306808189755934133735775514252696997342, 1);
updateConfig(5, 53272389541550468998524231495554213931535354675872544579553587611547756068029);
updateConfig(
32443037445401376527959883768083708906893044152733957436551516245553282815386,
2211186247407213273680871110148807189326067499600070275884279229677703437
);
sellCreditMarket(address(0x0), 0, 1, 35988792354460757875445074184694814404688696852, false);
}
}
1 change: 0 additions & 1 deletion test/invariants/ExpectedErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ abstract contract ExpectedErrors is Deploy, Properties {

// SELF_LIQUIDATE_ERRORS
SELF_LIQUIDATE_ERRORS.push(Errors.LOAN_NOT_SELF_LIQUIDATABLE.selector);
SELF_LIQUIDATE_ERRORS.push(Errors.LIQUIDATION_NOT_AT_LOSS.selector);
SELF_LIQUIDATE_ERRORS.push(Errors.LIQUIDATOR_IS_NOT_LENDER.selector);

// LIQUIDATE_WITH_REPLACEMENT_ERRORS
Expand Down
4 changes: 3 additions & 1 deletion test/invariants/Ghosts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity 0.8.23;

import {Asserts} from "@chimera/Asserts.sol";

import {IAToken} from "@aave/interfaces/IAToken.sol";
import {PropertiesConstants} from "@crytic/properties/contracts/util/PropertiesConstants.sol";
import {UserView} from "@src/SizeView.sol";
import {RESERVED_ID} from "@src/libraries/LoanLibrary.sol";
Expand Down Expand Up @@ -57,6 +58,7 @@ abstract contract Ghosts is Deploy, Asserts, PropertiesConstants {
}

function __snapshot(Vars storage vars, uint256 positionId) internal {
IAToken aToken = IAToken(variablePool.getReserveData(address(usdc)).aTokenAddress);
vars.sig = msg.sig;
vars.debtPositionId = RESERVED_ID;
vars.creditPositionId = RESERVED_ID;
Expand Down Expand Up @@ -91,7 +93,7 @@ abstract contract Ghosts is Deploy, Asserts, PropertiesConstants {
vars.senderCollateralAmount = weth.balanceOf(sender);
vars.senderBorrowAmount = usdc.balanceOf(sender);
vars.sizeCollateralAmount = weth.balanceOf(address(size));
vars.sizeBorrowAmount = usdc.balanceOf(address(variablePool));
vars.sizeBorrowAmount = usdc.balanceOf(address(aToken));
(vars.debtPositionsCount, vars.creditPositionsCount) = size.getPositionsCount();
vars.variablePoolBorrowAmount = size.getUserView(address(variablePool)).borrowATokenBalance;
vars.totalDebtAmount = size.data().debtToken.totalSupply();
Expand Down
3 changes: 1 addition & 2 deletions test/invariants/PropertiesSpecifications.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ abstract contract PropertiesSpecifications {
string internal constant CLAIM_02 = "CLAIM_02: Claim is only valid for CreditPositions";

string internal constant LIQUIDATE_01 = "LIQUIDATE_01: Liquidate increases the sender collateral";
string internal constant LIQUIDATE_02 =
"LIQUIDATE_02: Liquidate decreases the sender's cash if the loan is not overdue";
string internal constant LIQUIDATE_02 = "LIQUIDATE_02: Liquidate decreases the sender's cash";
string internal constant LIQUIDATE_03 = "LIQUIDATE_03: Liquidate only succeeds if the borrower is liquidatable";
string internal constant LIQUIDATE_04 = "LIQUIDATE_04: Liquidate decreases the borrower's debt";
string internal constant LIQUIDATE_05 = "LIQUIDATE_05: Liquidate clears the loan's debt";
Expand Down
14 changes: 11 additions & 3 deletions test/invariants/TargetFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,22 @@ abstract contract TargetFunctions is Helper, ExpectedErrors, BaseTargetFunctions
}
}

function sellCreditLimit(uint256 yieldCurveSeed) public getSender checkExpectedErrors(SELL_CREDIT_LIMIT_ERRORS) {
function sellCreditLimit(uint256 maxDueDate, uint256 yieldCurveSeed)
public
getSender
checkExpectedErrors(SELL_CREDIT_LIMIT_ERRORS)
{
__before();

maxDueDate = between(maxDueDate, block.timestamp, block.timestamp + MAX_DURATION);
YieldCurve memory curveRelativeTime = _getRandomYieldCurve(yieldCurveSeed);

hevm.prank(sender);
(success, returnData) = address(size).call(
abi.encodeCall(size.sellCreditLimit, SellCreditLimitParams({curveRelativeTime: curveRelativeTime}))
abi.encodeCall(
size.sellCreditLimit,
SellCreditLimitParams({maxDueDate: maxDueDate, curveRelativeTime: curveRelativeTime})
)
);
if (success) {
__after();
Expand Down Expand Up @@ -496,7 +504,7 @@ abstract contract TargetFunctions is Helper, ExpectedErrors, BaseTargetFunctions
MAX_AMOUNT_USDC,
MAX_DURATION,
MAX_DURATION,
MAX_PERCENT,
MAX_PERCENT_SWAP_FEE_APR,
MAX_AMOUNT_USDC,
MAX_PERCENT,
MAX_PERCENT,
Expand Down
2 changes: 1 addition & 1 deletion test/invariants/interfaces/ITargetFunctions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ITargetFunctions {
bool exactAmountIn
) external;

function sellCreditLimit(uint256 yieldCurveSeed) external;
function sellCreditLimit(uint256 maxDueDate, uint256 yieldCurveSeed) external;

function buyCreditMarket(
address borrower,
Expand Down
1 change: 1 addition & 0 deletions test/libraries/Math.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ contract MathTest is Test, AssertsHelper {
assertLe(high, array.length - 1, "high is within bounds");

assertLe(low, high, "low <= high");
assertLe(low, high + 1, "low <= high + 1");

assertLe(array[low], value, "array[low] <= value <= array[high]");
assertLe(value, array[high], "array[low] <= value <= array[high]");
Expand Down
6 changes: 3 additions & 3 deletions test/libraries/OfferLibrary.t.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import {BorrowOffer, LoanOffer, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {LimitOrder, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {Test} from "forge-std/Test.sol";

contract OfferLibraryTest is Test {
function test_OfferLibrary_isNull() public {
LoanOffer memory l;
LimitOrder memory l;
assertEq(OfferLibrary.isNull(l), true);

BorrowOffer memory b;
LimitOrder memory b;
assertEq(OfferLibrary.isNull(b), true);
}
}
6 changes: 3 additions & 3 deletions test/local/actions/BuyCreditLimit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {BaseTest} from "@test/BaseTest.sol";

import {Errors} from "@src/libraries/Errors.sol";
import {RESERVED_ID} from "@src/libraries/LoanLibrary.sol";
import {LoanOffer, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {LimitOrder, OfferLibrary} from "@src/libraries/OfferLibrary.sol";

import {YieldCurve} from "@src/libraries/YieldCurveLibrary.sol";
import {YieldCurveHelper} from "@test/helpers/libraries/YieldCurveHelper.sol";
Expand All @@ -15,7 +15,7 @@ import {BuyCreditLimitParams} from "@src/libraries/actions/BuyCreditLimit.sol";
import {SellCreditMarketParams} from "@src/libraries/actions/SellCreditMarket.sol";

contract BuyCreditLimitTest is BaseTest {
using OfferLibrary for LoanOffer;
using OfferLibrary for LimitOrder;

function test_BuyCreditLimit_buyCreditLimit_adds_loanOffer_to_orderbook() public {
_deposit(alice, weth, 100e18);
Expand All @@ -38,7 +38,7 @@ contract BuyCreditLimitTest is BaseTest {
tenors[1] = 60 days;
int256[] memory aprs = new int256[](2);
aprs[0] = 0.15e18;
aprs[0] = 0.12e18;
aprs[1] = 0.12e18;

vm.prank(alice);
size.buyCreditLimit(
Expand Down
4 changes: 2 additions & 2 deletions test/local/actions/BuyCreditLimitValidation.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ pragma solidity 0.8.23;

import {BaseTest} from "@test/BaseTest.sol";

import {LoanOffer, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {LimitOrder, OfferLibrary} from "@src/libraries/OfferLibrary.sol";
import {YieldCurve} from "@src/libraries/YieldCurveLibrary.sol";
import {BuyCreditLimitParams} from "@src/libraries/actions/BuyCreditLimit.sol";

import {Errors} from "@src/libraries/Errors.sol";

contract BuyCreditLimitValidationTest is BaseTest {
using OfferLibrary for LoanOffer;
using OfferLibrary for LimitOrder;

function test_BuyCreditLimit_validation() public {
_deposit(alice, usdc, 100e6);
Expand Down
Loading
Loading