Skip to content

Commit

Permalink
fixup! feat: Rewrite order matching strategy
Browse files Browse the repository at this point in the history
Delete and insert instead of update
  • Loading branch information
holzeis committed Jun 4, 2024
1 parent 3598fc1 commit 1cfc725
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 63 deletions.
23 changes: 0 additions & 23 deletions coordinator/src/orderbook/db/orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderbookOrder> {
let order: Order = diesel::update(orders::table)
.set((
orders::direction.eq::<Direction>(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())
}
53 changes: 14 additions & 39 deletions coordinator/src/orderbook/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -365,25 +357,22 @@ impl Orderbook {
Ok(matches.into_values().collect::<Vec<_>>())
}

/// 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<Message> {
// 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<Message> {
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))
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion coordinator/src/orderbook/trading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1cfc725

Please sign in to comment.