Skip to content

Commit

Permalink
fix: diff tests + fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Van0k committed Oct 31, 2023
1 parent 169a58d commit cf1f17f
Show file tree
Hide file tree
Showing 16 changed files with 925 additions and 12 deletions.
10 changes: 5 additions & 5 deletions contracts/adapters/convex/ConvexV1_Booster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ contract ConvexV1BoosterAdapter is AbstractAdapter, IConvexV1BoosterAdapter {
}

/// @notice Deposits the entire balance of Curve LP tokens into Booster, except the specified amount
/// @param leftoverAmount Amount of Curve LP to keep on the account
/// @param _pid ID of the pool to deposit to
/// @param leftoverAmount Amount of Curve LP to keep on the account
/// @param _stake Whether to stake Convex LP tokens in the rewards pool
function depositDiff(uint256 leftoverAmount, uint256 _pid, bool _stake)
function depositDiff(uint256 _pid, uint256 leftoverAmount, bool _stake)
external
override
creditFacadeOnly
Expand Down Expand Up @@ -135,9 +135,9 @@ contract ConvexV1BoosterAdapter is AbstractAdapter, IConvexV1BoosterAdapter {
}

/// @notice Withdraws all Curve LP tokens from Booster, except the specified amount
/// @param leftoverAmount Amount of Convex LP to keep on the account
/// @param _pid ID of the pool to withdraw from
function withdrawDiff(uint256 leftoverAmount, uint256 _pid)
/// @param leftoverAmount Amount of Convex LP to keep on the account
function withdrawDiff(uint256 _pid, uint256 leftoverAmount)
external
override
creditFacadeOnly
Expand Down Expand Up @@ -173,7 +173,7 @@ contract ConvexV1BoosterAdapter is AbstractAdapter, IConvexV1BoosterAdapter {

if (balance > leftoverAmount) {
unchecked {
(tokensToEnable, tokensToDisable,) = _executeSwapSafeApprove(
(tokensToEnable, tokensToDisable,) = _executeSwapNoApprove(
tokenIn,
tokenOut,
abi.encodeCall(IBooster.withdraw, (_pid, balance - leftoverAmount)),
Expand Down
21 changes: 21 additions & 0 deletions contracts/test/unit/adapters/AdapterUnitTestHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ contract AdapterUnitTestHelper is Test, CreditManagerV3MockEvents {

address[8] tokens;

uint256 diffMintedAmount = 1001;
uint256 diffLeftoverAmount;
uint256 diffInputAmount;
bool diffDisableTokenIn;

function _setUp() internal {
configurator = makeAddr("CONFIGURATOR");
creditFacade = makeAddr("CREDIT_FACADE");
Expand All @@ -47,6 +52,22 @@ contract AdapterUnitTestHelper is Test, CreditManagerV3MockEvents {
creditManager.setActiveCreditAccount(creditAccount);
}

modifier diffTestCases() {
uint256 snapshot = vm.snapshot();

diffLeftoverAmount = 501;
diffInputAmount = 500;
diffDisableTokenIn = false;
_;

vm.revertTo(snapshot);

diffLeftoverAmount = 1;
diffInputAmount = 1000;
diffDisableTokenIn = true;
_;
}

function _revertsOnNonConfiguratorCaller() internal {
vm.expectRevert(CallerNotConfiguratorException.selector);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ contract AaveV2_LendingPoolAdapterUnitTest is AdapterUnitTestHelper {
_revertsOnNonFacadeCaller();
adapter.deposit(address(0), 0, address(0), 0);

_revertsOnNonFacadeCaller();
adapter.depositDiff(address(0), 0);

_revertsOnNonFacadeCaller();
adapter.depositAll(address(0));

_revertsOnNonFacadeCaller();
adapter.withdraw(address(0), 0, address(0));

_revertsOnNonFacadeCaller();
adapter.withdrawDiff(address(0), 0);

_revertsOnNonFacadeCaller();
adapter.withdrawAll(address(0));
}
Expand Down Expand Up @@ -86,6 +92,26 @@ contract AaveV2_LendingPoolAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToDisable, 1, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-4A]: `depositDiff` works as expected
function test_U_AAVE2_04A_depositDiff_works_as_expected() public diffTestCases {
deal({token: tokens[0], to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: tokens[0],
tokenOut: tokens[1],
callData: abi.encodeCall(ILendingPool.deposit, (tokens[0], diffInputAmount, creditAccount, 0)),
requiresApproval: true,
validatesTokens: true
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.depositDiff(tokens[0], diffLeftoverAmount);

assertEq(tokensToEnable, 2, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? 1 : 0, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-5A]: `withdraw` works as expected
function test_U_AAVE2_05A_withdraw_works_as_expected() public {
_readsActiveAccount();
Expand Down Expand Up @@ -142,4 +168,24 @@ contract AaveV2_LendingPoolAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToEnable, 1, "Incorrect tokensToEnable");
assertEq(tokensToDisable, 2, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-6A]: `withdrawDiff` works as expected
function test_U_AAVE2_06A_withdrawDiff_works_as_expected() public diffTestCases {
deal({token: tokens[1], to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: tokens[1],
tokenOut: tokens[0],
callData: abi.encodeCall(ILendingPool.withdraw, (tokens[0], diffInputAmount, creditAccount)),
requiresApproval: false,
validatesTokens: true
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.withdrawDiff(tokens[0], diffLeftoverAmount);

assertEq(tokensToEnable, 1, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? 2 : 0, "Incorrect tokensToDisable");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,36 @@ contract AaveV2_WrappedATokenAdapterUnitTest is AdapterUnitTestHelper {
_revertsOnNonFacadeCaller();
adapter.deposit(0);

_revertsOnNonFacadeCaller();
adapter.depositDiff(0);

_revertsOnNonFacadeCaller();
adapter.depositAll();

_revertsOnNonFacadeCaller();
adapter.depositUnderlying(0);

_revertsOnNonFacadeCaller();
adapter.depositDiffUnderlying(0);

_revertsOnNonFacadeCaller();
adapter.depositAllUnderlying();

_revertsOnNonFacadeCaller();
adapter.withdraw(0);

_revertsOnNonFacadeCaller();
adapter.withdrawDiff(0);

_revertsOnNonFacadeCaller();
adapter.withdrawAll();

_revertsOnNonFacadeCaller();
adapter.withdrawUnderlying(0);

_revertsOnNonFacadeCaller();
adapter.withdrawDiffUnderlying(0);

_revertsOnNonFacadeCaller();
adapter.withdrawAllUnderlying();
}
Expand Down Expand Up @@ -113,6 +125,26 @@ contract AaveV2_WrappedATokenAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToDisable, aTokenMask, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-4A]: `depositDiff` works as expected
function test_U_AAVE2_04A_depositDiff_works_as_expected() public diffTestCases {
deal({token: aToken, to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: aToken,
tokenOut: waToken,
callData: abi.encodeCall(WrappedAToken.deposit, (diffInputAmount)),
requiresApproval: true,
validatesTokens: false
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.depositDiff(diffLeftoverAmount);

assertEq(tokensToEnable, waTokenMask, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? aTokenMask : 0, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2W-5]: `depositUnderlying` works as expected
function test_U_AAVE2W_05_depositUnderlying_works_as_expected() public {
_executesSwap({
Expand Down Expand Up @@ -148,6 +180,26 @@ contract AaveV2_WrappedATokenAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToDisable, tokenMask, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-6A]: `depositDiffUnderlying` works as expected
function test_U_AAVE2_06A_depositDiffUnderlying_works_as_expected() public diffTestCases {
deal({token: token, to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: token,
tokenOut: waToken,
callData: abi.encodeCall(WrappedAToken.depositUnderlying, (diffInputAmount)),
requiresApproval: true,
validatesTokens: false
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.depositDiffUnderlying(diffLeftoverAmount);

assertEq(tokensToEnable, waTokenMask, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? tokenMask : 0, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2W-7]: `withdraw` works as expected
function test_U_AAVE2W_07_withdraw_works_as_expected() public {
_executesSwap({
Expand Down Expand Up @@ -183,6 +235,26 @@ contract AaveV2_WrappedATokenAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToDisable, waTokenMask, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-8A]: `withdrawDiff` works as expected
function test_U_AAVE2_08A_withdrawDiff_works_as_expected() public diffTestCases {
deal({token: waToken, to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: waToken,
tokenOut: aToken,
callData: abi.encodeCall(WrappedAToken.withdraw, (diffInputAmount)),
requiresApproval: false,
validatesTokens: false
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.withdrawDiff(diffLeftoverAmount);

assertEq(tokensToEnable, aTokenMask, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? waTokenMask : 0, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2W-9]: `withdrawUnderlying` works as expected
function test_U_AAVE2W_09_withdrawUnderlying_works_as_expected() public {
_executesSwap({
Expand Down Expand Up @@ -217,4 +289,24 @@ contract AaveV2_WrappedATokenAdapterUnitTest is AdapterUnitTestHelper {
assertEq(tokensToEnable, tokenMask, "Incorrect tokensToEnable");
assertEq(tokensToDisable, waTokenMask, "Incorrect tokensToDisable");
}

/// @notice U:[AAVE2-10A]: `withdrawDiffUnderlying` works as expected
function test_U_AAVE2_10A_withdrawDiffUnderlying_works_as_expected() public diffTestCases {
deal({token: waToken, to: creditAccount, give: diffMintedAmount});

_readsActiveAccount();
_executesSwap({
tokenIn: waToken,
tokenOut: token,
callData: abi.encodeCall(WrappedAToken.withdrawUnderlying, (diffInputAmount)),
requiresApproval: false,
validatesTokens: false
});

vm.prank(creditFacade);
(uint256 tokensToEnable, uint256 tokensToDisable) = adapter.withdrawDiffUnderlying(diffLeftoverAmount);

assertEq(tokensToEnable, tokenMask, "Incorrect tokensToEnable");
assertEq(tokensToDisable, diffDisableTokenIn ? waTokenMask : 0, "Incorrect tokensToDisable");
}
}
Loading

0 comments on commit cf1f17f

Please sign in to comment.