From 68dfd1bb22c1d6d33a46ca1eff5fa6f62cde62ed Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Tue, 21 Aug 2018 17:08:44 -0700 Subject: Add tests for getMarketBuyOrdersInfo --- .../forwarder-helper/src/forwarder_helper_impl.ts | 4 +- packages/forwarder-helper/src/types.ts | 2 +- .../test/forwarder_helper_impl_test.ts | 57 +++++++++++++++++++--- 3 files changed, 52 insertions(+), 11 deletions(-) (limited to 'packages') diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 43b2962da..123c925f1 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -83,7 +83,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { const { makerAssetFillAmount, feePercentage } = request; const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this.config; // TODO: make the slippage percentage customizable - const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); + const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE).round(); const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( orders, makerAssetFillAmount, @@ -93,7 +93,7 @@ export class ForwarderHelperImpl implements ForwarderHelper { }, ); if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { - throw new Error(ForwarderHelperError.InsufficientLiquidity); + throw new Error(ForwarderHelperError.InsufficientMakerAssetLiquidity); } // TODO: update this logic to find the minimum amount of feeOrders to cover the worst case as opposed to // finding order that cover all fees, this will help with estimating ETH and minimizing gas usage diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index 084c3303e..fb171cc90 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -12,7 +12,7 @@ export interface ForwarderHelper { } export enum ForwarderHelperError { - InsufficientLiquidity = 'INSUFFICIENT_LIQUIDITY', + InsufficientMakerAssetLiquidity = 'INSUFFICIENT_MAKER_ASSET_LIQUIDITY', InsufficientZrxLiquidity = 'INSUFFICIENT_ZRX_LIQUIDITY', } diff --git a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts index 150ba5f0b..3e2667a01 100644 --- a/packages/forwarder-helper/test/forwarder_helper_impl_test.ts +++ b/packages/forwarder-helper/test/forwarder_helper_impl_test.ts @@ -5,6 +5,7 @@ import * as _ from 'lodash'; import 'mocha'; import { ForwarderHelperImpl, ForwarderHelperImplConfig } from '../src/forwarder_helper_impl'; +import { ForwarderHelperError } from '../src/types'; import { chaiSetup } from './utils/chai_setup'; @@ -26,6 +27,7 @@ describe('ForwarderHelperImpl', () => { const testOrder3 = testOrderFactory.generateTestSignedOrder({ makerAssetAmount: new BigNumber(100), takerAssetAmount: new BigNumber(300), + takerFee: new BigNumber(1), }); // rate: 3 WETH / ZRX const testFeeOrder1 = testOrderFactory.generateTestSignedOrder({ @@ -42,13 +44,13 @@ describe('ForwarderHelperImpl', () => { makerAssetAmount: new BigNumber(100), takerAssetAmount: new BigNumber(100), }); + const inputForwarderHelperConfig: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], + remainingFillableMakerAssetAmounts: [new BigNumber(1), new BigNumber(2), new BigNumber(3)], + remainingFillableFeeAmounts: [new BigNumber(4), new BigNumber(5), new BigNumber(6)], + }; describe('#constructor', () => { - const inputForwarderHelperConfig: ForwarderHelperImplConfig = { - orders: [testOrder1, testOrder2, testOrder3], - feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], - remainingFillableMakerAssetAmounts: [new BigNumber(1), new BigNumber(2), new BigNumber(3)], - remainingFillableFeeAmounts: [new BigNumber(4), new BigNumber(5), new BigNumber(6)], - }; const inputForwarderHelperConfigNoRemainingAmounts: ForwarderHelperImplConfig = { orders: [testOrder1, testOrder2, testOrder3], feeOrders: [testFeeOrder1, testFeeOrder2, testFeeOrder3], @@ -90,6 +92,45 @@ describe('ForwarderHelperImpl', () => { expect(forwarderHelper.config.remainingFillableFeeAmounts).to.be.undefined(); }); }); - // describe('#getMarketBuyOrdersInfo', () => {}); - // describe('#getMarketSellOrdersInfo', () => {}); + describe('#getMarketBuyOrdersInfo', () => { + it('throws if not enough makerAsset liquidity', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + expect(() => { + // request for 6 makerAsset units, because we have exactly 6 available we should throw because there is a built in slippage buffer + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(6), + }); + }).to.throw(ForwarderHelperError.InsufficientMakerAssetLiquidity); + }); + it('throws if not enough ZRX liquidity', () => { + const inputForwarderHelperConfigNoFees: ForwarderHelperImplConfig = { + orders: [testOrder1, testOrder2, testOrder3], + feeOrders: [], + }; + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfigNoFees); + expect(() => { + // request for 4 makerAsset units, we need fees but no fee orders exist, show we should throw + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(250), + }); + }).to.throw(ForwarderHelperError.InsufficientZrxLiquidity); + }); + it('passes the makerAssetFillAmount from the request to the info response', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + const makerAssetFillAmount = new BigNumber(4); + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount, + }); + expect(info.makerAssetFillAmount).to.bignumber.equal(makerAssetFillAmount); + }); + it('passes the feePercentage from the request to the info response', () => { + const forwarderHelper = new ForwarderHelperImpl(inputForwarderHelperConfig); + const feePercentage = new BigNumber(0.2); + const info = forwarderHelper.getMarketBuyOrdersInfo({ + makerAssetFillAmount: new BigNumber(4), + feePercentage, + }); + expect(info.feePercentage).to.bignumber.equal(feePercentage); + }); + }); }); -- cgit v1.2.3