aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils
diff options
context:
space:
mode:
authorSteve Klebanoff <steve.klebanoff@gmail.com>2018-12-15 02:23:01 +0800
committerSteve Klebanoff <steve.klebanoff@gmail.com>2018-12-15 07:34:45 +0800
commita3d93d17cdefc2258a9f08e6fc680df1fb2b8456 (patch)
tree0f074b7205b4026b0e3b9bd84c9d8a1257411757 /packages/asset-buyer/src/utils
parentc6c45095a8511814db6aa33e39794ae60debad8b (diff)
downloaddexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar.gz
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar.bz2
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar.lz
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar.xz
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.tar.zst
dexon-sol-tools-a3d93d17cdefc2258a9f08e6fc680df1fb2b8456.zip
Factor in slippage amount in InsufficientAssetLiquidityError error, and show in instant
Diffstat (limited to 'packages/asset-buyer/src/utils')
-rw-r--r--packages/asset-buyer/src/utils/buy_quote_calculator.ts17
1 files changed, 15 insertions, 2 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
index e52c9c5bf..23d3e9b24 100644
--- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts
+++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
@@ -1,5 +1,6 @@
import { marketUtils, SignedOrder } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
+import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';
import { constants } from '../constants';
@@ -39,8 +40,20 @@ export const buyQuoteCalculator = {
});
// if we do not have enough orders to cover the desired assetBuyAmount, throw
if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
- const amountRemaining = assetBuyAmount.minus(remainingFillAmount);
- throw new InsufficientAssetLiquidityError(amountRemaining);
+ // We needed the amount they requested to buy, plus the amount for slippage
+ const totalAmountRequested = assetBuyAmount.plus(slippageBufferAmount);
+ const amountUnableToFill = totalAmountRequested.minus(remainingFillAmount);
+ // multiplerNeededWithSlippage represents what we need to multiply the assetBuyAmount by
+ // in order to get the total amount needed considering slippage
+ // i.e. if slippagePercent was 0.2 (20%), multiplerNeededWithSlippage would be 1.2
+ const multiplerNeededWithSlippage = new BigNumber(1).plus(slippagePercentage);
+ // Given amountAvailableToFillConsideringSlippage * multiplerNeededWithSlippage = amountUnableToFill
+ // We divide amountUnableToFill by multiplerNeededWithSlippage to determine amountAvailableToFillConsideringSlippage
+ const amountAvailableToFillConsideringSlippage = amountUnableToFill
+ .div(multiplerNeededWithSlippage)
+ .round(0, BigNumber.ROUND_DOWN);
+
+ throw new InsufficientAssetLiquidityError(amountAvailableToFillConsideringSlippage);
}
// if we are not buying ZRX:
// given the orders calculated above, find the fee-orders that cover the desired assetBuyAmount (with slippage)