fix(clob): enforce backend precision limits for market orders#165
fix(clob): enforce backend precision limits for market orders#165Haut wants to merge 5 commits intoPolymarket:mainfrom
Conversation
Market orders (FOK/FAK) were being rejected by the backend due to precision violations. The backend requires maker amounts to have max 2 decimal places and taker amounts to have max 4 decimal places. Changes: - Add RoundingStrategy enum (Down, HalfUp, Up) with Down as default - Add .rounding_strategy() builder method for OrderBuilder<Market, K> - Apply context-aware rounding based on order side: - BUY: maker=USDC(2 dec), taker=shares(4 dec) - SELL: maker=shares(2 dec), taker=USDC(4 dec) - Add validation to reject orders where amounts round to zero - Export RoundingStrategy from clob module Fixes Polymarket#114
|
@chaz-polymarket this fixes #114 for me. adds an optional builder method for setting rounding and matches the other python / ts clients from what I can see. tested live and seems to work for everything I tried. not sure if you want to get in before 0.4 but its here if needed |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #165 +/- ##
==========================================
+ Coverage 85.81% 85.96% +0.14%
==========================================
Files 32 32
Lines 4885 4965 +80
==========================================
+ Hits 4192 4268 +76
- Misses 693 697 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Is it possible that this affects limit orders too? If I try to sell an amount of shares with a limit oder, I get "not available funds". If I subtract 0.01 from the amount, the sell order goes though. If this is another issue, I will gladly open a new ticket. |
Fixes #114
The existing implementation in
OrderBuilder<Market, K>::build()calculated maker/taker amounts using:Where
decimalsis the tick size scale (1-4) andLOT_SIZE_SCALEis 2. This produces amounts with 3-6 decimal places depending on tick size, violating the backend's strict precision requirements:Changes Made
Added
RoundingStrategyenum with three variants:Down(default) - Truncate towards zero (safest to avoid overspending)HalfUp- Standard rounding (round 0.5 away from zero)Up- Round away from zeroAdded
.rounding_strategy()builder method onOrderBuilder<Market, K>allowing users to configure rounding behavior per-orderImplemented context-aware rounding that respects the backend's precision matrix:
Added zero-amount validation - Returns
Error::validation()when rounding produces zero amounts (e.g., trying to sell 0.001 shares when limit is 0.01)Exported
RoundingStrategyfrom theclobmodule for user accessTests