Skip to content

Commit eadf5d1

Browse files
authored
fix: limits account for replaced order quantity (#2002)
This fixes a bug in the logic to calculate whether a request to place a new order exceeds the available capacity and trading limits by properly accounting for the quantity that is being replaced by the new order request. Previously, this was not accounted for and attempting to replace an order, even if not changing quantity, could result in an error due to exceeding the trading capacity. Closes #1999.
1 parent 9d0e470 commit eadf5d1

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

lib/orderbook/OrderBook.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ interface OrderBook {
3535
on(event: 'ownOrder.filled', listener: (order: OwnOrder) => void): this;
3636
/** Adds a listener to be called when a local order was added */
3737
on(event: 'ownOrder.added', listener: (order: OwnOrder) => void): this;
38-
/** Adds a listener to be called when a local order was removed */
38+
/** Adds a listener to be called when a local order was removed either manually or due to a swap initiated by a peer */
3939
on(event: 'ownOrder.removed', listener: (order: OwnOrder) => void): this;
4040

4141
/** Notifies listeners that a remote order was added */
@@ -44,13 +44,13 @@ interface OrderBook {
4444
emit(event: 'peerOrder.invalidation', order: OrderPortion): boolean;
4545
/** Notifies listeners that all or part of a remote order was filled by an own order and removed */
4646
emit(event: 'peerOrder.filled', order: OrderPortion): boolean;
47-
/** Notifies listeners that all or part of a local order was swapped and removed, after it was filled and executed remotely */
47+
/** Notifies listeners that all or part of a local order was swapped after being filled and executed remotely */
4848
emit(event: 'ownOrder.swapped', order: OrderPortion): boolean;
4949
/** Notifies listeners that all or part of a local order was filled by an own order and removed */
5050
emit(event: 'ownOrder.filled', order: OwnOrder): boolean;
5151
/** Notifies listeners that a local order was added */
5252
emit(event: 'ownOrder.added', order: OwnOrder): boolean;
53-
/** Notifies listeners that a local order was removed */
53+
/** Notifies listeners that a local order was removed either manually or due to a swap initiated by a peer */
5454
emit(event: 'ownOrder.removed', order: OwnOrder): boolean;
5555
}
5656

@@ -489,16 +489,6 @@ class OrderBook extends EventEmitter {
489489

490490
const tp = this.getTradingPair(order.pairId);
491491

492-
if (!this.nobalancechecks) {
493-
// for limit orders, we use the price of our order to calculate inbound/outbound amounts
494-
// for market orders, we use the price of the best matching order in the order book
495-
const price = (order.price === 0 || order.price === Number.POSITIVE_INFINITY) ?
496-
(order.isBuy ? tp.quoteAsk() : tp.quoteBid()) :
497-
order.price;
498-
499-
await this.swaps.swapClientManager.checkSwapCapacities({ ...order, price });
500-
}
501-
502492
let replacedOrderIdentifier: OrderIdentifier | undefined;
503493
if (replaceOrderId) {
504494
assert(!discardRemaining, 'can not replace order and discard remaining order');
@@ -509,6 +499,29 @@ class OrderBook extends EventEmitter {
509499
throw errors.ORDER_NOT_FOUND(replaceOrderId);
510500
}
511501
assert(replacedOrderIdentifier.pairId === order.pairId);
502+
}
503+
504+
if (!this.nobalancechecks) {
505+
// for limit orders, we use the price of our order to calculate inbound/outbound amounts
506+
// for market orders, we use the price of the best matching order in the order book
507+
const price = (order.price === 0 || order.price === Number.POSITIVE_INFINITY) ?
508+
(order.isBuy ? tp.quoteAsk() : tp.quoteBid()) :
509+
order.price;
510+
511+
const quantityBeingReplaced =
512+
replacedOrderIdentifier ?
513+
this.getOwnOrder(replacedOrderIdentifier.id, replacedOrderIdentifier.pairId).quantity :
514+
0;
515+
516+
/** The quantity that's being added to the replaced order. */
517+
const quantityDelta = order.quantity - quantityBeingReplaced;
518+
519+
if (quantityDelta > 0) {
520+
await this.swaps.swapClientManager.checkSwapCapacities({ ...order, price, quantity: quantityDelta });
521+
}
522+
}
523+
524+
if (replacedOrderIdentifier) {
512525
this.addOrderHold(replacedOrderIdentifier.id, replacedOrderIdentifier.pairId);
513526
}
514527

0 commit comments

Comments
 (0)