aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/test/buy_quote_calculator_test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/asset-buyer/test/buy_quote_calculator_test.ts')
-rw-r--r--packages/asset-buyer/test/buy_quote_calculator_test.ts143
1 files changed, 134 insertions, 9 deletions
diff --git a/packages/asset-buyer/test/buy_quote_calculator_test.ts b/packages/asset-buyer/test/buy_quote_calculator_test.ts
index a30017b72..fdc17ef25 100644
--- a/packages/asset-buyer/test/buy_quote_calculator_test.ts
+++ b/packages/asset-buyer/test/buy_quote_calculator_test.ts
@@ -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';
@@ -8,6 +9,7 @@ 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;
@@ -15,6 +17,10 @@ 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;
@@ -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],
@@ -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