Skip to content

Commit 514c2f0

Browse files
committed
Rework comments
1 parent 21a667b commit 514c2f0

File tree

3 files changed

+183
-71
lines changed

3 files changed

+183
-71
lines changed

crates/database/src/orders.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,18 @@ pub struct FullOrder {
489489
pub full_app_data: Option<Vec<u8>>,
490490
}
491491

492+
#[derive(Debug, sqlx::FromRow)]
493+
pub struct FullOrderWithQuote {
494+
#[sqlx(flatten)]
495+
pub full_order: FullOrder,
496+
pub quote_buy_amount: Option<BigDecimal>,
497+
pub quote_sell_amount: Option<BigDecimal>,
498+
pub quote_gas_amount: Option<f64>,
499+
pub quote_gas_price: Option<f64>,
500+
pub quote_sell_token_price: Option<f64>,
501+
pub solver: Option<Address>,
502+
}
503+
492504
impl FullOrder {
493505
pub fn valid_to(&self) -> i64 {
494506
if let Some((_, valid_to)) = self.ethflow_data {
@@ -567,6 +579,26 @@ pub async fn single_full_order(
567579
sqlx::query_as(QUERY).bind(uid).fetch_optional(ex).await
568580
}
569581

582+
pub async fn single_full_order_with_quote(
583+
ex: &mut PgConnection,
584+
uid: &OrderUid,
585+
) -> Result<Option<FullOrderWithQuote>, sqlx::Error> {
586+
#[rustfmt::skip]
587+
const QUERY: &str = const_format::concatcp!(
588+
"SELECT ", ORDERS_SELECT,
589+
", o_quotes.sell_amount as quote_sell_amount",
590+
", o_quotes.buy_amount as quote_buy_amount",
591+
", o_quotes.gas_amount as quote_gas_amount",
592+
", o_quotes.gas_price as quote_gas_price",
593+
", o_quotes.sell_token_price as quote_sell_token_price",
594+
", o_quotes.solver",
595+
" FROM ", ORDERS_FROM,
596+
" LEFT JOIN order_quotes o_quotes ON o.uid = o_quotes.order_uid",
597+
" WHERE o.uid = $1",
598+
);
599+
sqlx::query_as(QUERY).bind(uid).fetch_optional(ex).await
600+
}
601+
570602
// Partial query for getting the log indices of events of a single settlement.
571603
//
572604
// This will fail if we ever have multiple settlements in the same transaction
@@ -730,7 +762,6 @@ pub struct OrderWithQuote {
730762

731763
pub async fn user_orders_with_quote(
732764
ex: &mut PgConnection,
733-
min_valid_to: i64,
734765
owner: &Address,
735766
) -> Result<Vec<OrderWithQuote>, sqlx::Error> {
736767
#[rustfmt::skip]
@@ -749,7 +780,6 @@ pub async fn user_orders_with_quote(
749780
" INNER JOIN order_quotes o_quotes ON o.uid = o_quotes.order_uid"
750781
);
751782
sqlx::query_as::<_, OrderWithQuote>(QUERY)
752-
.bind(min_valid_to)
753783
.bind(owner)
754784
.fetch_all(ex)
755785
.await

crates/orderbook/src/database/orders.rs

+108-35
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use {
77
database::{
88
byte_array::ByteArray,
99
order_events::{insert_order_event, OrderEvent, OrderEventLabel},
10-
orders::{FullOrder, OrderKind as DbOrderKind},
10+
orders::{self, FullOrder, OrderKind as DbOrderKind},
1111
},
1212
ethcontract::H256,
1313
futures::{stream::TryStreamExt, FutureExt, StreamExt},
@@ -24,7 +24,6 @@ use {
2424
OrderUid,
2525
},
2626
signature::Signature,
27-
time::now_in_epoch_seconds,
2827
},
2928
num::Zero,
3029
number::conversions::{big_decimal_to_big_uint, big_decimal_to_u256, u256_to_big_decimal},
@@ -49,7 +48,7 @@ use {
4948
order_validation::{is_order_outside_market_price, Amounts, LimitOrderCounting},
5049
},
5150
sqlx::{types::BigDecimal, Connection, PgConnection},
52-
std::convert::TryInto,
51+
std::{convert::TryInto, ops::Deref},
5352
};
5453

5554
#[cfg_attr(test, mockall::automock)]
@@ -76,13 +75,44 @@ pub trait OrderStoring: Send + Sync {
7675
limit: Option<u64>,
7776
) -> Result<Vec<Order>>;
7877
async fn latest_order_event(&self, order_uid: &OrderUid) -> Result<Option<OrderEvent>>;
78+
async fn single_order_with_quote(&self, uid: &OrderUid) -> Result<Option<OrderWithQuote>>;
7979
}
8080

8181
pub struct SolvableOrders {
8282
pub orders: Vec<Order>,
8383
pub latest_settlement_block: u64,
8484
}
8585

86+
pub struct OrderWithQuote {
87+
pub order: Order,
88+
pub quote: Option<orders::Quote>,
89+
}
90+
91+
impl OrderWithQuote {
92+
pub fn new(order: Order, quote: Option<Quote>) -> Self {
93+
Self {
94+
quote: quote.map(|quote| orders::Quote {
95+
order_uid: ByteArray(order.metadata.uid.0),
96+
gas_amount: quote.data.fee_parameters.gas_amount,
97+
gas_price: quote.data.fee_parameters.gas_price,
98+
sell_token_price: quote.data.fee_parameters.sell_token_price,
99+
sell_amount: u256_to_big_decimal(&quote.sell_amount),
100+
buy_amount: u256_to_big_decimal(&quote.buy_amount),
101+
solver: ByteArray(quote.data.solver.0),
102+
}),
103+
order,
104+
}
105+
}
106+
}
107+
108+
impl Deref for OrderWithQuote {
109+
type Target = Order;
110+
111+
fn deref(&self) -> &Self::Target {
112+
&self.order
113+
}
114+
}
115+
86116
#[derive(Debug)]
87117
pub enum InsertionError {
88118
DuplicatedRecord,
@@ -316,6 +346,51 @@ impl OrderStoring for Postgres {
316346
order.map(full_order_into_model_order).transpose()
317347
}
318348

349+
async fn single_order_with_quote(&self, uid: &OrderUid) -> Result<Option<OrderWithQuote>> {
350+
let _timer = super::Metrics::get()
351+
.database_queries
352+
.with_label_values(&["single_order_with_quote"])
353+
.start_timer();
354+
355+
let mut ex = self.pool.acquire().await?;
356+
let order = orders::single_full_order_with_quote(&mut ex, &ByteArray(uid.0)).await?;
357+
order
358+
.map(|order_with_quote| {
359+
let quote = order_with_quote
360+
.quote_buy_amount
361+
.zip(order_with_quote.quote_sell_amount)
362+
.zip(order_with_quote.quote_gas_amount)
363+
.zip(order_with_quote.quote_gas_price)
364+
.zip(order_with_quote.quote_sell_token_price)
365+
.zip(order_with_quote.solver)
366+
.map(
367+
|(
368+
(
369+
(((buy_amount, sell_amount), gas_amount), gas_price),
370+
sell_token_price,
371+
),
372+
solver,
373+
)| {
374+
orders::Quote {
375+
order_uid: order_with_quote.full_order.uid,
376+
gas_amount,
377+
gas_price,
378+
sell_token_price,
379+
sell_amount,
380+
buy_amount,
381+
solver,
382+
}
383+
},
384+
);
385+
386+
Ok(OrderWithQuote {
387+
order: full_order_into_model_order(order_with_quote.full_order)?,
388+
quote,
389+
})
390+
})
391+
.transpose()
392+
}
393+
319394
async fn orders_for_tx(&self, tx_hash: &H256) -> Result<Vec<Order>> {
320395
let _timer = super::Metrics::get()
321396
.database_queries
@@ -380,39 +455,37 @@ impl LimitOrderCounting for Postgres {
380455
.start_timer();
381456

382457
let mut ex = self.pool.acquire().await?;
383-
Ok(database::orders::user_orders_with_quote(
384-
&mut ex,
385-
now_in_epoch_seconds().into(),
386-
&ByteArray(owner.0),
458+
Ok(
459+
database::orders::user_orders_with_quote(&mut ex, &ByteArray(owner.0))
460+
.await?
461+
.into_iter()
462+
.filter(|order_with_quote| {
463+
is_order_outside_market_price(
464+
&Amounts {
465+
sell: big_decimal_to_u256(&order_with_quote.order_sell_amount).unwrap(),
466+
buy: big_decimal_to_u256(&order_with_quote.order_buy_amount).unwrap(),
467+
fee: 0.into(),
468+
},
469+
&Amounts {
470+
sell: big_decimal_to_u256(&order_with_quote.quote_sell_amount).unwrap(),
471+
buy: big_decimal_to_u256(&order_with_quote.quote_buy_amount).unwrap(),
472+
fee: FeeParameters {
473+
gas_amount: order_with_quote.quote_gas_amount,
474+
gas_price: order_with_quote.quote_gas_price,
475+
sell_token_price: order_with_quote.quote_sell_token_price,
476+
}
477+
.fee(),
478+
},
479+
match order_with_quote.order_kind {
480+
DbOrderKind::Buy => model::order::OrderKind::Buy,
481+
DbOrderKind::Sell => model::order::OrderKind::Sell,
482+
},
483+
)
484+
})
485+
.count()
486+
.try_into()
487+
.unwrap(),
387488
)
388-
.await?
389-
.into_iter()
390-
.filter(|order_with_quote| {
391-
is_order_outside_market_price(
392-
&Amounts {
393-
sell: big_decimal_to_u256(&order_with_quote.order_sell_amount).unwrap(),
394-
buy: big_decimal_to_u256(&order_with_quote.order_buy_amount).unwrap(),
395-
fee: 0.into(),
396-
},
397-
&Amounts {
398-
sell: big_decimal_to_u256(&order_with_quote.quote_sell_amount).unwrap(),
399-
buy: big_decimal_to_u256(&order_with_quote.quote_buy_amount).unwrap(),
400-
fee: FeeParameters {
401-
gas_amount: order_with_quote.quote_gas_amount,
402-
gas_price: order_with_quote.quote_gas_price,
403-
sell_token_price: order_with_quote.quote_sell_token_price,
404-
}
405-
.fee(),
406-
},
407-
match order_with_quote.order_kind {
408-
DbOrderKind::Buy => model::order::OrderKind::Buy,
409-
DbOrderKind::Sell => model::order::OrderKind::Sell,
410-
},
411-
)
412-
})
413-
.count()
414-
.try_into()
415-
.unwrap())
416489
}
417490
}
418491

0 commit comments

Comments
 (0)