Skip to content

Commit

Permalink
feat(orderbook): add assertions to removeOrder
Browse files Browse the repository at this point in the history
This adds assertions to the `removeOrder` logic to ensure that we do not
remove any portion of an order that is currently on hold.
  • Loading branch information
sangaman committed Nov 30, 2018
1 parent db69aca commit aacc958
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/orderbook/TradingPair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,25 @@ class TradingPair {

private removeOrder = <T extends Order>(orderId: string, maps: OrderSidesMaps<Order>, quantityToRemove?: number):
{ order: T, fullyRemoved: boolean } => {
assert(quantityToRemove === undefined || quantityToRemove > 0, 'quantityToRemove cannot be 0 or negative');
const order = maps.buy.get(orderId) || maps.sell.get(orderId);
if (!order) {
throw errors.ORDER_NOT_FOUND(orderId);
}

if (quantityToRemove && quantityToRemove < order.quantity) {
// if quantityToRemove is below the order quantity, reduce the order quantity
if (isOwnOrder(order)) {
assert(quantityToRemove <= order.quantity - order.hold, 'cannot remove more than available quantity after holds');
}
order.quantity = order.quantity - quantityToRemove;
this.logger.debug(`order quantity reduced by ${quantityToRemove}: ${orderId}`);
return { order: { ...order, quantity: quantityToRemove } as T, fullyRemoved: false } ;
} else {
// otherwise, remove the order entirely
if (isOwnOrder(order)) {
assert(order.hold === 0, 'cannot remove an order with a hold');
}
const list = order.isBuy ? maps.buy : maps.sell;
list.delete(order.id);

Expand Down

0 comments on commit aacc958

Please sign in to comment.