Summary
Function Market::getCreditCapacityUsd adds delegatedCredit to totalDebt inflating the market's credit capacity. The more debt a market has, the more credit capacity allowing accumulating even more debt.
In getCreditCapacityUsd the sum delegatedCredit + totalDebt is returned.
This function is called from getAdjustedProfitForMarketId and withdrawUsdTokenFromMarketId
In both cases the values returned by market.getTotalDebt() and market.getTotalDelegatedCreditUsd() are passed further to getCreditCapacityUsd to calculate how much credit the market has left, if any.
While getTotalDelegatedCreditUsd returns a positive number, representing the credit value, getTotalDebt can also return negative values.
This function returns the sum of unrealized Debt and realized debt. As mentioned in contest known issues, PerpsEngine::getUnrealizedDebt returns a zero value which means getTotalDebt returns the value from getRealizedDebtUsd
NOTE: getRealizedDebtUsd has another issue reported in another submission.
For simplicity let's suppose creditDepositsValueUsdX18 from below is 0.
Market's netUsdTokenIssuance is increased each time usdToken is minted (to pay for profitable traders) and decreased when the market makes a profit.
This means that a negative value returned by getRealizedDebtUsd or getTotalDebt is a credit for market.
Going back to the getCreditCapacityUsd function, it can be seen why it returns the wrong value. The debt amount should be subtracted from the delegated credit, not added together as in the current implementation
Market credit capacity can increase indefinitely, increasing the risk for traders and vault LPs.
If debt is much bigger than allocated credit, traders will be paid in a token with no value (internal usdToken) since there will be no assets left in vaults to cover for debt.
In getCreditCapacityUsd the debt should be deducted from allocated credit.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.