DeFiFoundry
60,000 USDC
View results
Submission Details
Severity: medium
Invalid

Potential losses in zero or negative leverage scenarios not covered by protocol

Summary

Losses when leverage is zero or negative which protocol does not account for

Vulnerability Details

function getAccountLeverage(uint128 tradingAccountId) external view returns (UD60x18) {
TradingAccount.Data storage tradingAccount = TradingAccount.loadExisting(tradingAccountId);
SD59x18 marginBalanceUsdX18 = tradingAccount.getMarginBalanceUsd(tradingAccount.getAccountUnrealizedPnlUsd());
UD60x18 totalPositionsNotionalValue;
if (marginBalanceUsdX18.isZero()) return marginBalanceUsdX18.intoUD60x18();
for (uint256 i; i < tradingAccount.activeMarketsIds.length(); i++) {
uint128 marketId = tradingAccount.activeMarketsIds.at(i).toUint128();
PerpMarket.Data storage perpMarket = PerpMarket.load(marketId);
Position.Data storage position = Position.load(tradingAccountId, marketId);
UD60x18 indexPrice = perpMarket.getIndexPrice();
UD60x18 markPrice = perpMarket.getMarkPrice(unary(sd59x18(position.size)), indexPrice);
UD60x18 positionNotionalValueX18 = position.getNotionalValue(markPrice);
totalPositionsNotionalValue = totalPositionsNotionalValue.add(positionNotionalValueX18);
}
return totalPositionsNotionalValue.intoSD59x18().div(marginBalanceUsdX18).intoUD60x18();
}

This is an edge case which the code does not consider:

The getAccountLeverage function doesn't handle the case where there might be open positions but zero or negative margin balance, which could lead to incorrect leverage calculations or potential division by zero errors.

Zero Margin Balance:

  • Liquidation: When the margin balance reaches zero, the position is at risk of liquidation. If the value of the collateral drops to the maintenance margin level, the exchange will liquidate the position to cover the losses.

  • Profit and Loss: If there are unrealized profits in the position but have withdrawn all available margin, the margin balance could be zero. The open positions remain, but any adverse price movement could quickly lead to liquidation.

Negative Margin Balance:

  • Extreme Market Movements: In cases of extreme market volatility, the position might incur losses faster than the exchange can liquidate it, leading to a negative margin balance. This means traders owe the exchange more than their collateral value.

  • Fee Accumulation: If fees such as funding rates or trading fees are not covered by the available margin, they can push the balance into the negative.

Impact

There will be further losses for the protocol when this happens. There will be liquidations for traders as they may not receive any alerts to add more collateral or risk liquidations. There will be losses during this systemic risk scenario.

Tools Used

Manual Review

Recommendations

Account for these edge cases in the code.

Updates

Lead Judging Commences

inallhonesty Lead Judge
about 1 year ago
inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.