From f47f8cde9a71b94feb8f5eca62a915bd37b124f0 Mon Sep 17 00:00:00 2001 From: ilya Date: Tue, 16 Jul 2024 17:42:22 +0300 Subject: [PATCH] Single quote query --- crates/orderbook/src/database/fee_policies.rs | 27 ++++++++++++++++++- crates/orderbook/src/database/trades.rs | 18 ++----------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/crates/orderbook/src/database/fee_policies.rs b/crates/orderbook/src/database/fee_policies.rs index d784c6d8de..8b0dd88417 100644 --- a/crates/orderbook/src/database/fee_policies.rs +++ b/crates/orderbook/src/database/fee_policies.rs @@ -11,7 +11,6 @@ impl super::Postgres { pub async fn fee_policies( &self, keys_filter: &[(AuctionId, OrderUid)], - quotes: HashMap, ) -> anyhow::Result>> { let mut ex = self.pool.acquire().await?; @@ -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::>(); + + 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::>(); + timer.stop_and_record(); + fee_policies .into_iter() .map(|((auction_id, order_uid), policies)| { diff --git a/crates/orderbook/src/database/trades.rs b/crates/orderbook/src/database/trades.rs index ced6bf72de..2dbfc25d13 100644 --- a/crates/orderbook/src/database/trades.rs +++ b/crates/orderbook/src/database/trades.rs @@ -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] @@ -41,25 +41,11 @@ impl TradeRetrieving for Postgres { .await?; timer.stop_and_record(); - let order_uids = trades.iter().map(|t| t.order_uid).collect::>(); - 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::>(); - 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::>(); - 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()