From c6c45095a8511814db6aa33e39794ae60debad8b Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 13 Dec 2018 15:16:51 -0800 Subject: feat(asset-buyer): Custom InsufficientAssetLiquidityError error BREAKING CHANGE: A custom InsufficientAssetLiquidityError error is now raised when there is insufficient liquidity --- packages/asset-buyer/src/utils/buy_quote_calculator.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'packages/asset-buyer/src/utils') diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index b15b880c2..e52c9c5bf 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -3,7 +3,13 @@ import { BigNumber } from '@0x/utils'; import * as _ from 'lodash'; import { constants } from '../constants'; -import { AssetBuyerError, BuyQuote, BuyQuoteInfo, OrdersAndFillableAmounts } from '../types'; +import { + AssetBuyerError, + BuyQuote, + BuyQuoteInfo, + InsufficientAssetLiquidityError, + OrdersAndFillableAmounts, +} from '../types'; import { orderUtils } from './order_utils'; @@ -33,7 +39,8 @@ 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); + const amountRemaining = assetBuyAmount.minus(remainingFillAmount); + throw new InsufficientAssetLiquidityError(amountRemaining); } // if we are not buying ZRX: // given the orders calculated above, find the fee-orders that cover the desired assetBuyAmount (with slippage) -- cgit v1.2.3 From a3d93d17cdefc2258a9f08e6fc680df1fb2b8456 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 14 Dec 2018 10:23:01 -0800 Subject: Factor in slippage amount in InsufficientAssetLiquidityError error, and show in instant --- packages/asset-buyer/src/utils/buy_quote_calculator.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'packages/asset-buyer/src/utils') 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) -- cgit v1.2.3 From 69054d85e80f9e41200015a9b0eef2a9fe00f439 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 14 Dec 2018 15:18:20 -0800 Subject: Only send in amountAvailableToFill if it's a non-zero amount, add additional tests and nest, and put error into its own file --- packages/asset-buyer/src/utils/buy_quote_calculator.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'packages/asset-buyer/src/utils') diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index 23d3e9b24..59293d1b7 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -1,16 +1,10 @@ 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'; -import { - AssetBuyerError, - BuyQuote, - BuyQuoteInfo, - InsufficientAssetLiquidityError, - OrdersAndFillableAmounts, -} from '../types'; +import { InsufficientAssetLiquidityError } from '../errors'; +import { AssetBuyerError, BuyQuote, BuyQuoteInfo, OrdersAndFillableAmounts } from '../types'; import { orderUtils } from './order_utils'; @@ -53,7 +47,11 @@ export const buyQuoteCalculator = { .div(multiplerNeededWithSlippage) .round(0, BigNumber.ROUND_DOWN); - throw new InsufficientAssetLiquidityError(amountAvailableToFillConsideringSlippage); + throw new InsufficientAssetLiquidityError( + amountAvailableToFillConsideringSlippage.gt(constants.ZERO_AMOUNT) + ? amountAvailableToFillConsideringSlippage + : undefined, + ); } // if we are not buying ZRX: // given the orders calculated above, find the fee-orders that cover the desired assetBuyAmount (with slippage) -- cgit v1.2.3 From c8c8219c055cc5798cf5cdc71199ee7ae505cd5a Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 21 Dec 2018 16:26:07 -0800 Subject: Make amountAvailableToFill required --- packages/asset-buyer/src/utils/buy_quote_calculator.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'packages/asset-buyer/src/utils') diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index 59293d1b7..ceeee93d3 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -47,11 +47,7 @@ export const buyQuoteCalculator = { .div(multiplerNeededWithSlippage) .round(0, BigNumber.ROUND_DOWN); - throw new InsufficientAssetLiquidityError( - amountAvailableToFillConsideringSlippage.gt(constants.ZERO_AMOUNT) - ? amountAvailableToFillConsideringSlippage - : undefined, - ); + 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) -- cgit v1.2.3 From 26977f6408442883c20001a57a3b9001f4ab9b25 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 21 Dec 2018 16:29:53 -0800 Subject: Fix var name and use floor instead of .round(0, ROUND_DOWN) --- packages/asset-buyer/src/utils/buy_quote_calculator.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) (limited to 'packages/asset-buyer/src/utils') diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index ceeee93d3..fcded6ab1 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -36,16 +36,14 @@ export const buyQuoteCalculator = { if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { // 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 + 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%), 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); + // 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); } -- cgit v1.2.3