Skip to content

Commit

Permalink
feat(orderbook): add initialQuantity to orders
Browse files Browse the repository at this point in the history
This adds a new `initialQuantity` property to all stamped orders to
record the quantity of an order when it was first received by `xud`.
This is in preparation for storing information about orders to the
database.
  • Loading branch information
sangaman committed Nov 8, 2018
1 parent 0d17dee commit 3f07a9c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/orderbook/OrderBook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ class OrderBook extends EventEmitter {
return false;
}

const stampedOrder: orders.PeerOrder = { ...order, createdAt: ms() };
const stampedOrder: orders.PeerOrder = { ...order, createdAt: ms(), initialQuantity: order.quantity };

if (!tp.addPeerOrder(stampedOrder)) {
this.logger.debug(`incoming peer order is duplicated: ${order.id}`);
Expand Down Expand Up @@ -525,7 +525,7 @@ class OrderBook extends EventEmitter {
throw errors.DUPLICATE_ORDER(order.localId);
}

return { ...order, id: uuidv1(), createdAt: ms() };
return { ...order, initialQuantity: order.quantity, id: uuidv1(), createdAt: ms() };
}

private createOutgoingOrder = (order: orders.OwnOrder): orders.OutgoingOrder => {
Expand Down
4 changes: 3 additions & 1 deletion lib/types/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Remote = {
type Stamp = OrderIdentifier & {
/** Epoch timestamp when this order was created locally. */
createdAt: number;
/** The number of base currency tokens initially available for the order, before any actions such as trades reduced the available quantity. */
initialQuantity: number;
};

export type OwnMarketOrder = MarketOrder & Local;
Expand All @@ -54,7 +56,7 @@ export type PeerOrder = LimitOrder & Stamp & Remote;
export type Order = OwnOrder | PeerOrder;

/** An outgoing version of a local own order without fields that are not useful for peers. */
export type OutgoingOrder = Pick<OwnOrder, Exclude<keyof OwnOrder, 'localId' | 'createdAt' | 'hold'>>;
export type OutgoingOrder = Pick<OwnOrder, Exclude<keyof OwnOrder, 'localId' | 'createdAt' | 'hold' | 'initialQuantity'>>;

/** An outgoing version of a local own order without fields that are not useful for peers. */
export type IncomingOrder = OutgoingOrder & Remote;
Expand Down
6 changes: 4 additions & 2 deletions test/integration/OrderBook.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const createOwnOrder = (price: number, quantity: number, isBuy: boolean, created
quantity,
isBuy,
createdAt,
initialQuantity: quantity,
id: uuidv1(),
localId: uuidv1(),
pairId: PAIR_ID,
Expand All @@ -35,6 +36,7 @@ const createPeerOrder = (
isBuy,
createdAt,
peerPubKey,
initialQuantity: quantity,
id: uuidv1(),
pairId: PAIR_ID,
});
Expand Down Expand Up @@ -115,15 +117,15 @@ describe('OrderBook', () => {
});

it('should create, partially match, and remove an order', async () => {
const order: orders.OwnLimitOrder = { pairId: 'LTC/BTC', localId: uuidv1(), quantity: 10, price: 10, isBuy: true, hold: 0 };
const order: orders.OwnOrder = createOwnOrder(10, 10, true);
await orderBook.placeLimitOrder(order);
const takerOrder: orders.OwnMarketOrder = { pairId: 'LTC/BTC', localId: uuidv1(), quantity: 5, isBuy: false, hold: 0 };
await orderBook.placeMarketOrder(takerOrder);
expect(() => orderBook.removeOwnOrderByLocalId(order.localId)).to.not.throw();
});

it('should not add a new own order with a duplicated localId', async () => {
const order: orders.OwnLimitOrder = { pairId: 'LTC/BTC', localId: uuidv1(), quantity: 10, price: 100, isBuy: false, hold: 0 };
const order: orders.OwnOrder = createOwnOrder(100, 10, false);

expect(orderBook.placeLimitOrder(order)).to.be.fulfilled;

Expand Down
2 changes: 2 additions & 0 deletions test/unit/TradingPair.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const createOwnOrder = (price: number, quantity: number, isBuy: boolean, created
quantity,
isBuy,
createdAt,
initialQuantity: quantity,
id: uuidv1(),
localId: uuidv1(),
pairId: PAIR_ID,
Expand All @@ -32,6 +33,7 @@ const createPeerOrder = (
isBuy,
createdAt,
peerPubKey,
initialQuantity: quantity,
id: uuidv1(),
pairId: PAIR_ID,
});
Expand Down

0 comments on commit 3f07a9c

Please sign in to comment.