Skip to content
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

fix: sherlock-audit/2023-03-notional-judging#204 sherlock-audit/2023-03-notional-judging#194 incorrect inequality in deleverage vault #132

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contracts/internal/vaults/VaultValuation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ library VaultValuation {
// value). Resolving this would require additional complexity for not much gain. An
// account within 20% of the minBorrowSize in a vault that has fCash discounting enabled
// may experience a full liquidation as a result.
int256 postLiquidationDebtOutstanding = h.netDebtOutstanding[currencyIndex].neg().sub(depositUnderlyingInternal);
require(
h.netDebtOutstanding[currencyIndex].sub(depositUnderlyingInternal) < minBorrowSize,
postLiquidationDebtOutstanding == 0 || minBorrowSize <= postLiquidationDebtOutstanding,
"Must Liquidate All Debt"
);
} else {
Expand Down
45 changes: 42 additions & 3 deletions tests/stateful/vaults/test_vault_deleverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,48 @@ def check_deleverage_invariants(

check_system_invariants(environment, accounts, [vault])


@given(
currencyId=strategy("uint", min_value=1, max_value=3),
isPrime=strategy("bool"),
enablefCashDiscount=strategy("bool"),
)
def test_cannot_deleverage_below_min_borrow(
environment, accounts, currencyId, isPrime, enablefCashDiscount
):
# This vault price forces a full liquidation
vaultPrice = 0.92
e = setup_deleverage_conditions(
environment, accounts, currencyId, isPrime, enablefCashDiscount, vaultPrice
)
environment.notional.updateVault(
e['vault'].address,
get_vault_config(
currencyId=currencyId,
flags=set_flags(
0, ENABLED=True, ENABLE_FCASH_DISCOUNT=enablefCashDiscount, ALLOW_ROLL_POSITION=True
),
minAccountBorrowSize=100 * e['multiple'],
),
100_000_000e8,
)

depositAmount = 125 * e['multiple'] * 1e8
with brownie.reverts("Must Liquidate All Debt"):
# Min Borrow is 100, debt is 200, must liquidate down to zero if going below
# 100 debt outstanding
environment.notional.deleverageAccount(
accounts[1],
e["vault"].address,
accounts[2],
0,
depositAmount,
{"from": accounts[2], "value": depositAmount * 1e10 + 1e10 if currencyId == 1 else 0},
)




@given(
currencyId=strategy("uint", min_value=1, max_value=3),
isPrime=strategy("bool"),
Expand All @@ -382,7 +424,6 @@ def check_deleverage_invariants(
def test_deleverage_account_partial(
environment, accounts, currencyId, isPrime, enablefCashDiscount, deleverageShare
):
isPrime = False
# TODO: if we reduce the vault price, we may need to liquidate full...
vaultPrice = 0.955
e = setup_deleverage_conditions(
Expand Down Expand Up @@ -790,7 +831,6 @@ def setup_deleverage_account_over_debt_balance(

return e

@pytest.mark.only
@given(currencyId=strategy("uint", min_value=1, max_value=3))
def test_excess_cash_can_exit(environment, accounts, currencyId):
e = setup_deleverage_account_over_debt_balance(environment, accounts, currencyId)
Expand Down Expand Up @@ -840,7 +880,6 @@ def test_excess_cash_can_exit(environment, accounts, currencyId):
assert pytest.approx(expectedUnderlyingWithdraw, rel=1e-5) == balanceAfter - balanceBefore
check_system_invariants(environment, accounts, [e['vault']])

@pytest.mark.only
@given(currencyId=strategy("uint", min_value=1, max_value=3))
def test_excess_cash_can_settle(environment, accounts, currencyId):
e = setup_deleverage_account_over_debt_balance(environment, accounts, currencyId)
Expand Down