fix: tick size cache stale — force refresh on order create, better er…#282
Open
kingo233 wants to merge 5 commits intoPolymarket:mainfrom
Open
fix: tick size cache stale — force refresh on order create, better er…#282kingo233 wants to merge 5 commits intoPolymarket:mainfrom
kingo233 wants to merge 5 commits intoPolymarket:mainfrom
Conversation
So that except PolyApiException continues to catch all API errors including tick-size rejections; preserves status_code from wrapped api_exception. Co-authored-by: Cursor <cursoragent@cursor.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
- Add _flatten_error_message() to recursively extract string from
dict/list API error payloads (e.g. {"error": {"message": "..."}})
- Use flattened text in _is_tick_size_related_error() so keyword search
works for nested structures; use flattened message for TickSizeRejectedError
- Add tests for string, dict, nested dict, and list error payloads
Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix: tick size cache — force refresh on order create, clearer error on reject
Overview
This PR fixes the case where orders are rejected by the CLOB because the client used a stale tick size from its cache after the order book’s tick size changed on the server. It does two things: (1) always fetches the current tick size from the CLOB when creating/signing orders, and (2) raises a dedicated
TickSizeRejectedErrorwith a clear message when the API rejects an order due to tick size or price precision, so users know to clear the cache and retry.Description
Problem: The client caches each market’s minimum tick size (from
get_tick_size) for a TTL. If the CLOB updates a market’s tick size, the cache can still hold the old value. Users who rely onget_tick_size(or who signed orders earlier) may then submit orders with an outdated tick size and get rejected by the API, with no indication that the failure is due to cache vs. CLOB.Changes:
Force refresh when creating orders:
create_orderandcreate_market_ordernow call__resolve_tick_size(..., force_refresh=True), so the tick size used for signing is always fetched from the CLOB and never taken from cache. This prevents rejections caused by stale tick size on the create/sign path.get_tick_size(token_id, force_refresh=False): A new optional argumentforce_refreshskips the cache and fetches from the API, then updates the cache. Docstring updated to explain that when the market’s tick size may have changed, callers should useforce_refresh=Trueor callclear_tick_size_cache(token_id)first.TickSizeRejectedError: Whenpost_orderorpost_ordersreceives an API error whose message suggests tick size or price precision (e.g. contains "tick", "precision", "minimum_tick"), the client raisesTickSizeRejectedErrorinstead of a genericPolyApiException. The message tells the user to clear the tick size cache and retry. The original API exception is preserved as__cause__.Package export:
TickSizeRejectedErroris exported frompy_clob_clientso callers can catch it and implement retry or user-facing messaging.Documentation: README now has a “Tick size and order rejection” section describing cache behavior, when to use
force_refresh/clear_tick_size_cache, and how to handleTickSizeRejectedError. Notes section references this for order-rejection issues.Testing instructions
pytest tests/ -v. If the environment hits theeth_typing/ContractNameimport error when loading pytest plugins, fix the environment (e.g.pip install 'eth_typing<4.2.0'orpip install -U web3) then re-run.create_order/create_market_orderstill succeed (tick size is refreshed). When the API rejects an order with a tick-size-related error, confirm thatTickSizeRejectedErroris raised and the message suggests clearing the cache and retrying.Types of changes
Notes
get_tick_sizeis unchanged (still uses cache). Only the order-creation path forces a refresh.__resolve_tick_sizestill use the cache unless they are updated to passforce_refresh=Truewhere appropriate.PolyApiException.Status
[WIP]if necessary (changes not yet made).Note
Medium Risk
Changes order creation and error-handling behavior on the trading path, which could affect how clients retry/handle failed submissions; logic is small and covered by focused tests.
Overview
Prevents stale tick-size cache from causing order rejections by forcing a fresh
get_tick_size(..., force_refresh=True)whencreate_order/create_market_orderresolve tick size for signing.Adds
get_tick_size(token_id, force_refresh=False)and introducesTickSizeRejectedError, raised frompost_order/post_orderswhen aPolyApiExceptionmessage looks tick/precision-related (with helpers to flatten/inspect structured error payloads); exports the exception frompy_clob_client, documents cache/refresh guidance inREADME.md, and adds unit tests for the error detection/flattening logic.Written by Cursor Bugbot for commit b077da8. This will update automatically on new commits. Configure here.