<!-- canonical: https://0xsimao.com/findings/ostium-average-tracks-sum-epoch -->

# OstiumOpenPnl::average() can be simplified by adding a state variable that tracks the cumulative sum of nextEpochValues

Low/Info · Three Sigma · RWA perpetuals DEX · 19th February, 2024

Finding 3S-OS-N20 of the Ostium security review.

- Protocol: https://www.ostium.com/
- Report: /reports/ostium
- Source: https://cdn.sanity.io/files/qoqld077/production/a95b9c69e0f65d1d6b0e649f0d62a362358ca8ce.pdf

---

### Description

[OstiumOpenPnl::average()](https://github.com/0xOstium/smart-contracts-threeSigma/blob/audit-feedback/src/OstiumOpenPnl.sol#L187) calculates the average of all the nextEpochValues, which is an
expensive operation (depending on requestsCount and could be replaced by a state
variable tracking the cummulative sum.

### Status

Acknowledged

---

Related findings:

- [_tempTokenToCheck in BaseRouter does not need to be a state variable](https://0xsimao.com/findings/fuji-finance-temp-need-state-variable): Fuji Finance
- [Cap::_checkRemoteStateAndAdvance() may be optimized by returning early as soon as a different epoch is found in a remote domain](https://0xsimao.com/findings/mitosis-remote-returning-soon-epoch): Mitosis
