<!-- canonical: https://0xsimao.com/findings/vertex-precision-engines-muldiv-calculating -->

# Precision loss due to engines not using .muldiv() when calculating lp ratios

Low/Info · Three Sigma · Hybrid orderbook DEX · 3rd April 2024

Finding 3S-Vertex-L04 of the Vertex security review.

- Report: /reports/vertex
- Source: https://github.com/0xsimao/audits/blob/main/Three%20Sigma/2024-04-03-vertex.pdf

---

### Description

For example, in SpotEngineState::_getInLpBalance(), the [ratio](https://github.com/vertex-protocol/vertex-contracts-3sigma-audit/blob/main/contracts/SpotEngineState.sol#L338) is calculated first, likely
incurring in precision loss as lpState.supply is expected to be bigger than 1e18 (used in
.div()). This means that baseAmount and quoteAmount will be smaller due to the precision
loss.

### Recommendation

Everytime a division and multiplication are to be applied and division loss could occur (when
the denominator is bigger than 1e18), [mulDiv()](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/Math.sol#L128) should be used.
https://github.com/vertex-protocol/vertex-contracts-3sigma-audit/blob/main/contracts/SpotEngineLP.sol#L38
https://github.com/vertex-protocol/vertex-contracts-3sigma-audit/blob/main/contracts/PerpEngineState.sol#L115
https://github.com/vertex-protocol/vertex-contracts-3sigma-audit/blob/main/contracts/PerpEngine.sol#L174
https://github.com/vertex-protocol/vertex-contracts-3sigma-audit/blob/main/contracts/PerpEngine.sol#L177

### Status

Acknowledged

Disclosed by 0xSimao (https://0xsimao.com/).

---

Related findings:

- [WBTCZapper precision loss will lead to reverts](https://0xsimao.com/findings/evro-finance-ii-wbtc-zapper-precision-reverts): Evro Collateral Onboarding
- [Precision Loss in `notifyRewardAmount` Function Causes Unclaimable RewardToken](https://0xsimao.com/findings/exactly-protocol-update-staking-contract-ii-precision-notify-reward-unclaimable): Exactly Protocol Update - Staking Contract
- [Multiplication after division in StableEarnerRateModel leads to loss of precision](https://0xsimao.com/findings/m-0-stable-earner-model-precision): M^0 Minter Gateway
