From 3f07a9c2d6c055d327cfe4086f954c7b3caecd0a Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Wed, 31 Oct 2018 00:19:43 -0400 Subject: [PATCH] feat(orderbook): add initialQuantity to orders 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. --- lib/orderbook/OrderBook.ts | 4 ++-- lib/types/orders.ts | 4 +++- test/integration/OrderBook.spec.ts | 6 ++++-- test/unit/TradingPair.spec.ts | 2 ++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/orderbook/OrderBook.ts b/lib/orderbook/OrderBook.ts index 1957a88f7..06fcc176b 100644 --- a/lib/orderbook/OrderBook.ts +++ b/lib/orderbook/OrderBook.ts @@ -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}`); @@ -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 => { diff --git a/lib/types/orders.ts b/lib/types/orders.ts index 674a2ee15..2f031e5f1 100644 --- a/lib/types/orders.ts +++ b/lib/types/orders.ts @@ -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; @@ -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>; +export type OutgoingOrder = Pick>; /** An outgoing version of a local own order without fields that are not useful for peers. */ export type IncomingOrder = OutgoingOrder & Remote; diff --git a/test/integration/OrderBook.spec.ts b/test/integration/OrderBook.spec.ts index 1401c0fcd..c2cca73f7 100644 --- a/test/integration/OrderBook.spec.ts +++ b/test/integration/OrderBook.spec.ts @@ -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, @@ -35,6 +36,7 @@ const createPeerOrder = ( isBuy, createdAt, peerPubKey, + initialQuantity: quantity, id: uuidv1(), pairId: PAIR_ID, }); @@ -115,7 +117,7 @@ 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); @@ -123,7 +125,7 @@ describe('OrderBook', () => { }); 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; diff --git a/test/unit/TradingPair.spec.ts b/test/unit/TradingPair.spec.ts index 022b30aa8..3fae5f23b 100644 --- a/test/unit/TradingPair.spec.ts +++ b/test/unit/TradingPair.spec.ts @@ -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, @@ -32,6 +33,7 @@ const createPeerOrder = ( isBuy, createdAt, peerPubKey, + initialQuantity: quantity, id: uuidv1(), pairId: PAIR_ID, });