Skip to content

Commit

Permalink
skipping liquidity check when swapping from perps to underlying
Browse files Browse the repository at this point in the history
  • Loading branch information
aalavandhan committed Aug 29, 2024
1 parent 596b353 commit 23de243
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions spot-contracts/contracts/RolloverVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,12 @@ contract RolloverVault is
// The vault continues to hold the perp dust until the subsequent `swapPerpsForUnderlying` or manual `recover(perp)`.

// Revert if vault liquidity is too low.
_enforceUnderlyingBalAfterSwap(underlying_, s.vaultTVL);
// - Absolute balance is strictly greater than `minUnderlyingBal`.
// - Ratio of the balance to the vault's TVL is strictly greater than `minUnderlyingPerc`.
uint256 underlyingBal = underlying_.balanceOf(address(this));
if (underlyingBal <= minUnderlyingBal || underlyingBal.mulDiv(ONE, s.vaultTVL) <= minUnderlyingPerc) {
revert InsufficientLiquidity();
}

// sync underlying
_syncAsset(underlying_);
Expand All @@ -482,11 +487,7 @@ contract RolloverVault is
// Calculates the fee adjusted underlying amount to transfer to the user.
IPerpetualTranche perp_ = perp;
IERC20Upgradeable underlying_ = underlying;
(
uint256 underlyingAmtOut,
uint256 perpFeeAmtToBurn,
SubscriptionParams memory s
) = computePerpToUnderlyingSwapAmt(perpAmtIn);
(uint256 underlyingAmtOut, uint256 perpFeeAmtToBurn, ) = computePerpToUnderlyingSwapAmt(perpAmtIn);

// Revert if insufficient tokens are swapped in or out
if (underlyingAmtOut <= 0 || perpAmtIn <= 0) {
Expand All @@ -507,8 +508,8 @@ contract RolloverVault is
// transfer underlying out
underlying_.safeTransfer(msg.sender, underlyingAmtOut);

// Revert if vault liquidity is too low.
_enforceUnderlyingBalAfterSwap(underlying_, s.vaultTVL);
// NOTE: This operation increases the vault's underlying liquidity,
// so we skip the liquidity level check.

// sync underlying
_syncAsset(underlying_);
Expand Down Expand Up @@ -964,15 +965,4 @@ contract RolloverVault is
(uint256 trancheClaim, uint256 trancheSupply) = tranche.getTrancheCollateralization(collateralToken);
return trancheClaim.mulDiv(trancheAmt, trancheSupply, MathUpgradeable.Rounding.Up);
}

/// @dev Checks if the vault's underlying balance is above admin defined constraints.
/// - Absolute balance is strictly greater than `minUnderlyingBal`.
/// - Ratio of the balance to the vault's TVL is strictly greater than `minUnderlyingPerc`.
/// NOTE: We assume the vault TVL and the underlying to have the same base denomination.
function _enforceUnderlyingBalAfterSwap(IERC20Upgradeable underlying_, uint256 vaultTVL) private view {
uint256 underlyingBal = underlying_.balanceOf(address(this));
if (underlyingBal <= minUnderlyingBal || underlyingBal.mulDiv(ONE, vaultTVL) <= minUnderlyingPerc) {
revert InsufficientLiquidity();
}
}
}

0 comments on commit 23de243

Please sign in to comment.