From 3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7 Mon Sep 17 00:00:00 2001 From: Brandon Millman Date: Fri, 17 Aug 2018 00:15:52 -0700 Subject: Remove marketSell and add to marketBuy implementation --- packages/forwarder-helper/src/constants.ts | 5 ++ .../forwarder-helper/src/forwarder_helper_impl.ts | 60 +++++++++++++--------- packages/forwarder-helper/src/types.ts | 37 ------------- 3 files changed, 42 insertions(+), 60 deletions(-) create mode 100644 packages/forwarder-helper/src/constants.ts (limited to 'packages/forwarder-helper') diff --git a/packages/forwarder-helper/src/constants.ts b/packages/forwarder-helper/src/constants.ts new file mode 100644 index 000000000..0ad30e4c0 --- /dev/null +++ b/packages/forwarder-helper/src/constants.ts @@ -0,0 +1,5 @@ +import { BigNumber } from '@0xproject/utils'; + +export const constants = { + ZERO_AMOUNT: new BigNumber(0), +}; diff --git a/packages/forwarder-helper/src/forwarder_helper_impl.ts b/packages/forwarder-helper/src/forwarder_helper_impl.ts index 1069bca73..0d03c9f76 100644 --- a/packages/forwarder-helper/src/forwarder_helper_impl.ts +++ b/packages/forwarder-helper/src/forwarder_helper_impl.ts @@ -1,13 +1,11 @@ +import { marketUtils } from '@0xproject/order-utils'; import { SignedOrder } from '@0xproject/types'; import { BigNumber } from '@0xproject/utils'; -import { - ForwarderHelper, - MarketBuyOrdersInfo, - MarketBuyOrdersInfoRequest, - MarketSellOrdersInfo, - MarketSellOrdersInfoRequest, -} from './types'; +import { constants } from './constants'; +import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types'; + +const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface export class ForwarderHelperImpl implements ForwarderHelper { private _orders: SignedOrder[]; @@ -26,24 +24,40 @@ export class ForwarderHelperImpl implements ForwarderHelper { this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts; } public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { - const { makerAssetFillAmount, feePercentage, acceptableEthAmountRange } = request; - return { + const { makerAssetFillAmount, feePercentage } = request; + // TODO: make the slippage percentage customizable + const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE); + const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( + this._orders, makerAssetFillAmount, - orders: this._orders, - feeOrders: this._feeOrders, - minEthAmount: new BigNumber(0), - maxEthAmount: new BigNumber(0), - feePercentage, - }; - } - public getMarketSellOrdersInfo(request: MarketSellOrdersInfoRequest): MarketSellOrdersInfo { - const { ethAmount, feePercentage, acceptableFillAmountRange } = request; + { + remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, + slippageBufferAmount, + }, + ); + if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(ForwarderHelperError.InsufficientLiquidity); + } + // 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 + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + resultOrders, + this._feeOrders, + { + remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists, + remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists, + }, + ); + if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(ForwarderHelperError.InsufficientZrxLiquidity); + } + // TODO: calculate min and max eth usage return { - ethAmount, - orders: this._orders, - feeOrders: this._feeOrders, - minFillAmount: new BigNumber(0), - maxFillAmount: new BigNumber(0), + makerAssetFillAmount, + orders: resultOrders, + feeOrders: resultFeeOrders, + minEthAmount: constants.ZERO_AMOUNT, + maxEthAmount: constants.ZERO_AMOUNT, feePercentage, }; } diff --git a/packages/forwarder-helper/src/types.ts b/packages/forwarder-helper/src/types.ts index c6d4083a9..084c3303e 100644 --- a/packages/forwarder-helper/src/types.ts +++ b/packages/forwarder-helper/src/types.ts @@ -9,13 +9,6 @@ export interface ForwarderHelper { * @return An object that conforms to MarketBuyOrdersInfo that satisfies the request. See type definition for more information. */ getMarketBuyOrdersInfo: (request: MarketBuyOrdersInfoRequest) => MarketBuyOrdersInfo; - /** - * Given a MarketSellOrdersInfoRequest, returns a MarketSellOrdersInfo containing all information relevant to fulfilling the request - * using the ForwarderContract marketSellOrdersWithEth function. - * @param request An object that conforms to MarketSellOrdersInfoRequest. See type definition for more information. - * @return An object that conforms to MarketSellOrdersInfo that satisfies the request. See type definition for more information. - */ - getMarketSellOrdersInfo: (request: MarketSellOrdersInfoRequest) => MarketSellOrdersInfo; } export enum ForwarderHelperError { @@ -26,12 +19,10 @@ export enum ForwarderHelperError { /** * makerAssetFillAmount: The amount of makerAsset requesting to be filled * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations - * acceptableEthAmountRange: maximum difference between min and max eth cost */ export interface MarketBuyOrdersInfoRequest { makerAssetFillAmount: BigNumber; feePercentage?: BigNumber; - acceptableEthAmountRange?: BigNumber; } /** @@ -50,31 +41,3 @@ export interface MarketBuyOrdersInfo { maxEthAmount: BigNumber; feePercentage?: BigNumber; } - -/** - * ethAmount: The amount of eth used to fill - * feePercentage: Optional affiliate percentage amount factoring into eth amount calculations - * acceptableFillAmountRange: maximum difference between min and max asset filled - */ -export interface MarketSellOrdersInfoRequest { - ethAmount: BigNumber; - feePercentage?: BigNumber; - acceptableFillAmountRange?: BigNumber; -} - -/** - * ethAmount: The amount of eth used to fill - * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested ethAmount plus slippage - * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above - * minFillAmount: Amount of asset purchased in the worst case - * maxFillAmount: Amount of asset purchased in the best case - * feePercentage: Affiliate fee percentage used to calculate the eth amounts above. Passed thru directly from the request - */ -export interface MarketSellOrdersInfo { - ethAmount: BigNumber; - orders: SignedOrder[]; - feeOrders: SignedOrder[]; - minFillAmount: BigNumber; - maxFillAmount: BigNumber; - feePercentage?: BigNumber; -} -- cgit