Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: high
Valid

Incorrect Credit Capacity Calculation

Summary

A critical issue exists in the Market::getCreditCapacityUsd function where the total debt is incorrectly added to the delegated credit instead of being subtracted. This results in an overestimation of the credit capacity, which could lead to excessive borrowing, liquidity issues, and potential insolvency.


Vulnerability Details

Affected Code

The bug is in the Market::getCreditCapacityUsd function:
https://github.com/Cyfrin/2025-01-zaros-part-2/blob/35deb3e92b2a32cd304bf61d27e6071ef36e446d/src/market-making/leaves/Market.sol#L197

function getCreditCapacityUsd(
UD60x18 delegatedCreditUsdX18,
SD59x18 totalDebtUsdX18
)
internal
pure
returns (SD59x18 creditCapacityUsdX18)
{
// Incorrect formula: adds total debt instead of subtracting it
creditCapacityUsdX18 = delegatedCreditUsdX18.intoSD59x18().add(totalDebtUsdX18);
}

totalDebtUsdX18 is the addition of realized and unrealized debt and will be positive if the market has debt.

Expected Behavior

The credit capacity should be calculated as:

creditCapacityUsdX18 = delegatedCreditUsdX18 - totalDebtUsdX18
  • If the result is negative, the credit capacity should be set to zero.

Actual Behavior

The credit capacity is calculated as:

creditCapacityUsdX18 = delegatedCreditUsdX18 + totalDebtUsdX18
  • This adds the total debt to the delegated credit, leading to an overestimation of the credit capacity.


Impact


Tools Used

The bug was identified through a detailed review of the getCreditCapacityUsd function.

Recommendations

Update the formula to subtract the total debt from the delegated credit:

creditCapacityUsdX18 = delegatedCreditUsdX18.intoSD59x18().sub(totalDebtUsdX18);
Updates

Lead Judging Commences

inallhonesty Lead Judge 6 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Market::getCreditCapacityUsd overestimates the credit capacity (it adds instead of substracting)

Support

FAQs

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