diff --git a/programs/mango-v4/src/state/orderbook/book.rs b/programs/mango-v4/src/state/orderbook/book.rs index 2c254975f..79aa6d5e6 100644 --- a/programs/mango-v4/src/state/orderbook/book.rs +++ b/programs/mango-v4/src/state/orderbook/book.rs @@ -40,36 +40,6 @@ impl<'a> Orderbook<'a> { } } - pub fn best_price(&self, now_ts: u64, oracle_price_lots: i64, side: Side) -> Option { - Some( - self.bookside(side) - .iter_valid(now_ts, oracle_price_lots) - .next()? - .price_lots, - ) - } - - /// Walk up the book `quantity` units and return the price at that level. If `quantity` units - /// not on book, return None - pub fn impact_price( - &self, - side: Side, - quantity: i64, - now_ts: u64, - oracle_price_lots: i64, - ) -> Option { - let mut sum: i64 = 0; - let bookside = self.bookside(side); - let iter = bookside.iter_valid(now_ts, oracle_price_lots); - for order in iter { - cm!(sum += order.node.quantity); - if sum >= quantity { - return Some(order.price_lots); - } - } - None - } - #[allow(clippy::too_many_arguments)] pub fn new_order( &mut self, diff --git a/programs/mango-v4/src/state/orderbook/bookside.rs b/programs/mango-v4/src/state/orderbook/bookside.rs index 961ac4950..05f54b64a 100644 --- a/programs/mango-v4/src/state/orderbook/bookside.rs +++ b/programs/mango-v4/src/state/orderbook/bookside.rs @@ -3,6 +3,7 @@ use num_enum::{IntoPrimitive, TryFromPrimitive}; use static_assertions::const_assert_eq; use super::*; +use crate::util::checked_math as cm; #[derive( Eq, @@ -158,6 +159,28 @@ impl BookSide { } sum } + + /// Return the price of the order closest to the spread + pub fn best_price(&self, now_ts: u64, oracle_price_lots: i64) -> Option { + Some( + self.iter_valid(now_ts, oracle_price_lots) + .next()? + .price_lots, + ) + } + + /// Walk up the book `quantity` units and return the price at that level. If `quantity` units + /// not on book, return None + pub fn impact_price(&self, quantity: i64, now_ts: u64, oracle_price_lots: i64) -> Option { + let mut sum: i64 = 0; + for order in self.iter_valid(now_ts, oracle_price_lots) { + cm!(sum += order.node.quantity); + if sum >= quantity { + return Some(order.price_lots); + } + } + None + } } #[cfg(test)] diff --git a/programs/mango-v4/src/state/orderbook/order.rs b/programs/mango-v4/src/state/orderbook/order.rs index c8b8f0a11..f97ebb631 100644 --- a/programs/mango-v4/src/state/orderbook/order.rs +++ b/programs/mango-v4/src/state/orderbook/order.rs @@ -99,8 +99,9 @@ impl Order { order_book: &Orderbook, ) -> i64 { if order_type == PostOrderType::PostOnlySlide { - if let Some(best_other_price) = - order_book.best_price(now_ts, oracle_price_lots, self.side.invert_side()) + if let Some(best_other_price) = order_book + .bookside(self.side.invert_side()) + .best_price(now_ts, oracle_price_lots) { post_only_slide_limit(self.side, best_other_price, price_lots) } else { diff --git a/programs/mango-v4/src/state/perp_market.rs b/programs/mango-v4/src/state/perp_market.rs index c77c26604..cede555fb 100644 --- a/programs/mango-v4/src/state/perp_market.rs +++ b/programs/mango-v4/src/state/perp_market.rs @@ -221,8 +221,12 @@ impl PerpMarket { let oracle_price_lots = self.native_price_to_lot(oracle_price); // Get current book price & compare it to index price - let bid = book.impact_price(Side::Bid, self.impact_quantity, now_ts, oracle_price_lots); - let ask = book.impact_price(Side::Ask, self.impact_quantity, now_ts, oracle_price_lots); + let bid = + book.bookside(Side::Bid) + .impact_price(self.impact_quantity, now_ts, oracle_price_lots); + let ask = + book.bookside(Side::Ask) + .impact_price(self.impact_quantity, now_ts, oracle_price_lots); let diff_price = match (bid, ask) { (Some(bid), Some(ask)) => {