Skip to content

Commit

Permalink
Single quote query
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jul 16, 2024
1 parent e5a33c8 commit f47f8cd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
27 changes: 26 additions & 1 deletion crates/orderbook/src/database/fee_policies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ impl super::Postgres {
pub async fn fee_policies(
&self,
keys_filter: &[(AuctionId, OrderUid)],
quotes: HashMap<OrderUid, database::orders::Quote>,
) -> anyhow::Result<HashMap<(AuctionId, OrderUid), Vec<FeePolicy>>> {
let mut ex = self.pool.acquire().await?;

Expand All @@ -21,6 +20,32 @@ impl super::Postgres {
.start_timer();

let fee_policies = database::fee_policies::fetch(&mut ex, keys_filter).await?;
let quote_order_uids = fee_policies
.iter()
.filter_map(|((_, order_uid), policies)| {
policies
.iter()
.any(|policy| {
matches!(
policy.kind,
database::fee_policies::FeePolicyKind::PriceImprovement
)
})
.then_some(*order_uid)
})
.collect::<Vec<_>>();

let timer = super::Metrics::get()
.database_queries
.with_label_values(&["order_quotes"])
.start_timer();
let quotes = database::orders::read_quotes(&mut ex, quote_order_uids.as_slice())
.await?
.into_iter()
.map(|quote| (quote.order_uid, quote))
.collect::<HashMap<_, _>>();
timer.stop_and_record();

fee_policies
.into_iter()
.map(|((auction_id, order_uid), policies)| {
Expand Down
18 changes: 2 additions & 16 deletions crates/orderbook/src/database/trades.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use {
model::{fee_policy::FeePolicy, order::OrderUid, trade::Trade},
number::conversions::big_decimal_to_big_uint,
primitive_types::H256,
std::{collections::HashMap, convert::TryInto},
std::convert::TryInto,
};

#[async_trait::async_trait]
Expand Down Expand Up @@ -41,25 +41,11 @@ impl TradeRetrieving for Postgres {
.await?;
timer.stop_and_record();

let order_uids = trades.iter().map(|t| t.order_uid).collect::<Vec<_>>();
let timer = super::Metrics::get()
.database_queries
.with_label_values(&["order_quotes"])
.start_timer();
let quotes = database::orders::read_quotes(&mut ex, order_uids.as_slice())
.await?
.into_iter()
.map(|quote| (quote.order_uid, quote))
.collect::<HashMap<_, _>>();
timer.stop_and_record();

let auction_order_uids = trades
.iter()
.filter_map(|t| t.auction_id.map(|auction_id| (auction_id, t.order_uid)))
.collect::<Vec<_>>();
let fee_policies = self
.fee_policies(auction_order_uids.as_slice(), quotes)
.await?;
let fee_policies = self.fee_policies(auction_order_uids.as_slice()).await?;

trades
.into_iter()
Expand Down

0 comments on commit f47f8cd

Please sign in to comment.