Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1437 from 0xProject/feature/instant/tell-amount-a…
Browse files Browse the repository at this point in the history
…vailable

[instant] Tell user how much of an asset is available
  • Loading branch information
Steve Klebanoff authored Jan 10, 2019
2 parents 87c287a + fb36050 commit 6487fae
Show file tree
Hide file tree
Showing 12 changed files with 273 additions and 17 deletions.
9 changes: 9 additions & 0 deletions packages/asset-buyer/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "4.0.0",
"changes": [
{
"note": "Raise custom InsufficientAssetLiquidityError error with amountAvailableToFill attribute",
"pr": 1437
}
]
},
{
"timestamp": 1547040760,
"version": "3.0.5",
Expand Down
22 changes: 22 additions & 0 deletions packages/asset-buyer/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BigNumber } from '@0x/utils';

import { AssetBuyerError } from './types';

/**
* Error class representing insufficient asset liquidity
*/
export class InsufficientAssetLiquidityError extends Error {
/**
* The amount availabe to fill (in base units) factoring in slippage.
*/
public amountAvailableToFill: BigNumber;
/**
* @param amountAvailableToFill The amount availabe to fill (in base units) factoring in slippage
*/
constructor(amountAvailableToFill: BigNumber) {
super(AssetBuyerError.InsufficientAssetLiquidity);
this.amountAvailableToFill = amountAvailableToFill;
// Setting prototype so instanceof works. See https://github.com/Microsoft/TypeScript/wiki/Breaking-Changes#extending-built-ins-like-error-array-and-map-may-no-longer-work
Object.setPrototypeOf(this, InsufficientAssetLiquidityError.prototype);
}
}
1 change: 1 addition & 0 deletions packages/asset-buyer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { SignedOrder } from '@0x/types';
export { BigNumber } from '@0x/utils';

export { AssetBuyer } from './asset_buyer';
export { InsufficientAssetLiquidityError } from './errors';
export { BasicOrderProvider } from './order_providers/basic_order_provider';
export { StandardRelayerAPIOrderProvider } from './order_providers/standard_relayer_api_order_provider';
export {
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-buyer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export interface AssetBuyerOpts {
}

/**
* Possible errors thrown by an AssetBuyer instance or associated static methods.
* Possible error messages thrown by an AssetBuyer instance or associated static methods.
*/
export enum AssetBuyerError {
NoEtherTokenContractFound = 'NO_ETHER_TOKEN_CONTRACT_FOUND',
Expand Down
14 changes: 13 additions & 1 deletion packages/asset-buyer/src/utils/buy_quote_calculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
Expand Down
143 changes: 134 additions & 9 deletions packages/asset-buyer/test/buy_quote_calculator_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { orderFactory } from '@0x/order-utils/lib/src/order_factory';
import { SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import * as _ from 'lodash';
Expand All @@ -8,13 +9,18 @@ import { AssetBuyerError, OrdersAndFillableAmounts } from '../src/types';
import { buyQuoteCalculator } from '../src/utils/buy_quote_calculator';

import { chaiSetup } from './utils/chai_setup';
import { testHelpers } from './utils/test_helpers';

chaiSetup.configure();
const expect = chai.expect;

// tslint:disable:custom-no-magic-numbers
describe('buyQuoteCalculator', () => {
describe('#calculate', () => {
let firstOrder: SignedOrder;
let firstRemainingFillAmount: BigNumber;
let secondOrder: SignedOrder;
let secondRemainingFillAmount: BigNumber;
let ordersAndFillableAmounts: OrdersAndFillableAmounts;
let smallFeeOrderAndFillableAmount: OrdersAndFillableAmounts;
let allFeeOrdersAndFillableAmounts: OrdersAndFillableAmounts;
Expand All @@ -24,18 +30,18 @@ describe('buyQuoteCalculator', () => {
// the second order has a rate of 2 makerAsset / WETH with a takerFee of 100 ZRX and has 200 / 200 makerAsset units left to fill (completely fillable)
// generate one order for fees
// the fee order has a rate of 1 ZRX / WETH with no taker fee and has 100 ZRX left to fill (completely fillable)
const firstOrder = orderFactory.createSignedOrderFromPartial({
firstOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(400),
takerAssetAmount: new BigNumber(100),
takerFee: new BigNumber(200),
});
const firstRemainingFillAmount = new BigNumber(200);
const secondOrder = orderFactory.createSignedOrderFromPartial({
firstRemainingFillAmount = new BigNumber(200);
secondOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(200),
takerAssetAmount: new BigNumber(100),
takerFee: new BigNumber(100),
});
const secondRemainingFillAmount = secondOrder.makerAssetAmount;
secondRemainingFillAmount = secondOrder.makerAssetAmount;
ordersAndFillableAmounts = {
orders: [firstOrder, secondOrder],
remainingFillableMakerAssetAmounts: [firstRemainingFillAmount, secondRemainingFillAmount],
Expand All @@ -61,18 +67,137 @@ describe('buyQuoteCalculator', () => {
],
};
});
it('should throw if not enough maker asset liquidity', () => {
// we have 400 makerAsset units available to fill but attempt to calculate a quote for 500 makerAsset units
describe('InsufficientLiquidityError', () => {
it('should throw if not enough maker asset liquidity (multiple orders)', () => {
// we have 400 makerAsset units available to fill but attempt to calculate a quote for 500 makerAsset units
const errorFunction = () => {
buyQuoteCalculator.calculate(
ordersAndFillableAmounts,
smallFeeOrderAndFillableAmount,
new BigNumber(500),
0,
0,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(400));
});
it('should throw if not enough maker asset liquidity (multiple orders with 20% slippage)', () => {
// we have 400 makerAsset units available to fill but attempt to calculate a quote for 500 makerAsset units
const errorFunction = () => {
buyQuoteCalculator.calculate(
ordersAndFillableAmounts,
smallFeeOrderAndFillableAmount,
new BigNumber(500),
0,
0.2,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(333));
});
it('should throw if not enough maker asset liquidity (multiple orders with 5% slippage)', () => {
// we have 400 makerAsset units available to fill but attempt to calculate a quote for 500 makerAsset units
const errorFunction = () => {
buyQuoteCalculator.calculate(
ordersAndFillableAmounts,
smallFeeOrderAndFillableAmount,
new BigNumber(600),
0,
0.05,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(380));
});
it('should throw if not enough maker asset liquidity (partially filled order)', () => {
const firstOrderAndFillableAmount: OrdersAndFillableAmounts = {
orders: [firstOrder],
remainingFillableMakerAssetAmounts: [firstRemainingFillAmount],
};

const errorFunction = () => {
buyQuoteCalculator.calculate(
firstOrderAndFillableAmount,
smallFeeOrderAndFillableAmount,
new BigNumber(201),
0,
0,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(200));
});
it('should throw if not enough maker asset liquidity (completely fillable order)', () => {
const completelyFillableOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(123),
takerAssetAmount: new BigNumber(100),
takerFee: new BigNumber(200),
});
const completelyFillableOrdersAndFillableAmount: OrdersAndFillableAmounts = {
orders: [completelyFillableOrder],
remainingFillableMakerAssetAmounts: [completelyFillableOrder.makerAssetAmount],
};
const errorFunction = () => {
buyQuoteCalculator.calculate(
completelyFillableOrdersAndFillableAmount,
smallFeeOrderAndFillableAmount,
new BigNumber(124),
0,
0,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(123));
});
it('should throw with 1 amount available if no slippage', () => {
const smallOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(1),
takerAssetAmount: new BigNumber(1),
takerFee: new BigNumber(0),
});
const errorFunction = () => {
buyQuoteCalculator.calculate(
{ orders: [smallOrder], remainingFillableMakerAssetAmounts: [smallOrder.makerAssetAmount] },
smallFeeOrderAndFillableAmount,
new BigNumber(600),
0,
0,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, new BigNumber(1));
});
it('should throw without amount available to fill if amount rounds to 0', () => {
const smallOrder = orderFactory.createSignedOrderFromPartial({
makerAssetAmount: new BigNumber(1),
takerAssetAmount: new BigNumber(1),
takerFee: new BigNumber(0),
});
const errorFunction = () => {
buyQuoteCalculator.calculate(
{ orders: [smallOrder], remainingFillableMakerAssetAmounts: [smallOrder.makerAssetAmount] },
smallFeeOrderAndFillableAmount,
new BigNumber(600),
0,
0.2,
false,
);
};
testHelpers.expectInsufficientLiquidityError(expect, errorFunction, undefined);
});
});
it('should not throw if order is fillable', () => {
expect(() =>
buyQuoteCalculator.calculate(
ordersAndFillableAmounts,
smallFeeOrderAndFillableAmount,
new BigNumber(500),
allFeeOrdersAndFillableAmounts,
new BigNumber(300),
0,
0,
false,
),
).to.throw(AssetBuyerError.InsufficientAssetLiquidity);
).to.not.throw();
});
it('should throw if not enough ZRX liquidity', () => {
// we request 300 makerAsset units but the ZRX order is only enough to fill the first order, which only has 200 makerAssetUnits available
Expand Down
26 changes: 26 additions & 0 deletions packages/asset-buyer/test/utils/test_helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { BigNumber } from '@0x/utils';

import { InsufficientAssetLiquidityError } from '../../src/errors';

export const testHelpers = {
expectInsufficientLiquidityError: (
expect: Chai.ExpectStatic,
functionWhichTriggersError: () => void,
expectedAmountAvailableToFill?: BigNumber,
): void => {
let wasErrorThrown = false;
try {
functionWhichTriggersError();
} catch (e) {
wasErrorThrown = true;
expect(e).to.be.instanceOf(InsufficientAssetLiquidityError);
if (expectedAmountAvailableToFill) {
expect(e.amountAvailableToFill).to.be.bignumber.equal(expectedAmountAvailableToFill);
} else {
expect(e.amountAvailableToFill).to.be.undefined();
}
}

expect(wasErrorThrown).to.be.true();
},
};
1 change: 1 addition & 0 deletions packages/instant/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const ONE_SECOND_MS = 1000;
export const ONE_MINUTE_MS = ONE_SECOND_MS * 60;
export const GIT_SHA = process.env.GIT_SHA;
export const NODE_ENV = process.env.NODE_ENV;
export const SLIPPAGE_PERCENTAGE = 0.2;
export const NPM_PACKAGE_VERSION = process.env.NPM_PACKAGE_VERSION;
export const DEFAULT_UNKOWN_ASSET_NAME = '???';
export const ACCOUNT_UPDATE_INTERVAL_TIME_MS = ONE_SECOND_MS * 5;
Expand Down
21 changes: 19 additions & 2 deletions packages/instant/src/util/asset.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AssetBuyerError } from '@0x/asset-buyer';
import { AssetBuyerError, InsufficientAssetLiquidityError } from '@0x/asset-buyer';
import { AssetProxyId, ObjectMap } from '@0x/types';
import { BigNumber } from '@0x/utils';
import { Web3Wrapper } from '@0x/web3-wrapper';
import * as _ from 'lodash';

import { DEFAULT_UNKOWN_ASSET_NAME } from '../constants';
import { BIG_NUMBER_ZERO, DEFAULT_UNKOWN_ASSET_NAME } from '../constants';
import { assetDataNetworkMapping } from '../data/asset_data_network_mapping';
import { Asset, AssetMetaData, ERC20Asset, Network, ZeroExInstantError } from '../types';

Expand Down Expand Up @@ -111,6 +113,21 @@ export const assetUtils = {
assetBuyerErrorMessage: (asset: ERC20Asset, error: Error): string | undefined => {
if (error.message === AssetBuyerError.InsufficientAssetLiquidity) {
const assetName = assetUtils.bestNameForAsset(asset, 'of this asset');
if (
error instanceof InsufficientAssetLiquidityError &&
error.amountAvailableToFill.greaterThan(BIG_NUMBER_ZERO)
) {
const unitAmountAvailableToFill = Web3Wrapper.toUnitAmount(
error.amountAvailableToFill,
asset.metaData.decimals,
);
const roundedUnitAmountAvailableToFill = unitAmountAvailableToFill.round(2, BigNumber.ROUND_DOWN);

if (roundedUnitAmountAvailableToFill.greaterThan(BIG_NUMBER_ZERO)) {
return `There are only ${roundedUnitAmountAvailableToFill} ${assetName} available to buy`;
}
}

return `Not enough ${assetName} available`;
} else if (error.message === AssetBuyerError.InsufficientZrxLiquidity) {
return 'Not enough ZRX available';
Expand Down
7 changes: 6 additions & 1 deletion packages/instant/src/util/buy_quote_updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as _ from 'lodash';
import { Dispatch } from 'redux';
import { oc } from 'ts-optchain';

import { SLIPPAGE_PERCENTAGE } from '../constants';
import { Action, actions } from '../redux/actions';
import { AffiliateInfo, ERC20Asset, QuoteFetchOrigin } from '../types';
import { analytics } from '../util/analytics';
Expand Down Expand Up @@ -33,8 +34,12 @@ export const buyQuoteUpdater = {
}
const feePercentage = oc(options.affiliateInfo).feePercentage();
let newBuyQuote: BuyQuote | undefined;
const slippagePercentage = SLIPPAGE_PERCENTAGE;
try {
newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue, { feePercentage });
newBuyQuote = await assetBuyer.getBuyQuoteAsync(asset.assetData, baseUnitValue, {
feePercentage,
slippagePercentage,
});
} catch (error) {
const errorMessage = assetUtils.assetBuyerErrorMessage(asset, error);

Expand Down
Loading

0 comments on commit 6487fae

Please sign in to comment.