-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PROPOSAL] Correctly label the orderbook orders metric #2917
Changes from all commits
41adcd3
e686e8e
e88e34f
21a667b
bed0d84
028a1f8
b393f2f
b2ca305
e1b4929
2adfe39
9cb45a7
adee18a
f8aeba6
b4c484d
ed621c6
3ec80de
fc58810
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ use { | |
database::{ | ||
byte_array::ByteArray, | ||
order_events::{insert_order_event, OrderEvent, OrderEventLabel}, | ||
orders::{FullOrder, OrderKind as DbOrderKind}, | ||
orders::{self, FullOrder, OrderKind as DbOrderKind}, | ||
}, | ||
ethcontract::H256, | ||
futures::{stream::TryStreamExt, FutureExt, StreamExt}, | ||
|
@@ -76,13 +76,36 @@ pub trait OrderStoring: Send + Sync { | |
limit: Option<u64>, | ||
) -> Result<Vec<Order>>; | ||
async fn latest_order_event(&self, order_uid: &OrderUid) -> Result<Option<OrderEvent>>; | ||
async fn single_order_with_quote(&self, uid: &OrderUid) -> Result<Option<OrderWithQuote>>; | ||
} | ||
|
||
pub struct SolvableOrders { | ||
pub orders: Vec<Order>, | ||
pub latest_settlement_block: u64, | ||
} | ||
|
||
pub struct OrderWithQuote { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created a new object which contains an order with the quote information (the quote information corresponding to the one stored in |
||
pub order: Order, | ||
pub quote: Option<orders::Quote>, | ||
} | ||
|
||
impl OrderWithQuote { | ||
pub fn new(order: Order, quote: Option<Quote>) -> Self { | ||
Self { | ||
quote: quote.map(|quote| orders::Quote { | ||
order_uid: ByteArray(order.metadata.uid.0), | ||
gas_amount: quote.data.fee_parameters.gas_amount, | ||
gas_price: quote.data.fee_parameters.gas_price, | ||
sell_token_price: quote.data.fee_parameters.sell_token_price, | ||
sell_amount: u256_to_big_decimal("e.sell_amount), | ||
buy_amount: u256_to_big_decimal("e.buy_amount), | ||
solver: ByteArray(quote.data.solver.0), | ||
}), | ||
order, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum InsertionError { | ||
DuplicatedRecord, | ||
|
@@ -323,6 +346,51 @@ impl OrderStoring for Postgres { | |
order.map(full_order_into_model_order).transpose() | ||
} | ||
|
||
async fn single_order_with_quote(&self, uid: &OrderUid) -> Result<Option<OrderWithQuote>> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could probably be part of an existing DB test which tests quote fetching in general. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really good idea, adding them! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a test at |
||
let _timer = super::Metrics::get() | ||
.database_queries | ||
.with_label_values(&["single_order_with_quote"]) | ||
.start_timer(); | ||
|
||
let mut ex = self.pool.acquire().await?; | ||
let order = orders::single_full_order_with_quote(&mut ex, &ByteArray(uid.0)).await?; | ||
order | ||
.map(|order_with_quote| { | ||
let quote = match ( | ||
order_with_quote.quote_buy_amount, | ||
order_with_quote.quote_sell_amount, | ||
order_with_quote.quote_gas_amount, | ||
order_with_quote.quote_gas_price, | ||
order_with_quote.quote_sell_token_price, | ||
order_with_quote.solver, | ||
) { | ||
( | ||
Some(buy_amount), | ||
Some(sell_amount), | ||
Some(gas_amount), | ||
Some(gas_price), | ||
Some(sell_token_price), | ||
Some(solver), | ||
) => Some(orders::Quote { | ||
order_uid: order_with_quote.full_order.uid, | ||
gas_amount, | ||
gas_price, | ||
sell_token_price, | ||
sell_amount, | ||
buy_amount, | ||
solver, | ||
}), | ||
_ => None, | ||
}; | ||
|
||
Ok(OrderWithQuote { | ||
order: full_order_into_model_order(order_with_quote.full_order)?, | ||
quote, | ||
}) | ||
}) | ||
.transpose() | ||
} | ||
|
||
async fn orders_for_tx(&self, tx_hash: &H256) -> Result<Vec<Order>> { | ||
tokio::try_join!( | ||
self.user_order_for_tx(tx_hash), | ||
|
@@ -599,6 +667,7 @@ mod tests { | |
order::{Order, OrderData, OrderMetadata, OrderStatus, OrderUid}, | ||
signature::{Signature, SigningScheme}, | ||
}, | ||
primitive_types::U256, | ||
std::sync::atomic::{AtomicI64, Ordering}, | ||
}; | ||
|
||
|
@@ -1078,9 +1147,27 @@ mod tests { | |
..Default::default() | ||
}; | ||
|
||
db.insert_order(&order, None).await.unwrap(); | ||
let quote = Quote { | ||
id: Some(5), | ||
sell_amount: U256::from(1), | ||
buy_amount: U256::from(2), | ||
..Default::default() | ||
}; | ||
db.insert_order(&order, Some(quote.clone())).await.unwrap(); | ||
|
||
let interactions = db.single_order(&uid).await.unwrap().unwrap().interactions; | ||
assert_eq!(interactions, order.interactions); | ||
|
||
// Test `single_order_with_quote` | ||
let single_order_with_quote = db.single_order_with_quote(&uid).await.unwrap().unwrap(); | ||
assert_eq!(single_order_with_quote.order, order); | ||
assert_eq!( | ||
single_order_with_quote.quote.clone().unwrap().sell_amount, | ||
u256_to_big_decimal("e.sell_amount) | ||
); | ||
assert_eq!( | ||
single_order_with_quote.quote.unwrap().buy_amount, | ||
u256_to_big_decimal("e.buy_amount) | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Create a new query so we don't need to do two roundtrip to get an order with its quote data.