Skip to content

Add v1/orders POST endpoint to get orders in batches#4048

Draft
m-sz wants to merge 18 commits intomainfrom
get-orders-by-uid
Draft

Add v1/orders POST endpoint to get orders in batches#4048
m-sz wants to merge 18 commits intomainfrom
get-orders-by-uid

Conversation

@m-sz
Copy link
Contributor

@m-sz m-sz commented Jan 13, 2026

Description

Aave wants to track specific orders in bulk, knowing their ids.

Changes

Adds POST handler for v1/orders/lookup endpoint that requires a list of order uids and responds with a vector of their data. Has a hardcoded limit of 5000 orders per request.

How to test

Test on staging, query multiple orders using this API.

@m-sz m-sz marked this pull request as ready for review January 23, 2026 14:10
@m-sz m-sz requested a review from a team as a code owner January 23, 2026 14:10
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new endpoint v1/get_orders to retrieve orders in batches using a POST request with a limit of 5000 orders per request. The code adds a new module get_orders_by_uid.rs, modifies api.rs to include the new route, and updates database/orders.rs and orderbook.rs to support fetching multiple orders. I found a missing test case for the MAX_ORDERS_LIMIT validation.

@m-sz
Copy link
Contributor Author

m-sz commented Jan 23, 2026

Tested on sepolia staging:

curl -d "[\"0x4f30bff027049ef2d2f8c8ba17be5e2e452c8fc9ad48273ef44d48c0e210bbc16406ce57254d45d503c7cb3e729aab9ddbbb3ec269738a51\", \"0x4f30bff027049ef2d2f8c8ba17be5e2e452c8fc9ad48273ef44d48c0e210bbc16406ce57254d45d503c7cb3e729aab9ddbbb3ec269738a51\"]" "https://barn.api.cow.fi/sepolia/api/v1/orders/lookup" -H "Content-Type: application/json"

(queries twice for the same order)

response

[
    {
        "creationDate": "2026-01-23T14:18:53.912159Z",
        "owner": "0x6406ce57254d45d503c7cb3e729aab9ddbbb3ec2",
        "uid": "0x4f30bff027049ef2d2f8c8ba17be5e2e452c8fc9ad48273ef44d48c0e210bbc16406ce57254d45d503c7cb3e729aab9ddbbb3ec269738a51",
        "availableBalance": null,
        "executedBuyAmount": "3931710370705679403",
        "executedSellAmount": "4000000000000000000",
        "executedSellAmountBeforeFees": "4000000000000000000",
        "executedFeeAmount": "0",
        "executedFee": "68289629294320597",
        "executedFeeToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "invalidated": false,
        "status": "fulfilled",
        "class": "limit",
        "settlementContract": "0x9008d19f58aabd9ed0d60971565aa8510560ab41",
        "isLiquidityOrder": false,
        "fullAppData": "{\"appCode\":\"CoW Swap\",\"environment\":\"barn\",\"metadata\":{\"orderClass\":{\"orderClass\":\"market\"},\"quote\":{\"slippageBips\":101,\"smartSlippage\":true}},\"version\":\"1.12.0\"}",
        "quote": {
            "gasAmount": "98318.00000000000",
            "gasPrice": "3575917434.000000",
            "sellTokenPrice": "0.008587696541803472",
            "sellAmount": "4000000000000000000",
            "buyAmount": "4000000000000000000",
            "feeAmount": "40939622000450720",
            "solver": "0x99b4136666ca1d13020830350ca8d01a0e5e466b",
            "verified": true,
            "metadata": {
                "interactions": [],
                "jitOrders": [],
                "preInteractions": [],
                "version": "1.0"
            }
        },
        "sellToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "buyToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "receiver": "0x6406ce57254d45d503c7cb3e729aab9ddbbb3ec2",
        "sellAmount": "4000000000000000000",
        "buyAmount": "3919073868181753833",
        "validTo": 1769179729,
        "appData": "0x8a8e8bdd353e10a8fb1aabb9594537fba2f1cce8fc655fdeaaa568286b3b53af",
        "feeAmount": "0",
        "kind": "sell",
        "partiallyFillable": false,
        "sellTokenBalance": "erc20",
        "buyTokenBalance": "erc20",
        "signingScheme": "eip712",
        "signature": "0x6cc9e9e2e8826cffb092a8531b103bbc3e70472e6455185af20245d1be14748e13a440f8e19e88db1a47320cff5d7b4a5ac32f07c76f708f94531616f597e8411b",
        "interactions": {
            "pre": [],
            "post": []
        }
    },
    {
        "creationDate": "2026-01-23T14:18:53.912159Z",
        "owner": "0x6406ce57254d45d503c7cb3e729aab9ddbbb3ec2",
        "uid": "0x4f30bff027049ef2d2f8c8ba17be5e2e452c8fc9ad48273ef44d48c0e210bbc16406ce57254d45d503c7cb3e729aab9ddbbb3ec269738a51",
        "availableBalance": null,
        "executedBuyAmount": "3931710370705679403",
        "executedSellAmount": "4000000000000000000",
        "executedSellAmountBeforeFees": "4000000000000000000",
        "executedFeeAmount": "0",
        "executedFee": "68289629294320597",
        "executedFeeToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "invalidated": false,
        "status": "fulfilled",
        "class": "limit",
        "settlementContract": "0x9008d19f58aabd9ed0d60971565aa8510560ab41",
        "isLiquidityOrder": false,
        "fullAppData": "{\"appCode\":\"CoW Swap\",\"environment\":\"barn\",\"metadata\":{\"orderClass\":{\"orderClass\":\"market\"},\"quote\":{\"slippageBips\":101,\"smartSlippage\":true}},\"version\":\"1.12.0\"}",
        "quote": {
            "gasAmount": "98318.00000000000",
            "gasPrice": "3575917434.000000",
            "sellTokenPrice": "0.008587696541803472",
            "sellAmount": "4000000000000000000",
            "buyAmount": "4000000000000000000",
            "feeAmount": "40939622000450720",
            "solver": "0x99b4136666ca1d13020830350ca8d01a0e5e466b",
            "verified": true,
            "metadata": {
                "interactions": [],
                "jitOrders": [],
                "preInteractions": [],
                "version": "1.0"
            }
        },
        "sellToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "buyToken": "0xbe72e441bf55620febc26715db68d3494213d8cb",
        "receiver": "0x6406ce57254d45d503c7cb3e729aab9ddbbb3ec2",
        "sellAmount": "4000000000000000000",
        "buyAmount": "3919073868181753833",
        "validTo": 1769179729,
        "appData": "0x8a8e8bdd353e10a8fb1aabb9594537fba2f1cce8fc655fdeaaa568286b3b53af",
        "feeAmount": "0",
        "kind": "sell",
        "partiallyFillable": false,
        "sellTokenBalance": "erc20",
        "buyTokenBalance": "erc20",
        "signingScheme": "eip712",
        "signature": "0x6cc9e9e2e8826cffb092a8531b103bbc3e70472e6455185af20245d1be14748e13a440f8e19e88db1a47320cff5d7b4a5ac32f07c76f708f94531616f597e8411b",
        "interactions": {
            "pre": [],
            "post": []
        }
    }
]

@m-sz
Copy link
Contributor Author

m-sz commented Jan 23, 2026

By the way, we can have a discussion on what the endpoint itself should be.
This has to be a POST request since we need to provide JSON array of order ids and /v1/orders obviously creates an order so there has to be a different name

I got a couple suggestions from Claude

  1. Nested under a "batch" or "bulk" path
    POST /v1/batch/orders
    POST /v1/bulk/orders
    Groups batch operations separately from the main resource routes.
  2. Different verb-like suffix
    POST /v1/orders/fetch
    POST /v1/orders/get
    POST /v1/orders/list
    Less common but unambiguous.
  3. Underscore-prefixed action
    POST /v1/orders/_search
    POST /v1/orders/_mget
    Elasticsearch uses this convention. The underscore signals "this is a special action, not a resource."
  4. Hyphenated compound resource
    POST /v1/order-lookups
    POST /v1/order-queries
    Treats the bulk lookup as its own resource type.
  5. RPC-style separate namespace
    POST /v1/rpc/get-orders
    POST /v1/actions/get-orders

based on the above suggestions I have opted for a custom not-order-id-like name /v1/orders/lookup - to lookup many orders, but I am open for discussion and don't have a strong opinion.

async move {
Result::<_, Infallible>::Ok(match request_result {
Ok(uids) => {
let result = orderbook.get_orders(&uids).await;
Copy link
Contributor

Choose a reason for hiding this comment

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

This will accumulate all the orders in memory, which is probably not a good idea. Can we instead return a stream of data directly from the DB?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The stream is problematic to implement since an order can be either a regular one or a Jit order. It is doable though although I am not sure if this its this PR.

Additionally, if keeping the X amount of orders is too much, we can lower the limit I've beset (5K), to a more acceptable level.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you elaborate? sqlx::fetch already returns a stream, no? Then, axum::Body::from_stream() should send a stream of data back to the client.

@m-sz m-sz marked this pull request as draft January 26, 2026 12:25
@m-sz m-sz marked this pull request as ready for review February 5, 2026 13:58
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The implementation adds a new endpoint to fetch orders in batches. A critical issue was found in crates/orderbook/src/database/orders.rs where await is used on a stream, which will cause a compilation error. The implementation also inefficiently collects two full vectors into memory; a suggestion is provided to fix the bug and improve performance by chaining the streams.

Comment on lines 328 to 346
let orders: Vec<Result<Order>> =
orders::many_full_orders_with_quotes(&mut ex, uids.as_slice())
.await
.filter_map(async |order| order.ok())
.map(|order| {
let (order, quote) = order.into_order_and_quote();
full_order_with_quote_into_model_order(order, quote.as_ref())
})
.collect()
.await;
let jit_orders: Vec<Result<Order>> =
database::jit_orders::get_many_by_id(&mut ex, uids.as_slice())
.await
.filter_map(async |order| order.ok())
.map(|order| full_order_into_model_order(order))
.collect()
.await;

orders.into_iter().chain(jit_orders).collect()
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The .await calls on lines 330 and 340 are incorrect as many_full_orders_with_quotes and get_many_by_id return a BoxStream, not a Future. This will cause a compilation error.

Additionally, the current implementation is inefficient as it collects all results from two separate database queries into two Vecs in memory before merging them. For a large number of UIDs, this can lead to high memory usage.

This can be fixed by removing the erroneous .await calls and refactoring to chain the streams together before collecting, which is more memory-efficient.

Suggested change
let orders: Vec<Result<Order>> =
orders::many_full_orders_with_quotes(&mut ex, uids.as_slice())
.await
.filter_map(async |order| order.ok())
.map(|order| {
let (order, quote) = order.into_order_and_quote();
full_order_with_quote_into_model_order(order, quote.as_ref())
})
.collect()
.await;
let jit_orders: Vec<Result<Order>> =
database::jit_orders::get_many_by_id(&mut ex, uids.as_slice())
.await
.filter_map(async |order| order.ok())
.map(|order| full_order_into_model_order(order))
.collect()
.await;
orders.into_iter().chain(jit_orders).collect()
let orders = orders::many_full_orders_with_quotes(&mut ex, uids.as_slice())
.filter_map(|order| async { order.ok() })
.map(|order| {
let (order, quote) = order.into_order_and_quote();
full_order_with_quote_into_model_order(order, quote.as_ref())
});
let jit_orders = database::jit_orders::get_many_by_id(&mut ex, uids.as_slice())
.filter_map(|order| async { order.ok() })
.map(|order| full_order_into_model_order(order));
orders.chain(jit_orders).try_collect().await

@m-sz m-sz marked this pull request as draft February 5, 2026 14:05
#[tokio::test]
async fn get_orders_by_uid_request_too_many_orders() {
let mut uids = Vec::new();
for _ in 0..5001 {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use the MAX_ORDERS_LIMIT const so the test doesn't break if/when we change the limit

Copy link
Contributor

@fafk fafk left a comment

Choose a reason for hiding this comment

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

LGTM. The endpoint should be documented in openapi.yml too I think.

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.

3 participants