Skip to content

fix(client): use exact fixed-point order arithmetic - #302

Merged
cesarenaldi merged 6 commits into
mainfrom
codex/dev-576
Aug 25, 2026
Merged

fix(client): use exact fixed-point order arithmetic#302
cesarenaldi merged 6 commits into
mainfrom
codex/dev-576

Conversation

@cesarenaldi

@cesarenaldi cesarenaldi commented Aug 20, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • accept valid numeric and numeric-string prices after bounded snapping of insignificant IEEE-754 drift, while continuing to reject materially off-grid values
  • validate price range and tick-grid membership with six-decimal fixed-point bigint arithmetic
  • migrate limit and market order amount calculations to exact fixed-point arithmetic with explicit directed rounding
  • add regression coverage for both order calculators, both sides, protection modes, and every supported tick size

Implementation plan

  • Add behavior-preserving characterization coverage for current order amount calculations
  • Introduce shared fixed-point primitives and migrate limit/market amount calculations
  • Add bounded price snapping and tick-grid regression coverage

Verification

  • pnpm test:client -- --run
  • pnpm lint
  • pnpm typecheck

Fee calculation and maxSpend resizing are intentionally deferred to stacked draft PR #306.

Fixes #297

Linear: DEV-576


Note

Medium Risk
Changes how order prices and maker/taker amounts are computed before submission; incorrect rounding could cause rejections or wrong fills, though behavior is heavily regression-tested.

Overview
Replaces float-based order price validation and amount encoding with six-decimal fixed-point bigint math so limit and market orders match the exchange tick grid without IEEE-754 rounding surprises.

Price handling: validatePriceOnTickGrid now normalizes via toScaledPrice / toScaledAmount, tolerates insignificant drift within MAX_PRICE_DRIFT, rejects excess precision with unsupported precision, and checks grid membership with integer modulo. It returns a ScaledPrice instead of a raw number. Limit/market prep keep scaled prices internally and only convert back to number where fee helpers still expect floats.

Amount encoding: Limit and market offered/requested amounts move into shared computeLimitOrderAmounts / computeMarketOrderAmounts in amounts.ts, using mulDiv and quantize with explicit round-down vs round-up (including protectPrice on market buys). Public size/amount inputs are still quantized to two decimals before calculation. The old math.ts helpers (parseAmount, roundDown, decimalPlaces, etc.) are removed.

Tests: New coverage for fixed-point primitives, all supported tick sizes, both sides, protection modes, and arithmetic-noise prices (e.g. 0.4 + 0.2).

Reviewed by Cursor Bugbot for commit 75a0c6d. Bugbot is set up for automated code reviews on this repo. Configure here.

@cesarenaldi
cesarenaldi marked this pull request as ready for review August 24, 2026 09:46

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 625fc27. Configure here.

Comment thread packages/client/src/actions/orders/fixed.ts
@cesarenaldi
cesarenaldi merged commit ef14f6c into main Aug 25, 2026
8 checks passed
@cesarenaldi
cesarenaldi deleted the codex/dev-576 branch August 25, 2026 20:19
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.

[Bug]: validatePriceOnTickGrid false rejects valid prices computed via floating point arithmetic

1 participant