From 689b79991d7213e8650f454c5890c98b08bfb0ae Mon Sep 17 00:00:00 2001 From: Richard Holzeis Date: Sat, 9 Sep 2023 11:38:40 +0200 Subject: [PATCH] chore: Set expired limit orders to failed --- coordinator/src/orderbook/db/orders.rs | 9 +++++++++ coordinator/src/orderbook/trading.rs | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/coordinator/src/orderbook/db/orders.rs b/coordinator/src/orderbook/db/orders.rs index 32f0c1f71..86d6d4b87 100644 --- a/coordinator/src/orderbook/db/orders.rs +++ b/coordinator/src/orderbook/db/orders.rs @@ -274,6 +274,15 @@ pub fn set_order_state( Ok(OrderbookOrder::from(order)) } +pub fn set_expired_limit_orders_to_failed(conn: &mut PgConnection) -> QueryResult { + diesel::update(orders::table) + .filter(orders::order_state.eq(OrderState::Open)) + .filter(orders::order_type.eq(OrderType::Limit)) + .filter(orders::expiry.lt(OffsetDateTime::now_utc())) + .set(orders::order_state.eq(OrderState::Failed)) + .execute(conn) +} + /// Returns the order by id pub fn get_with_id(conn: &mut PgConnection, uid: Uuid) -> QueryResult> { let x = orders::table diff --git a/coordinator/src/orderbook/trading.rs b/coordinator/src/orderbook/trading.rs index 9cbded9b3..6a013cc2f 100644 --- a/coordinator/src/orderbook/trading.rs +++ b/coordinator/src/orderbook/trading.rs @@ -163,6 +163,13 @@ impl Trading { } let mut conn = self.pool.get()?; + + // before processing any match we set all expired limit orders to failed, to ensure the do + // not get matched. + // todo(holzeis): orders should probably do not have an expiry, but should either be + // replaced or deleted if not wanted anymore. + orders::set_expired_limit_orders_to_failed(&mut conn)?; + let order = orders::insert(&mut conn, new_order.clone(), order_reason) .map_err(|e| anyhow!("Failed to insert new order into db: {e:#}"))?;