# Inaccurate Perp debt calculation

PerpDepository.getDebtValue omits owedRealizedPnl from Perp account debt, diverging from Perp's own settlement value formula and misreporting debt.

- Payout: Contest reward
- Severity: medium
- Program: Uxd
- Credited: peanuts
- Disclosed: 2022-12-27
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2023-01-uxd-judging/issues/346
- Library page: https://bugbountylibrary.com/r/sherlock%3A2023-01-uxd%3AM-6
- Tags: domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error

**Target:** PerpDepository.getDebtValue view function (Perp integration)
**Root cause:** getDebtValue computes debt as quote balance + unrealized PnL - pending fee - funding payment, omitting owedRealizedPnl that Perp's Vault.getSettlementTokenValue includes.
**Steps:**
1. Read PerpDepository.getDebtValue at integrations/perp/PerpDepository.sol#L773
2. Note getPnlAndPendingFee returns (owedRealizedPnl, unrealizedPnl, pendingFee) but owedRealizedPnl is discarded
3. Compare to Perp Vault._getSettlementTokenBalanceAndUnrealizedPnl which adds owedRealizedPnl to settlement balance
4. Observe debt formula diverges from Perp's accountValue spec: collateral + owedRealizedPnl + pendingFunding + pendingFee + sum(unrealizedPnl)
**How found:** Code review comparing UXD's debt formula against Perp's own Vault.getSettlementTokenValue and accountValue spec; noticed the discarded first return value of getPnlAndPendingFee.
**Insight:** When integrating a lending/perp protocol, mirror the protocol's own account-value formula exactly; check every return tuple element of getPnlAndPendingFee is used, since owedRealizedPnl is easy to drop.
