-
Notifications
You must be signed in to change notification settings - Fork 9
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
Swap limit bug fix #221
Merged
Merged
Swap limit bug fix #221
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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_); | ||||||
|
@@ -482,11 +487,8 @@ 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 underlyingBalPre = underlying_.balanceOf(address(this)); | ||||||
(uint256 underlyingAmtOut, uint256 perpFeeAmtToBurn, ) = computePerpToUnderlyingSwapAmt(perpAmtIn); | ||||||
|
||||||
// Revert if insufficient tokens are swapped in or out | ||||||
if (underlyingAmtOut <= 0 || perpAmtIn <= 0) { | ||||||
|
@@ -507,8 +509,11 @@ contract RolloverVault is | |||||
// transfer underlying out | ||||||
underlying_.safeTransfer(msg.sender, underlyingAmtOut); | ||||||
|
||||||
// Revert if vault liquidity is too low. | ||||||
_enforceUnderlyingBalAfterSwap(underlying_, s.vaultTVL); | ||||||
// Revert if swap reduces vault's available liquidity. | ||||||
uint256 underlyingBalPost = underlying_.balanceOf(address(this)); | ||||||
if (underlyingBalPost <= underlyingBalPre) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Should be ok if it remains unchanged? |
||||||
revert InsufficientLiquidity(); | ||||||
} | ||||||
|
||||||
// sync underlying | ||||||
_syncAsset(underlying_); | ||||||
|
@@ -964,15 +969,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(); | ||||||
} | ||||||
} | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there's a weird corner case where underlying balance could go up (or maybe just remain constant?) after an underlying->perp swap.
e.g. If the vault was sitting on a bunch of As, and the new Zs free those up.
In that case I think we'd want to allow that to go through.
Perhaps the most generalized logic would be:
That situation would be exceptionally rare, though. Worth it? It would be one extra var and a conditional, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool. So basically if the underlying balances goes down, we then ensure that it doesn't go down too much. If it stays the same or increases its fine..