diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-09-14 19:45:14 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-09-15 20:14:48 +0800 |
commit | 91702bbae214f56e7ff66156fa799fcfb83f6e42 (patch) | |
tree | c45fc0577a577b81abd3e6eb0b5c7897db8fbfb8 /packages/asset-buyer/src/forwarder_helper_impl.ts | |
parent | 90674d9038b45644e82545558d9937b9eec498f9 (diff) | |
download | dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar.gz dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar.bz2 dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar.lz dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar.xz dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.tar.zst dexon-sol-tools-91702bbae214f56e7ff66156fa799fcfb83f6e42.zip |
Move packages/forwarder-helper into packages/asset-buyer
Diffstat (limited to 'packages/asset-buyer/src/forwarder_helper_impl.ts')
-rw-r--r-- | packages/asset-buyer/src/forwarder_helper_impl.ts | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/packages/asset-buyer/src/forwarder_helper_impl.ts b/packages/asset-buyer/src/forwarder_helper_impl.ts new file mode 100644 index 000000000..a90edb0bb --- /dev/null +++ b/packages/asset-buyer/src/forwarder_helper_impl.ts @@ -0,0 +1,64 @@ +import { marketUtils } from '@0xproject/order-utils'; +import { SignedOrder } from '@0xproject/types'; +import { BigNumber } from '@0xproject/utils'; +import * as _ from 'lodash'; + +import { constants } from './constants'; +import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types'; +import { forwarderHelperImplConfigUtils } from './utils/forwarder_helper_impl_config_utils'; + +const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface + +export interface ForwarderHelperImplConfig { + orders: SignedOrder[]; + feeOrders: SignedOrder[]; + remainingFillableMakerAssetAmounts?: BigNumber[]; + remainingFillableFeeAmounts?: BigNumber[]; +} + +export class ForwarderHelperImpl implements ForwarderHelper { + public readonly config: ForwarderHelperImplConfig; + constructor(config: ForwarderHelperImplConfig) { + this.config = forwarderHelperImplConfigUtils.sortedConfig(config); + } + public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo { + const { makerAssetFillAmount, feePercentage } = request; + const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this.config; + // TODO: make the slippage percentage customizable + const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE).round(); + const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( + orders, + makerAssetFillAmount, + { + remainingFillableMakerAssetAmounts, + slippageBufferAmount, + }, + ); + if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { + 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 + const { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders( + resultOrders, + feeOrders, + { + remainingFillableMakerAssetAmounts, + remainingFillableFeeAmounts, + }, + ); + if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) { + throw new Error(ForwarderHelperError.InsufficientZrxLiquidity); + } + // TODO: calculate min and max eth usage + // TODO: optimize orders call data + return { + makerAssetFillAmount, + orders: resultOrders, + feeOrders: resultFeeOrders, + minEthAmount: constants.ZERO_AMOUNT, + maxEthAmount: constants.ZERO_AMOUNT, + feePercentage, + }; + } +} |