Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions services/matching-engine/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion services/matching-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ path = "src/main.rs"
tokio = { version = "1.35", features = ["full"] }
axum = { version = "0.7", features = ["ws"] }
tower = "0.4"
tower-http = { version = "0.5", features = ["cors", "trace"] }
tower-http = { version = "0.5", features = ["cors", "trace", "limit"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
uuid = { version = "1.6", features = ["v4", "serde"] }
Expand All @@ -22,6 +22,10 @@ tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }
ordered-float = { version = "4.2", features = ["serde"] }
dashmap = "5.5"
parking_lot = "0.12"
sha2 = "0.10"
hex = "0.4"
crossbeam-channel = "0.5"
governor = "0.6"

[profile.release]
opt-level = 3
Expand Down
55 changes: 55 additions & 0 deletions services/matching-engine/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,61 @@ impl ExchangeEngine {
Ok(order)
}

/// Amend (Cancel/Replace) an order.
pub fn amend_order(
&self,
symbol: &str,
order_id: uuid::Uuid,
new_price: Option<Price>,
new_quantity: Option<Qty>,
) -> Result<(Vec<Trade>, Order, Order), String> {
if !self.cluster.is_accepting_orders() {
return Err("Node is not primary. Orders not accepted.".to_string());
}

let (trades, new_order, old_order) =
self.orderbooks.amend_order(symbol, order_id, new_price, new_quantity)?;

self.audit.record(
"ORDER_AMEND",
&order_id.to_string(),
&old_order.account_id,
symbol,
serde_json::json!({
"old_price": from_price(old_order.price),
"new_price": from_price(new_order.price),
"old_quantity": old_order.quantity,
"new_quantity": new_order.quantity,
}),
);

self.cluster.replicate(
"ORDER_AMEND",
serde_json::json!({
"order_id": order_id.to_string(),
"symbol": symbol,
"new_order_id": new_order.id.to_string(),
}),
);

// Post-trade processing for any fills from the amendment
for trade in &trades {
if let Ok((_buy_leg, _sell_leg)) = self.clearing.novate_trade(trade) {
self.cluster.replicate(
"TRADE",
serde_json::json!({
"trade_id": trade.id.to_string(),
"symbol": trade.symbol,
"price": from_price(trade.price),
"quantity": trade.quantity,
}),
);
}
}

Ok((trades, new_order, old_order))
}

/// Get exchange status summary.
pub fn status(&self) -> serde_json::Value {
let symbols = self.orderbooks.symbols();
Expand Down
Loading
Loading