Skip to content

Conversation

@findolor
Copy link
Collaborator

@findolor findolor commented Jan 5, 2026

Chained PRs

Motivation

See issues:

This is the V6 implementation of the take orders SDK, replacing the V5 version from #2360.

The V6 interface (from #2366) provides better exact swap support with minimumIO, maximumIO, and maximumIORatio parameters, enabling four distinct trading modes: buy exact, buy up to, spend exact, and spend up to.

Solution

SDK Layer (crates/common/src/take_orders/)

Modular SDK for take orders simulation and config building:

  • candidates.rs - Build TakeOrderCandidate from orders with concurrent quote fetching
  • simulation.rs - Simulate buy/spend modes over candidates with price-cap filtering
  • config.rs - TakeOrdersMode enum and TakeOrdersConfigV5 building
  • price.rs - Float comparison utilities

API Layer (crates/common/src/raindex_client/take_orders/)

Public API for generating takeOrders4 calldata:

  • get_take_orders_calldata - Main entry point that fetches orders, simulates, and returns calldata
  • request.rs - Input parsing and validation
  • selection.rs - Cross-orderbook simulation and best-yield selection
  • result.rs - Calldata result with pricing info (effective price, per-leg prices, max sell cap)

Key Features

  • Four trading modes: buyExact, buyUpTo, spendExact, spendUpTo
  • Cross-orderbook optimization: Selects the orderbook with best yield
  • Price cap enforcement: Filters orders exceeding the specified price limit
  • Comprehensive testing: 111 tests covering unit and e2e scenarios

Checks

By submitting this for review, I'm confirming I've done the following:

  • made this PR as small as possible
  • unit-tested any new functionality
  • linked any relevant issues or PRs
  • included screenshots (if this involves a front-end change)

- Add PRAGMA quick_check query to detect database corruption at startup
- Add check_integrity() method to BootstrapPipeline trait
- Reset database automatically when corruption is detected
- Handle both cases: non-"ok" response and query errors (severe corruption)
- Add IntegrityCheckRow struct to parse JSON response from quick_check
- split monolithic take_orders.rs into focused modules
- candidates.rs: TakeOrderCandidate building from orders
- simulation.rs: buy/spend simulation over candidates
- config.rs: TakeOrdersMode enum and config building
- price.rs: Float comparison utilities
- make_candidate: creates TakeOrderCandidate for unit tests
- make_simulation_candidate: creates candidate for simulation tests
- get_take_orders_calldata: public API for generating takeOrders4 calldata
- request.rs: input parsing and validation
- selection.rs: cross-orderbook simulation and selection
- result.rs: calldata result construction with pricing info
- comprehensive e2e tests with mock subgraph/rpc
@findolor findolor self-assigned this Jan 5, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 5, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


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 and usage tips.

Improves readability of INPUT_TOKENS_CLAUSE_BODY and OUTPUT_TOKENS_CLAUSE_BODY
by converting from single-line strings with embedded \n to proper multi-line
string literals.
…med-fix

Add database integrity check to detect and recover from corruption
…ified

When filtering orders by tokens with both inputs and outputs provided,
the query now uses OR semantics (token in inputs OR token in outputs)
instead of AND semantics. This fixes the UI behavior where selecting
a token should show all orders involving that token on either side.

- Local DB: Combined EXISTS clause with OR inside when both have tokens
- Subgraph: Two separate filter objects in the OR array
Verify that input token params come before output token params
in the combined OR clause construction.
When inputs and outputs lists are identical, use OR logic for "any-IO"
filtering (UI use case). When they differ, use separate EXISTS clauses
with AND logic to support directional filtering (SDK use case).
Use buffered() instead of buffer_unordered() to preserve input order
when zipping orders with their quote results.
Brings in token filtering changes and database integrity check.
The race condition fix (buffer_unordered -> buffered) was already
present in the refactored SDK module structure.
@findolor
Copy link
Collaborator Author

Code review

Found 1 issue:

  1. Inaccurate documentation for maxSellCap - doc says amount * priceCap but this formula only applies to buy modes. For spend modes, max_sell_cap is just amount (no multiplication by price cap).

Documentation states:

/// - `maxSellCap`: `amount * priceCap` (worst-case on-chain spend cap).

But implementation shows mode-dependent behavior:

let max_sell_cap = if mode.is_buy_mode() {
    mode.target_amount().mul(price_cap)?  // buy: amount * priceCap
} else {
    mode.target_amount()  // spend: just amount
};

/// - `expectedSell`: simulated sell at current quotes.
/// - `maxSellCap`: `amount * priceCap` (worst-case on-chain spend cap).
///

let expected_sell = built_config.sim.total_input;
let max_sell_cap = if mode.is_buy_mode() {
mode.target_amount().mul(price_cap)?
} else {
mode.target_amount()
};

🤖 Generated with Claude Code

- If this code review was useful, please react with 👍. Otherwise, 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.

2 participants