Skip to content

Fix quoting math precision loss and exactOut rounding - #137

Open
SovaSlava wants to merge 1 commit into
1inch:mainfrom
SovaSlava:fix/quoting-precision-and-rounding
Open

Fix quoting math precision loss and exactOut rounding#137
SovaSlava wants to merge 1 commit into
1inch:mainfrom
SovaSlava:fix/quoting-precision-and-rounding

Conversation

@SovaSlava

Copy link
Copy Markdown

Description

This PR fixes a precision loss bug in _quoteExactIn and corrects the rounding behavior in _quoteExactOut to enforce protocol solvency.

Previously, early division by BPS_BASE caused premature integer truncation during ExactIn quotes, leading to lower output amounts on swaps. Additionally, _quoteExactOut lacked explicit ceiling rounding on fractional input requirements. This fix unifies numerator/denominator scaling and implements native ceil division.

Key Changes

  • _quoteExactIn (Bugfix): Fixed intermediate precision loss by deferring BPS_BASE scaling to the final division step.
  • _quoteExactOut (Fix & Solvency): Implemented library-free ceiling division (if (numerator % denominator != 0) { amountIn++; }) to prevent zero-fee/1-sat liquidity extraction on micro-swaps.
  • Gas Optimization: Removed redundant intermediate division operations (DIV opcodes) and wrapped fee-multiplier math in unchecked blocks.

Impact

  • Accuracy: Eliminates premature Floor-truncation before pool balance multiplication.
  • Solvency: Guarantees input amounts round up in favor of the pool for exact-output swaps.
  • Efficiency: Reduces execution gas overhead per quote.

Testing

  • Added unit test cases for micro-swaps, high fees, zero amounts, and large uint256 balances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant