# MarketUtils.getFundingAmountPerSizeDelta() has a rounding logical error.

GMX MarketUtils.getFundingAmountPerSizeDelta used roundUpDivision for the divisor regardless of roundUp flag, causing rounding-down when round-up was requested and miscomputing funding for all users.

- Payout: Contest reward
- Severity: medium
- Program: Gmx
- Credited: chaduke
- Disclosed: 2023-04-20
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2023-04-gmx-judging/issues/164
- Library page: https://bugbountylibrary.com/r/sherlock%3A2023-04-gmx%3AM-2
- Tags: domain:web3 impact:integrity kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:precision-loss

**Target:** Solidity market math library (MarketUtils/Precision)
**Root cause:** The divisor is computed with Calc.roundUpDivision(openInterest, FLOAT_PRECISION_SQRT) unconditionally, so when roundUp=true the divisor rounds up and the final division effectively rounds down, inverting the intended rounding direction.
**Steps:**
1. Read MarketUtils.getFundingAmountPerSizeDelta at MarketUtils.sol#L1126-L1136: divisor = Calc.roundUpDivision(openInterest, Precision.FLOAT_PRECISION_SQRT); return Precision.toFactor(fundingAmount, divisor, roundUp).
2. Note divisor rounding is independent of the roundUp parameter.
3. Call getFundingAmountPerSizeDelta(2e15, 1e15+1, true) in a Foundry test.
4. Observe result == 1e15*1e30 while correct round-up is 1999999999999998000000000000001999999999999999.
5. Fix: return Precision.toFactor(fundingAmount*Precision.FLOAT_PRECISION_SQRT, openInterest, roundUp).
**How found:** Code review of the rounding-mode plumbing: noticed the divisor always used roundUpDivision regardless of the roundUp argument, then confirmed with a Foundry PoC comparing against a manually computed round-up value.
**Insight:** When a function takes a roundUp flag, audit every intermediate division/multiplication for whether its rounding direction must be opposite to the final one; a fixed-direction helper inside a parameterized-rounding function silently inverts results.
