From bcf3b0918102a4b834d03c509e8fe78bc897059a Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Fri, 20 Sep 2019 01:03:25 +0300 Subject: [PATCH 1/2] Use integer offer price parts --- src/service/dex/offers_graph.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/service/dex/offers_graph.ts b/src/service/dex/offers_graph.ts index 7f7a068e..350562a0 100644 --- a/src/service/dex/offers_graph.ts +++ b/src/service/dex/offers_graph.ts @@ -16,6 +16,8 @@ interface IEdge { interface IAssetOrder { amount: BigNumber; price: BigNumber; + priceN: number; + priceD: number; } class AssetOrders { @@ -37,17 +39,17 @@ class AssetOrders { public buy(amountToBuy: BigNumber): BigNumber { let amountToSell = new BigNumber(0); - for (const { amount, price } of this.orders) { + for (const { amount, priceN, priceD } of this.orders) { if (amountToBuy.gt(amount)) { - amountToSell = amountToSell.plus(amount.times(price)); + amountToSell = amountToSell.plus(amount.times(priceN).div(priceD)); amountToBuy = amountToBuy.minus(amount); } else { - amountToSell = amountToSell.plus(amountToBuy.times(price)); + amountToSell = amountToSell.plus(amountToBuy.times(priceN).div(priceD)); break; } } - return amountToSell; + return amountToSell.integerValue(BigNumber.ROUND_FLOOR); } } @@ -70,7 +72,12 @@ export class OffersGraph { const [assetToBuy, assetToSell] = [offer.buying, offer.selling]; const edge = this.getEdgeData(assetToSell, assetToBuy); - const order: IAssetOrder = { amount: offer.amount, price: offer.price }; + const order: IAssetOrder = { + amount: offer.amount, + price: offer.price, + priceN: offer.priceN, + priceD: offer.priceD + }; if (!edge) { this.addEdge(assetToSell, assetToBuy, { @@ -99,8 +106,8 @@ export class OffersGraph { let capacity = new BigNumber(0); const orderBook = new AssetOrders(); - for (const { amount, price } of offers) { - orderBook.addOrder({ amount, price }); + for (const { amount, price, priceN, priceD } of offers) { + orderBook.addOrder({ amount, price, priceN, priceD }); capacity = capacity.plus(amount); } From d7bb85f7ddbe49cdf30509c41efb06e05fd0e7d7 Mon Sep 17 00:00:00 2001 From: Timur Ramazanov Date: Fri, 20 Sep 2019 13:54:12 +0300 Subject: [PATCH 2/2] Using selling bound --- src/service/dex/offers_graph.ts | 58 +++++++++++++++++++++++---------- 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/src/service/dex/offers_graph.ts b/src/service/dex/offers_graph.ts index 350562a0..99f6f7f0 100644 --- a/src/service/dex/offers_graph.ts +++ b/src/service/dex/offers_graph.ts @@ -13,22 +13,46 @@ interface IEdge { data: IEdgeData; } -interface IAssetOrder { - amount: BigNumber; - price: BigNumber; - priceN: number; - priceD: number; +class AssetOrder { + public readonly amount: BigNumber; + public readonly price: BigNumber; // we use it just for sorting + public readonly priceN: number; + public readonly priceD: number; + + constructor(offer: Offer) { + this.amount = offer.amount; + this.price = offer.price; + this.priceD = offer.priceD; + this.priceN = offer.priceN; + } + + public get sellingBound() { + const { amount, priceN, priceD } = this; + let sellingBound = amount; + + if (priceN <= priceD) { + sellingBound = amount + .times(priceN) + .div(priceD) + .integerValue(BigNumber.ROUND_FLOOR) + .times(priceD) + .div(priceN) + .integerValue(BigNumber.ROUND_CEIL); + } + + return sellingBound; + } } class AssetOrders { - constructor(private readonly orders: IAssetOrder[] = []) {} + constructor(private readonly orders: AssetOrder[] = []) {} public getOrders() { // return copy of the orders return this.orders.map(o => o); } - public addOrder(order: IAssetOrder): void { + public addOrder(order: AssetOrder): void { this.orders.push(order); } @@ -39,7 +63,10 @@ class AssetOrders { public buy(amountToBuy: BigNumber): BigNumber { let amountToSell = new BigNumber(0); - for (const { amount, priceN, priceD } of this.orders) { + for (const order of this.orders) { + const amount = order.sellingBound; + const { priceN, priceD } = order; + if (amountToBuy.gt(amount)) { amountToSell = amountToSell.plus(amount.times(priceN).div(priceD)); amountToBuy = amountToBuy.minus(amount); @@ -49,7 +76,7 @@ class AssetOrders { } } - return amountToSell.integerValue(BigNumber.ROUND_FLOOR); + return amountToSell; } } @@ -72,12 +99,7 @@ export class OffersGraph { const [assetToBuy, assetToSell] = [offer.buying, offer.selling]; const edge = this.getEdgeData(assetToSell, assetToBuy); - const order: IAssetOrder = { - amount: offer.amount, - price: offer.price, - priceN: offer.priceN, - priceD: offer.priceD - }; + const order = new AssetOrder(offer); if (!edge) { this.addEdge(assetToSell, assetToBuy, { @@ -106,9 +128,9 @@ export class OffersGraph { let capacity = new BigNumber(0); const orderBook = new AssetOrders(); - for (const { amount, price, priceN, priceD } of offers) { - orderBook.addOrder({ amount, price, priceN, priceD }); - capacity = capacity.plus(amount); + for (const offer of offers) { + orderBook.addOrder(new AssetOrder(offer)); + capacity = capacity.plus(offer.amount); } this.updateEdge(selling, buying, { capacity, orderBook });