Skip to content

fix(polymarket): publish final batched book state - #35

Merged
proerror77 merged 1 commit into
mainfrom
codex/fix-polymarket-crossed-batches
Jul 15, 2026
Merged

proerror77 merged 1 commit into
mainfrom
codex/fix-polymarket-crossed-batches

Conversation

@proerror77

@proerror77 proerror77 commented Jul 15, 2026

Copy link
Copy Markdown
Owner

Summary

  • apply every Polymarket price_change entry before publishing a quote
  • publish one final quote per token and reject a still-crossed final book so the feed reconnects from a fresh snapshot
  • add regression coverage for transient and final crossed batches

Validation

  • cargo test -p ploy-market-data --features live --locked feeds::tests::clob_ -- --nocapture
  • cargo test -p ploy-market-data --features live --locked
  • cargo fmt --all -- --check
  • git diff --check

Summary by CodeRabbit

  • Bug Fixes

    • Improved Polymarket price update processing to publish only the final state from each batch.
    • Prevented stale or invalid crossed order books from being published.
    • Added fallback handling when a fresh snapshot is available after an invalid update batch.
  • Documentation

    • Marked batch coalescing for Polymarket price changes as completed.

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 8a468313-2712-4f59-9f6d-e6e4b35f810d

📥 Commits

Reviewing files that changed from the base of the PR and between 9885261 and 846fee2.

📒 Files selected for processing (2)
  • products/ploy/crates/ploy-market-data/src/feeds.rs
  • products/ploy/tasks/todo.md

📝 Walkthrough

Walkthrough

Polymarket CLOB price-change batches now coalesce fresh deltas before publishing quotes, validate final books for crossed prices, and propagate errors. Tests cover final-state publication, crossed books, stale updates, and the updated Result API.

Changes

Polymarket batch coalescing

Layer / File(s) Summary
Batch processing and error propagation
products/ploy/crates/ploy-market-data/src/feeds.rs
Price-change batches apply fresh deltas to cached books, publish only final states for changed tokens, reject crossed books, and propagate errors through WebSocket handling.
Batch behavior validation and task tracking
products/ploy/crates/ploy-market-data/src/feeds.rs, products/ploy/tasks/todo.md
Tests cover seeded books, final-state publication, crossed batches, stale updates, and the updated Result API; the task list records batch coalescing.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant WebSocketHandler
  participant BatchProcessor
  participant CachedBook
  participant QuotePublisher
  WebSocketHandler->>BatchProcessor: Forward PriceChange batch
  BatchProcessor->>CachedBook: Apply fresh deltas
  CachedBook-->>BatchProcessor: Final changed book states
  BatchProcessor->>QuotePublisher: Publish validated quote updates
  BatchProcessor-->>WebSocketHandler: Return updates or crossed-book error
Loading
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: publishing only the final batched Polymarket book state.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-polymarket-crossed-batches

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@proerror77
proerror77 merged commit 5e7908f into main Jul 15, 2026
25 checks passed

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 846fee2060

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

}
books.entry(token_id.clone()).or_default().apply(entry);
last_timestamp.insert(token_id.clone(), change.timestamp);
if last_entries.insert(token_id.clone(), entry).is_none() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve both top-of-book sizes while coalescing batches

When a price_change batch arrives before a Book snapshot has initialized ClobBookState (or immediately after reconnect), apply leaves the book empty and quote can only recover sizes from the single entry passed here. Because last_entries.insert overwrites earlier entries for the same token, a batch that updates the bid and then the ask publishes only the last side's size; the previous per-entry flow let the executor observe and carry both sides. This leaves strategies/backtests with missing or stale liquidity until a full book snapshot arrives, so the coalescing needs to retain per-token top bid and ask size metadata, not just the last entry.

Useful? React with 👍 / 👎.

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