Skip to content

Conversation

@aalavandhan
Copy link
Member

@aalavandhan aalavandhan commented Aug 29, 2024

Skipping liquidity check when swapping from perps to underlying.

Swapping from perps to underlying should almost always increase the vault's liquidity, so we only check if underlying balance doesn't decrease.

@openzeppelin-code
Copy link

openzeppelin-code bot commented Aug 29, 2024

Swap limit bug fix

Generated at commit: 2e2204c6c2a86961277204c8aa623798d5cd8e0b

🚨 Report Summary

Severity Level Results
Contracts Critical
High
Medium
Low
Note
Total
0
0
0
3
23
26
Dependencies Critical
High
Medium
Low
Note
Total
0
0
0
0
0
0

For more details view the full report in OpenZeppelin Code Inspector

_enforceUnderlyingBalAfterSwap(underlying_, s.vaultTVL);
// Revert if swap reduces vault's available liquidity.
uint256 underlyingBalPost = underlying_.balanceOf(address(this));
if (underlyingBalPost <= underlyingBalPre) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (underlyingBalPost <= underlyingBalPre) {
if (underlyingBalPost < underlyingBalPre) {

Should be ok if it remains unchanged?

_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));
Copy link
Member

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:

If underlying bal increases or stays the same, continue
else, execute the check logic you have here.

That situation would be exceptionally rare, though. Worth it? It would be one extra var and a conditional, I think.

Copy link
Member Author

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..

Copy link
Member

@brandoniles brandoniles left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@aalavandhan aalavandhan merged commit e8bfda7 into main Sep 3, 2024
@aalavandhan aalavandhan deleted the swap-limit-fix branch September 3, 2024 18:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants