diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-08-17 15:15:52 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-08-23 06:39:03 +0800 |
commit | 3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7 (patch) | |
tree | b53c5e88376bc6cfc52c8d20b645f9c7ceba1812 /packages/forwarder-helper/src/forwarder_helper_impl.ts | |
parent | 2ef867f398db381ebd8ec715bd108b46d8fa60f3 (diff) | |
download | dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.gz dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.tar.zst dexon-sol-tools-3c973ba9f64197fcc1a66f319e3d1aa33d96b6d7.zip |
Remove marketSell and add to marketBuy implementation
Diffstat (limited to 'packages/forwarder-helper/src/forwarder_helper_impl.ts')
-rw-r--r-- | packages/forwarder-helper/src/forwarder_helper_impl.ts | 60 |
1 files changed, 37 insertions, 23 deletions
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, }; } |