Skip to content

Commit

Permalink
Added run market order tests
Browse files Browse the repository at this point in the history
  • Loading branch information
crnbarr93 committed Feb 20, 2024
1 parent 6c18090 commit febf5a6
Show file tree
Hide file tree
Showing 2 changed files with 437 additions and 44 deletions.
65 changes: 41 additions & 24 deletions contracts/orderbook/src/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,39 +180,59 @@ pub fn run_market_order(
let placed_order_denom = orderbook.get_expected_denom(&order.order_direction);

let (min_tick, max_tick, ordering) = match order.order_direction {
OrderDirection::Ask => (Some(orderbook.next_bid_tick), tick_bound, Order::Ascending),
OrderDirection::Bid => (tick_bound, Some(orderbook.next_ask_tick), Order::Descending),
OrderDirection::Ask => {
if let Some(tick_bound) = tick_bound {
ensure!(
tick_bound <= orderbook.next_bid_tick,
ContractError::InvalidTickId {
tick_id: tick_bound
}
);
}
(tick_bound, Some(orderbook.next_bid_tick), Order::Descending)
}
OrderDirection::Bid => {
if let Some(tick_bound) = tick_bound {
ensure!(
tick_bound >= orderbook.next_ask_tick,
ContractError::InvalidTickId {
tick_id: tick_bound
}
);
}
(Some(orderbook.next_ask_tick), tick_bound, Order::Ascending)
}
};

// Create ticks iterator between first tick and requested tick
// TODO: Remove filter map and map
let ticks = TICK_LIQUIDITY
.prefix(order.book_id)
.range(
storage,
min_tick.map(Bound::inclusive),
max_tick.map(Bound::inclusive),
ordering,
)
.filter_map(|x| x.ok())
.map(|(id, _)| id);

for current_tick in ticks {
let ticks = TICK_LIQUIDITY.prefix(order.book_id).range(
storage,
min_tick.map(Bound::inclusive),
max_tick.map(Bound::inclusive),
ordering,
);

for maybe_current_tick in ticks {
let current_tick = maybe_current_tick?.0;

// Create orders iterator for all orders on current tick
let tick_orders = orders()
.prefix((order.book_id, current_tick))
.range(storage, None, None, Order::Ascending)
.filter_map(|r| r.ok())
.map(|(_, order)| order);
let tick_orders = orders().prefix((order.book_id, current_tick)).range(
storage,
None,
None,
Order::Ascending,
);

for current_order in tick_orders {
for maybe_current_order in tick_orders {
let current_order = maybe_current_order?.1;
let fill_quantity = order.quantity.min(current_order.quantity);
// Add to total amount fulfiled from placed order
amount_fulfiled = amount_fulfiled.checked_add(fill_quantity)?;
// Generate fulfilment for current order
let fulfilment = Fulfilment::new(current_order, fill_quantity);
fulfilments.push(fulfilment);

// Update remaining order quantity
order.quantity = order.quantity.checked_sub(fill_quantity)?;
// TODO: Price detection
if order.quantity.is_zero() {
Expand All @@ -228,7 +248,6 @@ pub fn run_market_order(

// TODO: Price detection
if order.quantity.is_zero() {
order.quantity -= amount_fulfiled;
return Ok((
fulfilments,
BankMsg::Send {
Expand All @@ -239,8 +258,6 @@ pub fn run_market_order(
}
}

order.quantity = order.quantity.checked_sub(amount_fulfiled)?;

// TODO: Price detection
Ok((
fulfilments,
Expand Down
Loading

0 comments on commit febf5a6

Please sign in to comment.