aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/buy_quote_calculator.ts
diff options
context:
space:
mode:
authorSteve Klebanoff <steve@0xproject.com>2019-01-10 08:29:11 +0800
committerGitHub <noreply@github.com>2019-01-10 08:29:11 +0800
commit6487fae1131eb6e9215c918f23bac317157f368b (patch)
treebc02d4b3b0b556b2e911f4ba2c60f4035fdd717e /packages/asset-buyer/src/utils/buy_quote_calculator.ts
parent87c287a5e26e89ee25dd43793415d00d4ddfd5fa (diff)
parentfb3605026ef63ef6897010a52bf3f4c116cdf271 (diff)
downloaddexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar.gz
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar.bz2
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar.lz
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar.xz
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.tar.zst
dexon-sol-tools-6487fae1131eb6e9215c918f23bac317157f368b.zip
Merge pull request #1437 from 0xProject/feature/instant/tell-amount-available
[instant] Tell user how much of an asset is available
Diffstat (limited to 'packages/asset-buyer/src/utils/buy_quote_calculator.ts')
-rw-r--r--packages/asset-buyer/src/utils/buy_quote_calculator.ts14
1 files changed, 13 insertions, 1 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
index b15b880c2..fcded6ab1 100644
--- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts
+++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
@@ -3,6 +3,7 @@ import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { constants } from '../constants';
+import { InsufficientAssetLiquidityError } from '../errors';
import { AssetBuyerError, BuyQuote, BuyQuoteInfo, OrdersAndFillableAmounts } from '../types';
import { orderUtils } from './order_utils';
@@ -33,7 +34,18 @@ export const buyQuoteCalculator = {
});
// if we do not have enough orders to cover the desired assetBuyAmount, throw
if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
- throw new Error(AssetBuyerError.InsufficientAssetLiquidity);
+ // We needed the amount they requested to buy, plus the amount for slippage
+ const totalAmountRequested = assetBuyAmount.plus(slippageBufferAmount);
+ const amountAbleToFill = totalAmountRequested.minus(remainingFillAmount);
+ // multiplierNeededWithSlippage 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%), multiplierNeededWithSlippage would be 1.2
+ const multiplierNeededWithSlippage = new BigNumber(1).plus(slippagePercentage);
+ // Given amountAvailableToFillConsideringSlippage * multiplierNeededWithSlippage = amountAbleToFill
+ // We divide amountUnableToFill by multiplierNeededWithSlippage to determine amountAvailableToFillConsideringSlippage
+ const amountAvailableToFillConsideringSlippage = amountAbleToFill.div(multiplierNeededWithSlippage).floor();
+
+ 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)