Part 2

Zaros
PerpetualsDEXFoundrySolidity
70,000 USDC
View results
Submission Details
Severity: low
Invalid

`lastCreditDepositsValueRehydration` is never updated leading to unnecessary gas cost

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 /// @return realizedDebtUsdX18 The market's net realized debt in USD as SD59x18.
234 function getRealizedDebtUsd(Data storage self) internal view returns (SD59x18 realizedDebtUsdX18) {
235 // prepare the credit deposits usd value variable;
236 UD60x18 creditDepositsValueUsdX18;
237
238 // if the credit deposits usd value cache is up to date, return the stored value
239 if (block.timestamp <= self.lastCreditDepositsValueRehydration) {
240 creditDepositsValueUsdX18 = ud60x18(self.creditDepositsValueCacheUsd);
241 } else {
242 // otherwise, we'll need to loop over credit deposits to calculate it
243 creditDepositsValueUsdX18 = getCreditDepositsValueUsd(self);
244 }
245
246 // finally after determining the market's latest credit deposits usd value, sum it with the stored net usd
247 // token issuance to return the net realized debt usd value
248 realizedDebtUsdX18 = creditDepositsValueUsdX18.intoSD59x18().add(sd59x18(self.netUsdTokenIssuance));
249 }

Impact

The whole idea of lastCreditDepositsValueRehydration is to reduce gas costs:

/// @param lastCreditDepositsValueRehydration The last timestamp where `creditDepositsValueCacheUsd` has been
/// updated, avoids wasting gas.

Recommendation

Call rehydrateCreditDepositsValueCache function everytime if the credit deposits usd value cache is not up to date.

[src/market-making/leaves/Market.sol]
475 /// @notice Rehydrates the credit deposits usd value cache and returns its latest value to the caller.
476 /// @param self The market storage pointer.
477 /// @return creditDepositsValueUsdX18 The market's credit deposits value in USD.
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 }
Updates

Lead Judging Commences

inallhonesty Lead Judge
6 months ago
inallhonesty Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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