Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/autopilot/src/database/auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ impl QuoteStoring for Postgres {
.map(|quote| Ok((quote.id, quote.try_into()?)))
.transpose()
}

async fn get_next_auction_id(&self) -> Result<i64> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

is there any risk of infinite recursion here? If the inside func gets renamed? (ie, the one in crates/orderbook/src/database/quotes.rs)
It works rn ofc, just a rust doubt 😅

self.get_next_auction_id().await
}
}

impl Postgres {
Expand Down
8 changes: 8 additions & 0 deletions crates/driver/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ paths:
schema:
type: boolean
required: false
- in: query
name: auctionId
description: |
auction that will be associated with this quote competition. Only
populated for fast path orders
schema:
type: number

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should it be a type: integer with format: int64? IIRC, number allows decimals.

required: false
responses:
"200":
description: Quote successfully created.
Expand Down
3 changes: 3 additions & 0 deletions crates/driver/src/domain/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ pub struct Order {
pub side: order::Side,
pub deadline: chrono::DateTime<chrono::Utc>,
pub enable_fast_path: bool,
/// auction associated with the given quote for faciliating
/// fast path execution.
pub auction_id: Option<i64>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I've added this in #4678 already. That's ready to merge, so this'll probably need to be rebased after

}

impl Order {
Expand Down
5 changes: 5 additions & 0 deletions crates/driver/src/infra/api/routes/quote/dto/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl Order {
},
deadline: self.deadline,
enable_fast_path: self.enable_fast_path,
auction_id: self.auction_id,
}
}
}
Expand All @@ -32,6 +33,10 @@ pub struct Order {
deadline: chrono::DateTime<chrono::Utc>,
#[serde(default)]
enable_fast_path: bool,
/// auction associated with the quote competition
/// for fast path quotes
#[serde(default)]
auction_id: Option<i64>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why didn't we have this as a u32 (i mean, rn it makes sense, just asking for when we first set auction_id, since it's a natural number always) 👀

}

#[derive(Debug, Deserialize)]
Expand Down
11 changes: 11 additions & 0 deletions crates/orderbook/src/database/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,15 @@ impl QuoteStoring for Postgres {
.map(|quote| Ok((quote.id, quote.try_into()?)))
.transpose()
}

async fn get_next_auction_id(&self) -> Result<i64> {
let _timer = super::Metrics::get()
.database_queries
.with_label_values(&["get_next_auction_id"])
.start_timer();
let mut ex = self.pool.acquire().await?;
database::auction::get_next_auction_id(&mut ex)
.await
.context("failed to fetch next auction_id")
Comment on lines +63 to +65

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like the fast path id never collides with the full auction id, but we have other readers, probably like reward payouts, analytics, circuit breaker. I just wanted to ensure this won't cause any issues, since they assume this id is always a full auction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, the sequence would ensure that there are no duplicated auction ids. Will double check with them that this will not cause any issues. 👌

}
}
9 changes: 9 additions & 0 deletions crates/price-estimation/src/competition/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}),
Arc::new(Query {
verification: Default::default(),
Expand All @@ -288,6 +289,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}),
Arc::new(Query {
verification: Default::default(),
Expand All @@ -298,6 +300,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}),
Arc::new(Query {
verification: Default::default(),
Expand All @@ -308,6 +311,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}),
Arc::new(Query {
verification: Default::default(),
Expand All @@ -318,6 +322,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}),
];
let estimates = [
Expand Down Expand Up @@ -417,6 +422,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
});

fn estimate(amount: u64) -> Estimate {
Expand Down Expand Up @@ -480,6 +486,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
});

fn estimate(amount: u64) -> Estimate {
Expand Down Expand Up @@ -561,6 +568,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
});

fn estimate(amount: u64) -> Estimate {
Expand Down Expand Up @@ -631,6 +639,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/price-estimation/src/instrumented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
});

let mut estimator = MockPriceEstimating::new();
Expand Down
4 changes: 4 additions & 0 deletions crates/price-estimation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ pub struct Query {
#[serde(skip_serializing_if = "std::ops::Not::not")]
pub fast_path: bool,
pub timeout: Duration,
/// Only populated for fast path requests and used to later tell the
/// driver which solution to execute.
#[serde(skip_serializing_if = "Option::is_none")]
pub auction_id: Option<i64>,
}

/// Conditions under which a given price estimate needs to work in order to be
Expand Down
1 change: 1 addition & 0 deletions crates/price-estimation/src/native/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ impl NativePriceEstimator {
block_dependent: false,
fast_path: false,
timeout,
auction_id: None,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions crates/price-estimation/src/sanitized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -239,6 +240,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -262,6 +264,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Err(PriceEstimationError::ProtocolInternal(anyhow::anyhow!(
"cost of converting native asset would overflow gas price"
Expand All @@ -280,6 +283,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -304,6 +308,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -325,6 +330,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -346,6 +352,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -368,6 +375,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -390,6 +398,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Err(PriceEstimationError::UnsupportedToken {
token: BAD_TOKEN,
Expand All @@ -407,6 +416,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Err(PriceEstimationError::UnsupportedToken {
token: BAD_TOKEN,
Expand Down Expand Up @@ -535,6 +545,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand All @@ -555,6 +566,7 @@ mod tests {
block_dependent: false,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
},
Ok(Estimate {
out_amount: U256::ONE,
Expand Down
5 changes: 4 additions & 1 deletion crates/price-estimation/src/trade_finding/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl ExternalTradeFinder {
kind: query.kind,
deadline: chrono::Utc::now() + query.timeout,
fast_path: query.fast_path,
auction_id: query.auction_id,
};
let block_dependent = query.block_dependent;
let id = observe::tracing::distributed::request_id::from_current_span();
Expand Down Expand Up @@ -401,8 +402,10 @@ pub mod dto {
pub amount: U256,
pub kind: OrderKind,
pub deadline: chrono::DateTime<chrono::Utc>,
#[serde(default)]
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub fast_path: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub auction_id: Option<i64>,
}

#[serde_as]
Expand Down
35 changes: 31 additions & 4 deletions crates/shared/src/order_quoting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ impl QuoteParameters {
&self,
default_quote_timeout: std::time::Duration,
max_quote_timeout: std::time::Duration,
auction_id: Option<i64>,
) -> price_estimation::Query {
// TODO: refactor interfaces to make them impossible to misuse.
debug_assert_eq!(auction_id.is_some(), self.fast_path);

let (kind, in_amount) = self.side.kind_and_amount();

let timeout = self
Expand All @@ -69,6 +73,7 @@ impl QuoteParameters {
block_dependent: true,
fast_path: self.fast_path,
timeout,
auction_id,
}
}

Expand Down Expand Up @@ -371,6 +376,10 @@ pub trait QuoteStoring: Send + Sync {
parameters: QuoteSearchParameters,
expiration: DateTime<Utc>,
) -> Result<Option<(QuoteId, QuoteData)>>;

/// Generates a new unique auction id. This is used to associate a fast path
/// quote with auction competition data.
async fn get_next_auction_id(&self) -> Result<i64>;
}

#[cfg_attr(test, mockall::automock)]
Expand Down Expand Up @@ -462,6 +471,11 @@ impl OrderQuoter {
&self,
parameters: &QuoteParameters,
) -> Result<QuoteData, CalculateQuoteError> {
let auction_id = match parameters.fast_path {
Comment thread
AryanGodara marked this conversation as resolved.
true => Some(self.storage.get_next_auction_id().await?),
false => None,
};
Comment thread
MartinquaXD marked this conversation as resolved.

let expiration = match parameters.signing_scheme {
QuoteSigningScheme::Eip1271 {
onchain_order: true,
Expand All @@ -473,8 +487,11 @@ impl OrderQuoter {
_ => self.now.now() + self.validity.standard_quote,
};

let trade_query =
Arc::new(parameters.to_price_query(self.default_quote_timeout, self.max_quote_timeout));
let trade_query = Arc::new(parameters.to_price_query(
self.default_quote_timeout,
self.max_quote_timeout,
auction_id,
));
let (effective_gas_price, trade_estimate, sell_token_price, _) = futures::try_join!(
self.gas_estimator
.effective_gas_price()
Expand Down Expand Up @@ -634,9 +651,16 @@ impl StreamingQuoting for OrderQuoter {
let estimator = self.streaming_price_estimator.clone().ok_or_else(|| {
CalculateQuoteError::Other(anyhow::anyhow!("streaming estimator not configured"))
})?;
let auction_id = match parameters.fast_path {
true => Some(self.storage.get_next_auction_id().await?),
false => None,
};

let trade_query =
Arc::new(parameters.to_price_query(self.default_quote_timeout, self.max_quote_timeout));
let trade_query = Arc::new(parameters.to_price_query(
self.default_quote_timeout,
self.max_quote_timeout,
auction_id,
));

let (effective_gas_price, sell_token_price, _buy_token_price) = futures::try_join!(
self.gas_estimator
Expand Down Expand Up @@ -969,6 +993,7 @@ mod tests {
block_dependent: true,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}
})
.returning(|_| {
Expand Down Expand Up @@ -1117,6 +1142,7 @@ mod tests {
block_dependent: true,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}
})
.returning(|_| {
Expand Down Expand Up @@ -1260,6 +1286,7 @@ mod tests {
block_dependent: true,
fast_path: false,
timeout: HEALTHY_PRICE_ESTIMATION_TIME,
auction_id: None,
}
})
.returning(|_| {
Expand Down
Loading