Skip to content

Fix float rounding drift in order amount calculations - #72

Open
Cassxbt wants to merge 1 commit into
Polymarket:mainfrom
Cassxbt:fix-decimal-order-rounding
Open

Fix float rounding drift in order amount calculations#72
Cassxbt wants to merge 1 commit into
Polymarket:mainfrom
Cassxbt:fix-decimal-order-rounding

Conversation

@Cassxbt

@Cassxbt Cassxbt commented May 20, 2026

Copy link
Copy Markdown

Summary

  • Use Decimal-based quantization for order-builder rounding helpers.
  • Preserve cent-boundary sizes like 16.90, 33.30, and 66.60 when converting limit order amounts to token decimals.
  • Add focused buy/sell regression coverage for 0.01 tick-size order amounts.

Motivation

Float arithmetic can round valid cent-boundary order sizes down before token decimal conversion. For example, a 16.90 size at 0.30 price can produce 5,067,000 instead of 5,070,000 maker amount, creating invalid sub-cent order amounts.

The Decimal rounding uses ROUND_HALF_UP for normal rounding to match the positive-value behavior of the TypeScript v2 client's Math.round path.

Closes #68.

Tests

  • python -m pytest tests/order_builder/test_decimal_order_amounts.py tests/order_builder/test_helpers.py -q
  • python -m pytest -q
  • python -m black --check py_clob_client_v2/order_builder/helpers.py tests/order_builder/test_decimal_order_amounts.py
  • git diff --check

Note

Medium Risk
Touches core order amount rounding/conversion logic used to compute maker/taker amounts; subtle rounding behavior changes could affect order validity at certain tick sizes, but scope is contained and covered by new regression tests.

Overview
Switches order-builder rounding helpers from float/math-based rounding to Decimal.quantize with explicit ROUND_FLOOR/ROUND_CEILING/ROUND_HALF_UP, and updates to_token_decimals/decimal_places to avoid float drift when scaling to 6-decimal token units.

Adds regression tests ensuring limit buy/sell amount calculations at 0.01 tick size preserve cent-boundary sizes (e.g., 16.90 at 0.30) when converted to maker/taker token decimals.

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

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] Limit orders: round_down in get_order_amounts produces sub-cent amount, server rejects

1 participant