Summary
Each time when getRealizedDebtUsd
function is called it checks whether block.timestamp <= self.lastCreditDepositsValueRehydration
. Which will be always false because lastCreditDepositsValueRehydration
is never updated. This is causing recalculation of credit deposits every time (see line 243 bellow).
[src/market-making/leaves/Market.sol]
233
234 function getRealizedDebtUsd(Data storage self) internal view returns (SD59x18 realizedDebtUsdX18) {
235
236 UD60x18 creditDepositsValueUsdX18;
237
238
239 if (block.timestamp <= self.lastCreditDepositsValueRehydration) {
240 creditDepositsValueUsdX18 = ud60x18(self.creditDepositsValueCacheUsd);
241 } else {
242
243 creditDepositsValueUsdX18 = getCreditDepositsValueUsd(self);
244 }
245
246
247
248 realizedDebtUsdX18 = creditDepositsValueUsdX18.intoSD59x18().add(sd59x18(self.netUsdTokenIssuance));
249 }
Impact
The whole idea of lastCreditDepositsValueRehydration
is to reduce gas costs:
Recommendation
Call rehydrateCreditDepositsValueCache
function everytime if the credit deposits usd value cache is not up to date.
[src/market-making/leaves/Market.sol]
475
476
477
478 function rehydrateCreditDepositsValueCache(Data storage self)
479 internal
480 returns (UD60x18 creditDepositsValueUsdX18)
481 {
482 creditDepositsValueUsdX18 = getCreditDepositsValueUsd(self);
483 self.creditDepositsValueCacheUsd = creditDepositsValueUsdX18.intoUint128();
484 self.lastCreditDepositsValueRehydration = block.timestamp.toUint128();
485 }