diff --git a/coordinator/src/orderbook/db/orders.rs b/coordinator/src/orderbook/db/orders.rs index fff9701e7..d95367c3a 100644 --- a/coordinator/src/orderbook/db/orders.rs +++ b/coordinator/src/orderbook/db/orders.rs @@ -459,26 +459,3 @@ pub fn get_all_limit_order_filled_matches( Ok(filled_matches) } - -/// Updates a given order if it is still open. -pub fn update_order(conn: &mut PgConnection, order: OrderbookOrder) -> QueryResult { - let order: Order = diesel::update(orders::table) - .set(( - orders::direction.eq::(order.direction.into()), - orders::timestamp.eq(OffsetDateTime::now_utc()), - orders::quantity.eq(order.quantity.to_f32().expect("to fit")), - orders::price.eq(order.price.to_f32().expect("to fit")), - orders::leverage.eq(order.leverage), - orders::expiry.eq(order.expiry), - )) - .filter( - orders::order_id.eq(order.id).and( - orders::trader_pubkey - .eq(order.trader_id.to_string()) - .and(orders::order_state.eq(OrderState::Open)), - ), - ) - .get_result(conn)?; - - Ok(order.into()) -} diff --git a/coordinator/src/orderbook/mod.rs b/coordinator/src/orderbook/mod.rs index 1eaa092fa..c73c113b8 100644 --- a/coordinator/src/orderbook/mod.rs +++ b/coordinator/src/orderbook/mod.rs @@ -73,14 +73,6 @@ impl OrderbookSide { let entry = self.orders.entry(order.price).or_default(); entry.push(order); } - /// updates an order in the orderbook. Ignores the update if order does not exist. - pub fn update_order(&mut self, updated_order: Order) { - if let Some(vec) = self.orders.get_mut(&updated_order.price) { - if let Some(order) = vec.iter_mut().find(|o| o.id == updated_order.id) { - *order = updated_order; - } - } - } /// removes the order by the given id from the orderbook. pub fn remove_order(&mut self, order_id: Uuid) { @@ -365,25 +357,22 @@ impl Orderbook { Ok(matches.into_values().collect::>()) } - /// Updates an order in the orderbook. If the order id couldn't be found the update will be + /// Updates a limit order in the orderbook. If the order id couldn't be found the update will be /// ignored. - async fn update_order(&mut self, order: Order) -> Result { - // TODO(holzeis): We might want to delay that action in a regular task allowing us to - // process orderbook updates much faster in memory and postpone the expensive - // database transactions. - let order = spawn_blocking({ - let mut conn = self.pool.clone().get()?; - move || { - let order = orders::update_order(&mut conn, order)?; - anyhow::Ok(order) - } + async fn update_limit_order(&mut self, order: Order) -> Result { + self.remove_order(order.id).await?; + self.add_limit_order(NewLimitOrder { + id: order.id, + contract_symbol: order.contract_symbol, + price: order.price, + quantity: order.quantity, + trader_id: order.trader_id, + direction: order.direction, + leverage: Decimal::try_from(order.leverage).expect("to fit"), + expiry: order.expiry, + stable: false, }) - .await??; - - match order.direction { - commons::Direction::Short => self.shorts.update_order(order), - commons::Direction::Long => self.longs.update_order(order), - } + .await?; Ok(Message::Update(order)) } @@ -504,20 +493,6 @@ mod orderbook_tests { assert_eq!(Some(long_order), order); } - #[test] - pub fn test_update_order_to_orderbook_side() { - let mut longs = OrderbookSide::new(vec![], Direction::Long); - - let mut long_order = dummy_order(dec!(100), dec!(50000), Direction::Long); - longs.add_order(long_order); - - long_order.quantity = dec!(200); - longs.update_order(long_order); - - let order = longs.get_orders().first().cloned(); - assert_eq!(Some(long_order), order); - } - #[test] pub fn test_remove_order_from_orderbook_side() { let mut longs = OrderbookSide::new(vec![], Direction::Long); diff --git a/coordinator/src/orderbook/trading.rs b/coordinator/src/orderbook/trading.rs index bb56ada9d..2dfbdc385 100644 --- a/coordinator/src/orderbook/trading.rs +++ b/coordinator/src/orderbook/trading.rs @@ -142,7 +142,7 @@ pub fn spawn_orderbook( order_type: commons::OrderType::Limit, .. }, - ) => match orderbook.update_order(order).await { + ) => match orderbook.update_limit_order(order).await { Ok(message) => Some(message), Err(e) => { tracing::error!(trader_pubkey=%order.trader_id, order_id=%order.id,