diff --git a/server/src/listeners/order_book/state.rs b/server/src/listeners/order_book/state.rs index 3fb74ae..275524a 100644 --- a/server/src/listeners/order_book/state.rs +++ b/server/src/listeners/order_book/state.rs @@ -1,7 +1,7 @@ use crate::{ listeners::order_book::{L2Snapshots, TimedSnapshots, utils::compute_l2_snapshots}, order_book::{ - Coin, InnerOrder, Oid, + Coin, InnerOrder, Oid, Px, multi_book::{OrderBooks, Snapshots}, }, prelude::*, @@ -100,6 +100,10 @@ impl OrderBookState { let time = order.time.and_utc().timestamp_millis(); let mut inner_order: InnerL4Order = order.try_into()?; inner_order.modify_sz(sz); + // Use px from book diff (more accurate than order status px) + if let Ok(diff_px) = Px::parse_from_str(diff.px()) { + inner_order.modify_px(diff_px); + } // must replace time with time of entering book, which is the timestamp of the order status update #[allow(clippy::unwrap_used)] inner_order.convert_trigger(time.try_into().unwrap()); diff --git a/server/src/listeners/order_book/utils.rs b/server/src/listeners/order_book/utils.rs index fdbd3b5..9c57c23 100644 --- a/server/src/listeners/order_book/utils.rs +++ b/server/src/listeners/order_book/utils.rs @@ -71,8 +71,15 @@ pub(super) fn validate_snapshot_consistency( return Err(format!("Missing {} book", coin.value()).into()); } } - if !snapshot_map.is_empty() { - return Err("Extra orderbooks detected".to_string().into()); + // Only error if expected has non-empty books that we don't have + // Empty books are equivalent to not existing + let non_empty_expected: Vec<_> = snapshot_map + .iter() + .filter(|(_, book)| !book.as_ref()[0].is_empty() || !book.as_ref()[1].is_empty()) + .map(|(c, _)| c.value()) + .collect(); + if !non_empty_expected.is_empty() { + return Err(format!("Extra orderbooks detected: {:?}", non_empty_expected).into()); } Ok(()) } diff --git a/server/src/order_book/types.rs b/server/src/order_book/types.rs index 447eb3f..cf3d90d 100644 --- a/server/src/order_book/types.rs +++ b/server/src/order_book/types.rs @@ -65,6 +65,7 @@ pub(crate) trait InnerOrder: Clone { fn decrement_sz(&mut self, dec: Sz); fn fill(&mut self, maker_order: &mut Self) -> Sz; fn modify_sz(&mut self, sz: Sz); + fn modify_px(&mut self, px: Px); fn convert_trigger(&mut self, ts: u64); } diff --git a/server/src/types/inner.rs b/server/src/types/inner.rs index d7535f0..630c00f 100644 --- a/server/src/types/inner.rs +++ b/server/src/types/inner.rs @@ -62,6 +62,10 @@ impl InnerOrder for InnerL4Order { match_sz } + fn modify_px(&mut self, px: Px) { + self.limit_px = px; + } + fn convert_trigger(&mut self, ts: u64) { if self.is_trigger { self.trigger_px = "0.0".to_string(); diff --git a/server/src/types/node_data.rs b/server/src/types/node_data.rs index acb6ed2..641a2c0 100644 --- a/server/src/types/node_data.rs +++ b/server/src/types/node_data.rs @@ -29,6 +29,10 @@ impl NodeDataOrderDiff { pub(crate) fn coin(&self) -> Coin { Coin::new(&self.coin) } + + pub(crate) fn px(&self) -> &str { + &self.px + } } #[derive(Debug, Clone, Serialize, Deserialize)]