Skip to content

Commit

Permalink
[updated bestReturns utils and tests for the same][#3]
Browse files Browse the repository at this point in the history
  • Loading branch information
sacOO7 committed May 4, 2020
1 parent 091b781 commit 725b7bf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
8 changes: 6 additions & 2 deletions lisk-dex-electron/src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand Down
79 changes: 59 additions & 20 deletions lisk-dex-electron/src/tests/Utils.test.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -61,40 +62,78 @@ 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);
});

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);
});
});

0 comments on commit 725b7bf

Please sign in to comment.