<!-- canonical: https://0xsimao.com/findings/maple-finance-iii-term-date-funded-gas -->

# open-term-loan-private: check dateFunded!=0 first to save gas.

Low/Info · Three Sigma · Institutional lending · 10th April, 2024

Finding 3S-MAPLE-N09 of the Maple Finance security review.

- Protocol: https://maple.finance/
- Report: /reports/maple-finance-iii-2024
- Source: https://cdn.sanity.io/files/qoqld077/production/36dbe5ca76da3d2392bcee581548067705b8bd36.pdf

---

### Description

On open-term-loan in function makePayment(), first the payment breakdown is called and
after that the loan is checked to be active. getPaymentBreakdown() does several storage
reads. dateFunded is not changed within the function scope, so the check if the loan is
active (and the check for the principal as well) can be done before getPaymentBreakdown
is called and save on gas from the storage reads in case the loan is inactive.

### Recommendation

Change lines 173-178 to:

```solidity
require(dateFunded != 0, "ML:MP:LOAN_INACTIVE");
require(principalToReturn_ <= principal, "ML:MP:RETURNING_TOO_MUCH");
(calledPrincipal_, interest_, lateInterest_, delegateServiceFee_, platformServiceFee_) = getPaymentBreakdown(block.timestamp);

require(principalToReturn_ >= calledPrincipal_, "ML:MP:INSUFFICIENT_FOR_CALL");
```

### Status

Addressed in the following PR:
https://github.com/maple-labs/open-term-loan-private/pull/54

---

Related findings:

- [In OstiumTradingStorage, firstEmptyTradeIndex() and firstEmptyOpenLimitIndex() overwrite index 0 if not found](https://0xsimao.com/findings/ostium-storage-index-overwrite-found): Ostium
- [In glAVAX, checking that amount > 0 earlier can save some gas](https://0xsimao.com/findings/glacier-avax-checking-earlier-gas): Glacier
- [Storage variables should be cached whenever possible to save gas](https://0xsimao.com/findings/glacier-storage-cached-whenever-gas): Glacier
- [genesisLimit can be placed as constant variables to save gas.](https://0xsimao.com/findings/metazero-i-genesis-placed-constant-gas): MetaZero Omnichain NFTs
