<!-- canonical: https://0xsimao.com/findings/mitosis-resolve-idle-calculating-gas -->

# BasicVault::_resolveWithIdleBalance() can return before calculating _idleBalance() to save gas

Low/Info · Three Sigma · Cross-chain liquidity layer 1 · 25th June, 2024

Finding 3S-Mitosis-N12 of the Mitosis security review.

- Protocol: https://mitosis.org/
- Report: /reports/mitosis
- Source: https://cdn.sanity.io/files/qoqld077/production/b6b3bd7bb47407d99e76abb7c6dc615c1db5018e.pdf

---

### Description

BasicVault::_resolveWithIdleBalance() returns when there is enough reserved
amount to fulfill all requests. In this case, there is no need to fetch the _idleBalance(),
should the order should be inverted to save gas.

### Recommendation

```solidity
function _resolveWithIdleBalance(StorageV2 storage $v2, IERC20 asset_)
internal returns (uint256 resolved) {
    uint256 remaining = $v2.redeemQueue.remaining();
    if (remaining == 0) return 0;
    uint256 idleBalance_ = _idleBalance($v2, asset_);
    resolved = remaining > idleBalance_ ? idleBalance_ : remaining;
    $v2.redeemQueue.reserve(resolved);
    return resolved;
}
```

### Status

Addressed in [#a607d6c](https://github.com/mitosis-org/evm/pull/201/commits/a607d6c107ba8f5373a01aa8d9a078c41fc1dea0).

---

Related findings:

- [Destination Vault rewards are not added to idleIncrease when info.totalAssetsPulled > info.totalAssetsToPull](https://0xsimao.com/findings/tokemak-rewards-info-pulled-pull): Tokemak
- [`Vault::withdraw()` withdraws too much liquidity leading to idle capital and loss of fees](https://0xsimao.com/findings/yieldoor-withdraw-withdraws-capital-fees): Yieldoor
- [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
