From 725b7bf4c8144299c9c7c9304b8d3d3e39913b50 Mon Sep 17 00:00:00 2001 From: sacOO7 Date: Tue, 5 May 2020 02:12:22 +0530 Subject: [PATCH] [updated bestReturns utils and tests for the same][#3] --- lisk-dex-electron/src/Utils.js | 8 ++- lisk-dex-electron/src/tests/Utils.test.js | 79 +++++++++++++++++------ 2 files changed, 65 insertions(+), 22 deletions(-) diff --git a/lisk-dex-electron/src/Utils.js b/lisk-dex-electron/src/Utils.js index 31421b0..fa76a4b 100644 --- a/lisk-dex-electron/src/Utils.js +++ b/lisk-dex-electron/src/Utils.js @@ -91,8 +91,8 @@ const estimatedFlatReturnsForBuyer = (amount, price, asks) => { const estimateBestReturnsForSeller = (amount, price, bids) => { let estimatedReturns = 0; let status = EstimationStatus.NO_MATCH; + let amountYetToBeSold = amount; for (const bid of bids) { - const amountYetToBeSold = amount - (estimatedReturns / price); if (price <= bid.price) { const bestBidReturns = amountYetToBeSold * bid.price; if (bid.amount >= bestBidReturns) { @@ -101,6 +101,8 @@ const estimateBestReturnsForSeller = (amount, price, bids) => { break; } else { estimatedReturns += bid.amount; + const amountSold = (bid.amount / bid.price); + amountYetToBeSold -= amountSold; status = EstimationStatus.PARTIAL_MATCH; } } @@ -111,8 +113,8 @@ const estimateBestReturnsForSeller = (amount, price, bids) => { const estimatedBestReturnsForBuyer = (amount, price, asks) => { let estimatedReturns = 0; let status = EstimationStatus.NO_MATCH; + let amountYetToBeSold = amount; for (const ask of asks) { - const amountYetToBeSold = amount - (estimatedReturns * price); if (price >= ask.price) { const bestAskReturns = amountYetToBeSold / ask.price; if (ask.amount >= bestAskReturns) { @@ -121,6 +123,8 @@ const estimatedBestReturnsForBuyer = (amount, price, asks) => { break; } else { estimatedReturns += ask.amount; + const amountSold = (ask.amount * ask.price); + amountYetToBeSold -= amountSold; status = EstimationStatus.PARTIAL_MATCH; } } diff --git a/lisk-dex-electron/src/tests/Utils.test.js b/lisk-dex-electron/src/tests/Utils.test.js index d274e81..030d286 100644 --- a/lisk-dex-electron/src/tests/Utils.test.js +++ b/lisk-dex-electron/src/tests/Utils.test.js @@ -1,5 +1,6 @@ import { - groupByKey, Keys, Values, formatThousands, estimatedReturnsForSeller, estimatedReturnsForBuyer, EstimationStatus, + groupByKey, Keys, Values, formatThousands, estimateFlatReturnsForSeller, estimatedFlatReturnsForBuyer, EstimationStatus, + estimateBestReturnsForSeller, estimatedBestReturnsForBuyer, } from '../Utils'; import { asks, bids } from './fixtures/orderbook'; @@ -61,20 +62,58 @@ describe('Utils tests => ', () => { }); test.each` - sellerAmountInLshForSell | marketPriceInLsk | estimatedReturnsInLsk | buyerOrders | estimatedStatus - ${150} | ${0.78} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} - ${160} | ${0.40} | ${64} | ${bids} | ${EstimationStatus.MATCH} - ${142} | ${0.48} | ${68.16} | ${bids} | ${EstimationStatus.MATCH} - ${739.130434783} | ${0.23} | ${170} | ${bids} | ${EstimationStatus.MATCH} - ${947.368421053} | ${0.19} | ${180} | ${bids} | ${EstimationStatus.MATCH} - ${818.181818182} | ${0.22} | ${178.2073} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} - ${327.868852459} | ${0.61} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} - ${457.142857143} | ${0.35} | ${154.4369} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} - ${1495.0166113} | ${0.602} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} - `('Should estimate $sellerAmountInLshForSell based on $marketPriceInLsk to $estimatedReturnsInLsk', ({ + sellerAmountInLshForSell | marketPriceInLsk | estimatedReturnsInLsk | buyerOrders | estimatedStatus + ${150} | ${0.78} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + ${160} | ${0.40} | ${64} | ${bids} | ${EstimationStatus.MATCH} + ${142} | ${0.48} | ${68.16} | ${bids} | ${EstimationStatus.MATCH} + ${739.130434783} | ${0.23} | ${170} | ${bids} | ${EstimationStatus.MATCH} + ${947.368421053} | ${0.19} | ${180} | ${bids} | ${EstimationStatus.MATCH} + ${818.181818182} | ${0.22} | ${178.2073} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${327.868852459} | ${0.61} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + ${457.142857143} | ${0.35} | ${154.4369} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${1495.0166113} | ${0.602} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + `('Should estimate flat returns for {$sellerAmountInLshForSell} LSH based on {$marketPriceInLsk} LSK/LSH', ({ buyerOrders, sellerAmountInLshForSell, marketPriceInLsk, estimatedReturnsInLsk, estimatedStatus, }) => { - const actualEstimatedReturnsInLsk = estimatedReturnsForSeller(sellerAmountInLshForSell, marketPriceInLsk, buyerOrders); + const actualEstimatedReturnsInLsk = estimateFlatReturnsForSeller(sellerAmountInLshForSell, marketPriceInLsk, buyerOrders); + expect(actualEstimatedReturnsInLsk.estimatedReturns.toFixed(4)).toBe(estimatedReturnsInLsk.toFixed(4)); + expect(actualEstimatedReturnsInLsk.status).toBe(estimatedStatus); +}); + + test.each` + buyerAmountInLskForSell | marketPriceInLsk | estimatedLshCanBeBought | sellerOrders | estimatedStatus + ${2000} | ${0.77} | ${3.1169} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} + ${2793.6} | ${0.96} | ${2910} | ${asks} | ${EstimationStatus.MATCH} + ${2821.5} | ${0.95} | ${2970} | ${asks} | ${EstimationStatus.MATCH} + ${2845.25} | ${0.95} | ${2995} | ${asks} | ${EstimationStatus.MATCH} + ${63.36} | ${0.88} | ${72} | ${asks} | ${EstimationStatus.MATCH} + ${79.2} | ${0.88} | ${86.3001} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} + ${76.244} | ${0.76} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} + ${78} | ${0.56} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} + ${974.86} | ${0.20} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} + `('Should estimate flat returns for {$buyerAmountInLskForSell} LSK based on {$marketPriceInLsk} LSK/LSH', ({ + sellerOrders, buyerAmountInLskForSell, marketPriceInLsk, estimatedLshCanBeBought, estimatedStatus, +}) => { + const actualLshCanBeBought = estimatedFlatReturnsForBuyer(buyerAmountInLskForSell, marketPriceInLsk, sellerOrders); + expect(actualLshCanBeBought.estimatedReturns.toFixed(4)).toBe(estimatedLshCanBeBought.toFixed(4)); + expect(actualLshCanBeBought.status).toBe(estimatedStatus); +}); + + test.each` + sellerAmountInLshForSell | marketPriceInLsk | estimatedReturnsInLsk | buyerOrders | estimatedStatus + ${150} | ${0.78} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + ${160} | ${0.40} | ${94.5792} | ${bids} | ${EstimationStatus.MATCH} + ${142} | ${0.48} | ${85.2} | ${bids} | ${EstimationStatus.MATCH} + ${739.130434783} | ${0.23} | ${178.2073} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${947.368421053} | ${0.19} | ${188.2073} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${818.181818182} | ${0.22} | ${178.2073} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${327.868852459} | ${0.61} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + ${457.142857143} | ${0.35} | ${154.4369} | ${bids} | ${EstimationStatus.PARTIAL_MATCH} + ${1495.0166113} | ${0.602} | ${0} | ${bids} | ${EstimationStatus.NO_MATCH} + `('Should estimate best returns for {$sellerAmountInLshForSell} LSH based on {$marketPriceInLsk} LSK/LSH', ({ + buyerOrders, sellerAmountInLshForSell, marketPriceInLsk, estimatedReturnsInLsk, estimatedStatus, +}) => { + const actualEstimatedReturnsInLsk = estimateBestReturnsForSeller(sellerAmountInLshForSell, marketPriceInLsk, buyerOrders); expect(actualEstimatedReturnsInLsk.estimatedReturns.toFixed(4)).toBe(estimatedReturnsInLsk.toFixed(4)); expect(actualEstimatedReturnsInLsk.status).toBe(estimatedStatus); }); @@ -82,19 +121,19 @@ describe('Utils tests => ', () => { test.each` buyerAmountInLskForSell | marketPriceInLsk | estimatedLshCanBeBought | sellerOrders | estimatedStatus ${2000} | ${0.77} | ${3.1169} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} - ${2793.6} | ${0.96} | ${2910} | ${asks} | ${EstimationStatus.MATCH} - ${2821.5} | ${0.95} | ${2970} | ${asks} | ${EstimationStatus.MATCH} - ${2845.25} | ${0.95} | ${2995} | ${asks} | ${EstimationStatus.MATCH} - ${63.36} | ${0.88} | ${72} | ${asks} | ${EstimationStatus.MATCH} + ${2793.6} | ${0.96} | ${2996.5777} | ${asks} | ${EstimationStatus.MATCH} + ${2821.5} | ${0.95} | ${2999.7445} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} + ${2845.25} | ${0.95} | ${2999.7445} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} + ${63.36} | ${0.88} | ${72.9715} | ${asks} | ${EstimationStatus.MATCH} ${79.2} | ${0.88} | ${86.3001} | ${asks} | ${EstimationStatus.PARTIAL_MATCH} ${76.244} | ${0.76} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} ${78} | ${0.56} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} ${974.86} | ${0.20} | ${0} | ${asks} | ${EstimationStatus.NO_MATCH} - `('Should estimate $buyerAmountInLskForSell based on $marketPriceInLsk to $estimatedLshCanBeBought', ({ + `('Should estimate best returns for {$buyerAmountInLskForSell} LSK based on {$marketPriceInLsk} LSK/LSH', ({ sellerOrders, buyerAmountInLskForSell, marketPriceInLsk, estimatedLshCanBeBought, estimatedStatus, }) => { - const actualLshCanBeBought = estimatedReturnsForBuyer(buyerAmountInLskForSell, marketPriceInLsk, sellerOrders); - expect(actualLshCanBeBought.estimatedReturns).toBe(estimatedLshCanBeBought); + const actualLshCanBeBought = estimatedBestReturnsForBuyer(buyerAmountInLskForSell, marketPriceInLsk, sellerOrders); + expect(actualLshCanBeBought.estimatedReturns.toFixed(4)).toBe(estimatedLshCanBeBought.toFixed(4)); expect(actualLshCanBeBought.status).toBe(estimatedStatus); }); });