Summary
Getting the price of getRealizedDebtUSD allows market to update the debts for markets. The accounting is incorrect due to decimal scaling.
Vulnerability Details
Within leaves/Market.sol. We know that ud60x18
types are all scaled into 1e18. The creditDepositsValueUsdX18 is calculated using the returned adjusted price of 1e18 multiplied by the value which is also in 1e18. The implementation did not divide creditDepositsValueUsdX18 by 1e18, resulting in inflated prices.getRealizedDebtUsd
is called, the realized debt is wrongly scaled, resulting in incorrect PnL.
function getRealizedDebtUsd(Data storage self) internal view returns (SD59x18 realizedDebtUsdX18) {
UD60x18 creditDepositsValueUsdX18;
if (block.timestamp <= self.lastCreditDepositsValueRehydration) {
creditDepositsValueUsdX18 = ud60x18(self.creditDepositsValueCacheUsd);
} else {
creditDepositsValueUsdX18 = getCreditDepositsValueUsd(self);
}
realizedDebtUsdX18 = creditDepositsValueUsdX18.intoSD59x18().add(sd59x18(self.netUsdTokenIssuance));
}
function getCreditDepositsValueUsd(Data storage self) internal view returns (UD60x18 creditDepositsValueUsdX18) {
EnumerableMap.AddressToUintMap storage creditDeposits = self.creditDeposits;
uint256 creditDepositsLength = creditDeposits.length();
for (uint256 i; i < creditDepositsLength; i++) {
(address asset, uint256 value) = creditDeposits.at(i);
Collateral.Data storage collateral = Collateral.load(asset);
creditDepositsValueUsdX18 =
creditDepositsValueUsdX18.add((collateral.getAdjustedPrice().mul(ud60x18(value))));
}
}
Which inturns affects the debt distribution within Vault.sol
function _recalculateConnectedMarketsState(
Data storage self,
uint128[] memory connectedMarketsIdsCache,
bool shouldRehydrateCache
)
-- SNIP --
if (!ctx.marketUnrealizedDebtUsdX18.isZero() || !ctx.marketRealizedDebtUsdX18.isZero()) {
market.distributeDebtToVaults(ctx.marketUnrealizedDebtUsdX18, ctx.marketRealizedDebtUsdX18);
}
-- SNIP --
Impact
Decimal precision is wrongly scaled, resulting in inflated price.
Tools Used
Recommendations
DIvide creditDepositsValueUsdX18 by 1e18.