Skip to content

Commit

Permalink
Use GET Request For Driver Quotes (#1835)
Browse files Browse the repository at this point in the history
This PR changes the driver's `quote` endpoint to be an HTTP `GET`
instead of `POST`.

The rationale behind this change is that `GET` requests are more cache
friendly, and we intend on caching `driver` quote requests in our
infrastructure.

That being said - I did notice that the `deadline` parameter is included
in the query string and, therefore, might affect the cache. I am unsure
if our caching configuration in the infrastructure allows "skipping"
query parameters for determining cache hits. If not we can:
- Move the `deadline` to be an HTTP header
- Make `deadline` optional and have a default value in the `driver`
- Other ideas I haven't thought of...

Note that

### Test Plan

Unit tests should continue to pass. You can also manually run the driver
locally and tests a quote request:

```
% curl -s "http://localhost:11088/mybologna/quote?sellToken=0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2&buyToken=0xDEf1CA1fb7FBcDC777520aa7f396b4E015F497aB&amount=1000000000000000000&kind=sell&deadline=2024-03-25T00:00:00.000Z" | jq

{
  "amount": "29273928646386956708",
  "interactions": [
    {
      "target": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      "value": "0",
      "callData": "0x095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000000000000000000"
    },
    {
      "target": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
      "value": "0",
      "callData": "0x095ea7b30000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488dffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
    },
    {
      "target": "0x7a250d5630b4cf539739df2c5dacb4c659f2488d",
      "value": "0",
      "callData": "0x8803dbee0000000000000000000000000000000000000000000000019641e3010406b9a40000000000000000000000000000000000000000000000000e043da61725000000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000009008d19f58aabd9ed0d60971565aa8510560ab41ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000def1ca1fb7fbcdc777520aa7f396b4e015f497ab"
    }
  ],
  "solver": "0x7e5f4552091a69125d5dfcb7b8c2659029395bdf"
}
```

The OpenAPI spec gets verified in CI.
  • Loading branch information
Nicholas Rodrigues Lordello authored Sep 6, 2023
1 parent df0f7b4 commit aabdf24
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
69 changes: 40 additions & 29 deletions crates/driver/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,42 @@ info:
version: 0.0.1
paths:
/quote:
post:
get:
description: Get price estimation quote.
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/QuoteRequest"
parameters:
- in: query
name: sellToken
description: The token to sell.
schema:
$ref: "#/components/schemas/Address"
required: true
- in: query
name: buyToken
description: The token to buy.
schema:
$ref: "#/components/schemas/Address"
required: true
- in: query
name: kind
description: |
- `buy`: amount is in buy_token, out_amount is in sell_token
- `sell`: amount is in sell_token, out_amount is in buy_token
schema:
type: string
enum: ["buy", "sell"]
required: true
- in: query
name: amount
description: The amount to buy or sell.
schema:
$ref: "#/components/schemas/TokenAmount"
required: true
- in: query
name: deadline
description: The time until which the caller expects a response.
schema:
$ref: "#/components/schemas/DateTime"
required: true
responses:
200:
description: Quote successfully created.
Expand Down Expand Up @@ -202,22 +230,6 @@ components:
and bytes 52..56 valid to,
type: string
example: "0x30cff40d9f60caa68a37f0ee73253ad6ad72b45580c945fe3ab67596476937197854163b1b0d24e77dca702b97b5cc33e0f83dcb626122a6"
QuoteRequest:
description: Description of what price to estimate.
type: object
properties:
sellToken:
$ref: "#/components/schemas/Address"
buyToken:
$ref: "#/components/schemas/Address"
kind:
description: |
- `buy`: amount is in buy_token, out_amount is in sell_token
- `sell`: amount is in sell_token, out_amount is in buy_token
type: string
enum: ["buy", "sell"]
amount:
$ref: "#/components/schemas/TokenAmount"
QuoteResponse:
oneOf:
- description: |
Expand All @@ -237,6 +249,10 @@ components:
items:
$ref: "#/components/schemas/Interaction"
- $ref: "#/components/schemas/Error"
DateTime:
description: An ISO 8601 UTC date time string.
type: string
example: "2020-12-03T18:35:18.814523Z"
SolveRequest:
description: Request to the solve endpoint.
type: object
Expand All @@ -262,12 +278,7 @@ components:
description: |
Information about tokens used in the auction.
deadline:
description: |
The time until which the caller expects a response.
Encoded as ISO 8601 UTC.
type: string
example: "2020-12-03T18:35:18.814523Z"
$ref: "#/components/schemas/DateTime"
SolveResponse:
description: |
Response of the solve endpoint.
Expand Down
4 changes: 2 additions & 2 deletions crates/driver/src/infra/api/routes/quote/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ mod dto;
pub use dto::OrderError;

pub(in crate::infra::api) fn quote(router: axum::Router<State>) -> axum::Router<State> {
router.route("/quote", axum::routing::post(route))
router.route("/quote", axum::routing::get(route))
}

async fn route(
state: axum::extract::State<State>,
order: axum::Json<dto::Order>,
order: axum::extract::Query<dto::Order>,
) -> Result<axum::Json<dto::Quote>, (hyper::StatusCode, axum::Json<Error>)> {
let handle_request = async {
let order = order.0.into_domain().tap_err(|err| {
Expand Down
4 changes: 2 additions & 2 deletions crates/driver/src/tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -679,12 +679,12 @@ impl Test {

let res = self
.client
.post(format!(
.get(format!(
"http://{}/{}/quote",
self.driver.addr,
solver::NAME
))
.json(&driver::quote_req(self))
.query(&driver::quote_req(self))
.send()
.await
.unwrap();
Expand Down
9 changes: 4 additions & 5 deletions crates/shared/src/trade_finding/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
request_sharing::RequestSharing,
trade_finding::{Interaction, Quote, Trade, TradeError, TradeFinding},
},
anyhow::{anyhow, Context},
anyhow::anyhow,
futures::{future::BoxFuture, FutureExt},
reqwest::{header, Client},
url::Url,
Expand Down Expand Up @@ -47,13 +47,12 @@ impl ExternalTradeFinder {
deadline,
};

let body = serde_json::to_string(&order).context("failed to encode body")?;
let mut request = self
.client
.post(self.quote_endpoint.clone())
.get(self.quote_endpoint.clone())
.query(&order)
.header(header::CONTENT_TYPE, "application/json")
.header(header::ACCEPT, "application/json")
.body(body);
.header(header::ACCEPT, "application/json");

if let Some(id) = observe::request_id::get_task_local_storage() {
request = request.header("X-REQUEST-ID", id);
Expand Down

0 comments on commit aabdf24

Please sign in to comment.