@@ -440,8 +440,9 @@ contract RolloverVault is
440
440
function swapUnderlyingForPerps (uint256 underlyingAmtIn ) external nonReentrant whenNotPaused returns (uint256 ) {
441
441
// Calculates the fee adjusted perp amount to transfer to the user.
442
442
// NOTE: This operation should precede any token transfers.
443
- IERC20Upgradeable underlying_ = underlying;
444
443
IPerpetualTranche perp_ = perp;
444
+ IERC20Upgradeable underlying_ = underlying;
445
+ uint256 underlyingBalPre = underlying_.balanceOf (address (this ));
445
446
(uint256 perpAmtOut , uint256 perpFeeAmtToBurn , SubscriptionParams memory s ) = computeUnderlyingToPerpSwapAmt (
446
447
underlyingAmtIn
447
448
);
@@ -468,8 +469,16 @@ contract RolloverVault is
468
469
// NOTE: In case this operation mints slightly more perps than that are required for the swap,
469
470
// The vault continues to hold the perp dust until the subsequent `swapPerpsForUnderlying` or manual `recover(perp)`.
470
471
471
- // Revert if vault liquidity is too low.
472
- _enforceUnderlyingBalAfterSwap (underlying_, s.vaultTVL);
472
+ // If vault liquidity has reduced, revert if it reduced too much.
473
+ // - Absolute balance is strictly greater than `minUnderlyingBal`.
474
+ // - Ratio of the balance to the vault's TVL is strictly greater than `minUnderlyingPerc`.
475
+ uint256 underlyingBalPost = underlying_.balanceOf (address (this ));
476
+ if (
477
+ underlyingBalPost < underlyingBalPre &&
478
+ (underlyingBalPost <= minUnderlyingBal || underlyingBalPost.mulDiv (ONE, s.vaultTVL) <= minUnderlyingPerc)
479
+ ) {
480
+ revert InsufficientLiquidity ();
481
+ }
473
482
474
483
// sync underlying
475
484
_syncAsset (underlying_);
@@ -482,11 +491,8 @@ contract RolloverVault is
482
491
// Calculates the fee adjusted underlying amount to transfer to the user.
483
492
IPerpetualTranche perp_ = perp;
484
493
IERC20Upgradeable underlying_ = underlying;
485
- (
486
- uint256 underlyingAmtOut ,
487
- uint256 perpFeeAmtToBurn ,
488
- SubscriptionParams memory s
489
- ) = computePerpToUnderlyingSwapAmt (perpAmtIn);
494
+ uint256 underlyingBalPre = underlying_.balanceOf (address (this ));
495
+ (uint256 underlyingAmtOut , uint256 perpFeeAmtToBurn , ) = computePerpToUnderlyingSwapAmt (perpAmtIn);
490
496
491
497
// Revert if insufficient tokens are swapped in or out
492
498
if (underlyingAmtOut <= 0 || perpAmtIn <= 0 ) {
@@ -507,8 +513,11 @@ contract RolloverVault is
507
513
// transfer underlying out
508
514
underlying_.safeTransfer (msg .sender , underlyingAmtOut);
509
515
510
- // Revert if vault liquidity is too low.
511
- _enforceUnderlyingBalAfterSwap (underlying_, s.vaultTVL);
516
+ // Revert if swap reduces vault's available liquidity.
517
+ uint256 underlyingBalPost = underlying_.balanceOf (address (this ));
518
+ if (underlyingBalPost < underlyingBalPre) {
519
+ revert InsufficientLiquidity ();
520
+ }
512
521
513
522
// sync underlying
514
523
_syncAsset (underlying_);
@@ -964,15 +973,4 @@ contract RolloverVault is
964
973
(uint256 trancheClaim , uint256 trancheSupply ) = tranche.getTrancheCollateralization (collateralToken);
965
974
return trancheClaim.mulDiv (trancheAmt, trancheSupply, MathUpgradeable.Rounding.Up);
966
975
}
967
-
968
- /// @dev Checks if the vault's underlying balance is above admin defined constraints.
969
- /// - Absolute balance is strictly greater than `minUnderlyingBal`.
970
- /// - Ratio of the balance to the vault's TVL is strictly greater than `minUnderlyingPerc`.
971
- /// NOTE: We assume the vault TVL and the underlying to have the same base denomination.
972
- function _enforceUnderlyingBalAfterSwap (IERC20Upgradeable underlying_ , uint256 vaultTVL ) private view {
973
- uint256 underlyingBal = underlying_.balanceOf (address (this ));
974
- if (underlyingBal <= minUnderlyingBal || underlyingBal.mulDiv (ONE, vaultTVL) <= minUnderlyingPerc) {
975
- revert InsufficientLiquidity ();
976
- }
977
- }
978
976
}
0 commit comments