Skip to content

Commit

Permalink
Merge branch 'code4rena-audit-fixes' into code4rena-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggiano committed Jul 5, 2024
2 parents 92ba77b + ac05f7a commit 5bb8df5
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 30 deletions.
3 changes: 2 additions & 1 deletion script/Liquidate.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ contract LiquidateScript is Script, Logger {

Size size = Size(payable(sizeContractAddress));

LiquidateParams memory params = LiquidateParams({debtPositionId: 0, minimumCollateralProfit: 0});
LiquidateParams memory params =
LiquidateParams({debtPositionId: 0, minimumCollateralProfit: 0, deadline: block.timestamp});

vm.startBroadcast(deployerPrivateKey);
size.liquidate(params);
Expand Down
4 changes: 2 additions & 2 deletions test/local/actions/BuyCreditMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ contract BuyCreditMarketLendTest is BaseTest {
);
}

function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountOut_parametric(
function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountOut_specification(
uint256 A1,
uint256 A2,
uint256 deltaT1,
Expand Down Expand Up @@ -457,7 +457,7 @@ contract BuyCreditMarketLendTest is BaseTest {
);
}

function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountIn_parametric(
function testFuzz_BuyCreditMarket_buyCreditMarket_exactAmountIn_specification(
uint256 V1,
uint256 V2,
uint256 deltaT1,
Expand Down
8 changes: 5 additions & 3 deletions test/local/actions/Compensate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {RESERVED_ID} from "@src/libraries/LoanLibrary.sol";
import {SellCreditMarketParams} from "@src/libraries/actions/SellCreditMarket.sol";
import {SetUserConfiguration, SetUserConfigurationParams} from "@src/libraries/actions/SetUserConfiguration.sol";

import {Math} from "@src/libraries/Math.sol";
import {BuyCreditMarketParams} from "@src/libraries/actions/BuyCreditMarket.sol";
import {CompensateParams} from "@src/libraries/actions/Compensate.sol";
import {BaseTest} from "@test/BaseTest.sol";
Expand Down Expand Up @@ -444,15 +445,16 @@ contract CompensateTest is BaseTest {
uint256 tenor = 73 days;
uint256 dueDate = startDate + 73 days;
uint256 amount = 1000e6;
uint256 swapFee1 = size.getSwapFee(amount, tenor);
uint256 debtPositionId = _sellCreditMarket(alice, bob, RESERVED_ID, amount, tenor, false);
uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
uint256 apr = size.getLoanOfferAPR(bob, tenor);
uint256 r = Math.aprToRatePerTenor(apr, tenor);
uint256 swapFee1 = size.getSwapFee(Math.mulDivUp(futureValue, 1e18, 1e18 + r), tenor);
uint256 creditPositionId = size.getCreditPositionIdsByDebtPositionId(debtPositionId)[0];

assertEq(_state().feeRecipient.borrowATokenBalance, swapFee1);
assertEq(_state().alice.borrowATokenBalance, amount);

uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;

uint256 aliceCollateralBefore = _state().alice.collateralTokenBalance;

vm.warp(block.timestamp + 30 days);
Expand Down
5 changes: 3 additions & 2 deletions test/local/actions/Liquidate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ contract LiquidateTest is BaseTest {
_buyCreditLimit(alice, block.timestamp + 365 days, YieldCurveHelper.pointCurve(365 days, 0.03e18));
uint256 amount = 15e6;
uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, 365 days, false);
uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;

_setPrice(0.1e18);

Expand All @@ -107,7 +108,7 @@ contract LiquidateTest is BaseTest {

Vars memory _before = _state();

uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId, 0);
uint256 liquidatorProfit = _liquidate(liquidator, debtPositionId);

Vars memory _after = _state();

Expand All @@ -116,7 +117,7 @@ contract LiquidateTest is BaseTest {
assertEq(
_after.feeRecipient.borrowATokenBalance,
_before.feeRecipient.borrowATokenBalance,
size.getSwapFee(amount, 365 days)
size.getSwapFee(Math.mulDivUp(futureValue, 1e18, 1.03e18), 365 days)
);
assertEq(
_after.feeRecipient.collateralTokenBalance,
Expand Down
3 changes: 2 additions & 1 deletion test/local/actions/Multicall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ contract MulticallTest is BaseTest {

function test_Multicall_multicall_bypasses_cap_if_it_is_to_reduce_debt() public {
_setPrice(1e18);
_updateConfig("swapFeeAPR", 0);
uint256 amount = 100e6;
uint256 cap = amount + size.getSwapFee(100e6, 365 days);
uint256 cap = amount;
_updateConfig("borrowATokenCap", cap);

_deposit(alice, usdc, cap);
Expand Down
4 changes: 2 additions & 2 deletions test/local/actions/SelfLiquidate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract SelfLiquidateTest is BaseTest {
size.debtTokenAmountToCollateralTokenAmount(size.getDebtPosition(debtPositionId).futureValue);

vm.expectRevert();
_liquidate(liquidator, debtPositionId, debtInCollateralToken);
_liquidate(liquidator, debtPositionId, debtInCollateralToken, block.timestamp);

Vars memory _before = _state();

Expand Down Expand Up @@ -133,7 +133,7 @@ contract SelfLiquidateTest is BaseTest {
size.debtTokenAmountToCollateralTokenAmount(size.getDebtPosition(debtPositionId).futureValue);

vm.expectRevert();
_liquidate(liquidator, debtPositionId, futureValueInCollateralToken);
_liquidate(liquidator, debtPositionId, futureValueInCollateralToken, block.timestamp);

Vars memory _before = _state();

Expand Down
34 changes: 15 additions & 19 deletions test/local/actions/SellCreditMarket.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,17 @@ contract SellCreditMarketTest is BaseTest {
uint256 amount = 100e6;
uint256 tenor = 365 days;

uint256 futureValue = Math.mulDivUp(amount, (PERCENT + 0.03e18), PERCENT);
uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, tenor, false);

uint256 futureValueOpening = Math.mulDivUp(futureValue, size.riskConfig().crOpening, PERCENT);
uint256 minimumCollateral = size.debtTokenAmountToCollateralTokenAmount(futureValueOpening);
uint256 swapFee = size.getSwapFee(amount, tenor);
uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
uint256 issuanceValue = Math.mulDivDown(futureValue, PERCENT, PERCENT + 0.03e18);
uint256 swapFee = size.getSwapFee(issuanceValue, tenor);

Vars memory _after = _state();

assertGt(_before.bob.collateralTokenBalance, minimumCollateral);
assertEq(_after.alice.borrowATokenBalance, _before.alice.borrowATokenBalance - amount - swapFee);
assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + amount);
assertEq(_after.variablePool.collateralTokenBalance, _before.variablePool.collateralTokenBalance);
assertEq(_after.bob.debtBalance, size.getDebtPosition(debtPositionId).futureValue);
assertEq(_after.bob.debtBalance, futureValue);
}

function testFuzz_SellCreditMarket_sellCreditMarket_used_to_borrow(uint256 amount, uint256 apr, uint256 tenor)
Expand All @@ -66,22 +63,21 @@ contract SellCreditMarketTest is BaseTest {

Vars memory _before = _state();

uint256 rate = uint256(Math.aprToRatePerTenor(apr, tenor));
uint256 debt = Math.mulDivUp(amount, (PERCENT + rate), PERCENT);
uint256 rate = Math.aprToRatePerTenor(apr, tenor);

uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, amount, tenor, false);
uint256 debtOpening = Math.mulDivUp(debt, size.riskConfig().crOpening, PERCENT);
uint256 minimumCollateral = size.debtTokenAmountToCollateralTokenAmount(debtOpening);
uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
uint256 issuanceValue = Math.mulDivDown(futureValue, PERCENT, PERCENT + rate);

Vars memory _after = _state();

assertGt(_before.bob.collateralTokenBalance, minimumCollateral);
assertEq(
_after.alice.borrowATokenBalance,
_before.alice.borrowATokenBalance - amount - size.getSwapFee(amount, tenor)
_before.alice.borrowATokenBalance - amount - size.getSwapFee(issuanceValue, tenor)
);
assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + amount);
assertEq(_after.variablePool.collateralTokenBalance, _before.variablePool.collateralTokenBalance);
assertEq(_after.bob.debtBalance, size.getDebtPosition(debtPositionId).futureValue);
assertEq(_after.bob.debtBalance, futureValue);
}

function test_SellCreditMarket_sellCreditMarket_fragmentation() public {
Expand Down Expand Up @@ -516,15 +512,15 @@ contract SellCreditMarketTest is BaseTest {

Vars memory _before = _state();

_sellCreditMarket(bob, alice, RESERVED_ID, cash, tenor, false);
uint256 debtPositionId = _sellCreditMarket(bob, alice, RESERVED_ID, cash, tenor, false);
uint256 futureValue = size.getDebtPosition(debtPositionId).futureValue;
uint256 swapFeePercent = Math.mulDivUp(size.feeConfig().swapFeeAPR, tenor, 365 days);
uint256 r = Math.aprToRatePerTenor(apr, tenor);
uint256 swapFee = Math.mulDivUp(futureValue, swapFeePercent, PERCENT + r);

Vars memory _after = _state();

assertEq(
_after.alice.borrowATokenBalance,
_before.alice.borrowATokenBalance - cash - Math.mulDivUp(cash, swapFeePercent, PERCENT)
);
assertEq(_after.alice.borrowATokenBalance, _before.alice.borrowATokenBalance - cash - swapFee);
assertEq(_after.bob.borrowATokenBalance, _before.bob.borrowATokenBalance + cash);
}

Expand Down

0 comments on commit 5bb8df5

Please sign in to comment.