Conversation
Time Submission Status
|
WalkthroughThis PR introduces a new Changes
Sequence DiagramsequenceDiagram
participant Caller as User
participant Action as cancel_order Action
participant Market as Market Lookup
participant Order as Order Retrieval
participant Refund as Collateral/Holdings
participant DB as ob_positions DB
Caller->>Action: Call cancel_order(query_id, outcome, price)
Action->>Action: Validate caller address
Action->>Market: Validate market exists & not settled
alt Market Invalid or Settled
Action-->>Caller: Error (invalid/settled market)
end
Action->>Order: Retrieve order by query_id
alt Order Not Found
Action-->>Caller: Error (order not found)
end
alt price < 0 (Buy Order)
Action->>Refund: Calculate & unlock collateral
Refund->>DB: Release locked funds
else price > 0 (Sell Order)
Action->>Refund: Update holdings (return shares)
Refund->>DB: Upsert shares back to holdings
else price == 0
Action-->>Caller: Error (cannot cancel holdings)
end
Action->>DB: Delete cancelled order from ob_positions
Action-->>Caller: Cancellation complete
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~30 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
internal/migrations/032-order-book-actions.sql (1)
1-8: Update file header comment to include cancel_order action.The header lists
create_market,get_market_info, andlist_marketsbut omits the newly addedcancel_orderaction. Consider updating the documentation for discoverability./* * ORDER BOOK ACTIONS * * User-facing actions for the prediction market: * - create_market: Create a new prediction market * - get_market_info: Get market details by ID * - list_markets: List markets with optional filtering + * - cancel_order: Cancel an open buy or sell order */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
internal/migrations/032-order-book-actions.sql(1 hunks)tests/streams/order_book/cancel_order_test.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
tests/streams/order_book/cancel_order_test.go (2)
tests/streams/utils/runner.go (1)
RunSchemaTest(37-116)tests/streams/utils/utils.go (1)
GetTestOptionsWithCache(75-88)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: acceptance-test
🔇 Additional comments (13)
internal/migrations/032-order-book-actions.sql (1)
976-1116: LGTM! Well-structured cancel_order implementation.The action follows established patterns in the codebase:
- Validation order is correct (caller → parameters → market → participant → order)
- Collateral refund calculation matches the lock calculation in
place_buy_order- UPSERT pattern for returning shares is consistent with other actions
- Proper use of helper functions (
ob_get_participant_id,ob_unlock_collateral)tests/streams/order_book/cancel_order_test.go (12)
28-40: Good test coverage with clear TODO for settlement test.The test suite covers essential scenarios: success cases for buy/sell, multiple price levels, various error conditions, and balance/refund verification. The commented-out
testCancelOrderMarketSettledwith TODO is acceptable for now.
44-88: LGTM! Clean test structure with proper setup-action-verify pattern.
91-159: LGTM! Thorough verification of sell order cancellation and share return.
162-228: LGTM! Good test for selective cancellation across multiple price levels.
231-264: LGTM! Correctly tests the error case for non-existent orders.
307-322: LGTM! Test correctly validates market existence check ordering.The SQL action validates market existence before checking participant, so this test correctly expects "Market does not exist" error.
325-357: LGTM! Tests boundary conditions for price validation.
360-389: LGTM! Verifies holdings cannot be cancelled.
392-445: LGTM! Comprehensive balance verification for refund flow.
448-515: LGTM! Thorough verification of shares returning to holdings after sell order cancellation.
522-551: LGTM! Helper follows established patterns from other order book tests.
554-568: LGTM! Test utility correctly simulates settlement state.Using raw SQL
UPDATEis appropriate for test setup to simulate settlement without depending on the actual settlement action implementation.
resolves: https://github.com/truflation/website/issues/2919
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.