pub struct MatchingEngine { /* private fields */ }Expand description
Matching engine managing spot trading pairs and their orderbooks.
§Examples
Basic usage:
use off_grid_primitives::spot::MatchingEngine;
// Create a new matching engine
let engine = MatchingEngine::new();
// By default there are no pairs
assert_eq!(engine.pair_count(), 0);
assert!(!engine.has_pair(&b"BTC-USD".to_vec()));Implementations§
Source§impl MatchingEngine
impl MatchingEngine
pub fn add_pair( &mut self, cid: impl Into<Vec<u8>>, client_admin_account_id: impl Into<Vec<u8>>, client_fee_account_id: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, timestamp: i64, )
pub fn add_pair_client( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, admin_account_id: impl Into<Vec<u8>>, fee_account_id: impl Into<Vec<u8>>, ) -> Result<EventQueue, OrderBookError>
Sourcepub fn limit_sell(
&mut self,
cid: impl Into<Vec<u8>>,
pair_id: impl Into<Vec<u8>>,
existing_order_id: Option<OrderId>,
owner: impl Into<Vec<u8>>,
price: u64,
amnt: u64,
iqty: u64,
timestamp: i64,
expires_at: i64,
maker_fee_bps: i16,
taker_fee_bps: i16,
time_in_force: TimeInForce,
) -> Result<EventQueue, OrderBookError>
pub fn limit_sell( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, existing_order_id: Option<OrderId>, owner: impl Into<Vec<u8>>, price: u64, amnt: u64, iqty: u64, timestamp: i64, expires_at: i64, maker_fee_bps: i16, taker_fee_bps: i16, time_in_force: TimeInForce, ) -> Result<EventQueue, OrderBookError>
Place a limit sell order (ask order) Matches against existing orders first, then places remaining in orderbook based on time_in_force
Returns ((order_id, found_dormant), events) where:
order_id: The order ID of the placed orderfound_dormant: Whether a dormant order was found and reusedevents: Vector of events emitted during this operation
Sourcepub fn limit_buy(
&mut self,
cid: impl Into<Vec<u8>>,
pair_id: impl Into<Vec<u8>>,
existing_order_id: Option<OrderId>,
owner: impl Into<Vec<u8>>,
price: u64,
amount: u64,
public_amount: u64,
timestamp: i64,
expires_at: i64,
maker_fee_bps: i16,
taker_fee_bps: i16,
time_in_force: TimeInForce,
) -> Result<EventQueue, OrderBookError>
pub fn limit_buy( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, existing_order_id: Option<OrderId>, owner: impl Into<Vec<u8>>, price: u64, amount: u64, public_amount: u64, timestamp: i64, expires_at: i64, maker_fee_bps: i16, taker_fee_bps: i16, time_in_force: TimeInForce, ) -> Result<EventQueue, OrderBookError>
Place a limit buy order (bid order) Matches against existing orders first, then places remaining in orderbook based on time_in_force
Returns ((order_id, found_dormant), events) where:
order_id: The order ID of the placed orderfound_dormant: Whether a dormant order was found and reusedevents: Vector of events emitted during this operation
Sourcepub fn market_sell(
&mut self,
cid: impl Into<Vec<u8>>,
pair_id: impl Into<Vec<u8>>,
existing_order_id: Option<OrderId>,
owner: impl Into<Vec<u8>>,
amount: u64,
public_amount: u64,
timestamp: i64,
expires_at: i64,
maker_fee_bps: i16,
taker_fee_bps: i16,
time_in_force: TimeInForce,
) -> Result<EventQueue, OrderBookError>
pub fn market_sell( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, existing_order_id: Option<OrderId>, owner: impl Into<Vec<u8>>, amount: u64, public_amount: u64, timestamp: i64, expires_at: i64, maker_fee_bps: i16, taker_fee_bps: i16, time_in_force: TimeInForce, ) -> Result<EventQueue, OrderBookError>
Execute a market sell order Matches against existing orders first (market orders match at any price)
Returns (OrderMatch, events) where:
OrderMatch: Contains trade execution detailsevents: Vector of events emitted during this operation
Sourcepub fn market_buy(
&mut self,
cid: impl Into<Vec<u8>>,
pair_id: impl Into<Vec<u8>>,
existing_order_id: Option<OrderId>,
owner: impl Into<Vec<u8>>,
amount: u64,
public_amount: u64,
timestamp: i64,
expires_at: i64,
maker_fee_bps: i16,
taker_fee_bps: i16,
time_in_force: TimeInForce,
) -> Result<EventQueue, OrderBookError>
pub fn market_buy( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, existing_order_id: Option<OrderId>, owner: impl Into<Vec<u8>>, amount: u64, public_amount: u64, timestamp: i64, expires_at: i64, maker_fee_bps: i16, taker_fee_bps: i16, time_in_force: TimeInForce, ) -> Result<EventQueue, OrderBookError>
Execute a market buy order Matches against existing orders first (market orders match at any price)
Returns (OrderMatch, events) where:
OrderMatch: Contains trade execution detailsevents: Vector of events emitted during this operation
Sourcepub fn cancel_order(
&mut self,
cid: impl Into<Vec<u8>>,
pair_id: impl Into<Vec<u8>>,
order_id: OrderId,
owner: impl Into<Vec<u8>>,
is_bid: bool,
) -> Result<EventQueue, OrderBookError>
pub fn cancel_order( &mut self, cid: impl Into<Vec<u8>>, pair_id: impl Into<Vec<u8>>, order_id: OrderId, owner: impl Into<Vec<u8>>, is_bid: bool, ) -> Result<EventQueue, OrderBookError>
Cancel an order
Returns events - Vector of events emitted during this operation
order_id: The ID of the order to cancelowner: The owner of the order (for authorization)is_bid: Whether the order is a bid (true) or ask (false)
Sourcepub fn pair_count(&self) -> usize
pub fn pair_count(&self) -> usize
Get the number of pairs in the matching engine