diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-10-03 01:17:09 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-10-03 04:54:57 +0800 |
commit | b3a868da0e77da04b123f4ff397d8f1a3a4d3810 (patch) | |
tree | 474af79684faee3cd5be938ca22b08264721390d /packages/asset-buyer/src/utils | |
parent | 04dd4ce6d1d0ca9703610579632d6989f9959277 (diff) | |
download | dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.gz dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.bz2 dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.lz dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.xz dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.tar.zst dexon-sol-tools-b3a868da0e77da04b123f4ff397d8f1a3a4d3810.zip |
Merge AssetBuyer and AssetBuyerManager
Diffstat (limited to 'packages/asset-buyer/src/utils')
-rw-r--r-- | packages/asset-buyer/src/utils/buy_quote_calculator.ts | 15 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/order_provider_response_processor.ts | 76 |
2 files changed, 30 insertions, 61 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index 9946924ef..b706ea143 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -3,24 +3,23 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { constants } from '../constants'; -import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types'; +import { AssetBuyerError, BuyQuote, OrdersAndFillableAmounts } from '../types'; import { orderUtils } from './order_utils'; // Calculates a buy quote for orders that have WETH as the takerAsset export const buyQuoteCalculator = { calculate( - ordersAndFillableAmounts: AssetBuyerOrdersAndFillableAmounts, + ordersAndFillableAmounts: OrdersAndFillableAmounts, + feeOrdersAndFillableAmounts: OrdersAndFillableAmounts, assetBuyAmount: BigNumber, feePercentage: number, slippagePercentage: number, ): BuyQuote { - const { - orders, - feeOrders, - remainingFillableMakerAssetAmounts, - remainingFillableFeeAmounts, - } = ordersAndFillableAmounts; + const orders = ordersAndFillableAmounts.orders; + const remainingFillableMakerAssetAmounts = ordersAndFillableAmounts.remainingFillableMakerAssetAmounts; + const feeOrders = feeOrdersAndFillableAmounts.orders; + const remainingFillableFeeAmounts = feeOrdersAndFillableAmounts.remainingFillableMakerAssetAmounts; const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round(); const { resultOrders, diff --git a/packages/asset-buyer/src/utils/order_provider_response_processor.ts b/packages/asset-buyer/src/utils/order_provider_response_processor.ts index 31fdcc182..74eec162d 100644 --- a/packages/asset-buyer/src/utils/order_provider_response_processor.ts +++ b/packages/asset-buyer/src/utils/order_provider_response_processor.ts @@ -8,19 +8,14 @@ import * as _ from 'lodash'; import { constants } from '../constants'; import { AssetBuyerError, - AssetBuyerOrdersAndFillableAmounts, OrderProviderRequest, OrderProviderResponse, + OrdersAndFillableAmounts, SignedOrderWithRemainingFillableMakerAssetAmount, } from '../types'; import { orderUtils } from './order_utils'; -interface OrdersAndRemainingFillableMakerAssetAmounts { - orders: SignedOrder[]; - remainingFillableMakerAssetAmounts: BigNumber[]; -} - export const orderProviderResponseProcessor = { throwIfInvalidResponse(response: OrderProviderResponse, request: OrderProviderRequest): void { const { makerAssetData, takerAssetData } = request; @@ -38,65 +33,40 @@ export const orderProviderResponseProcessor = { * - Sort by rate */ async processAsync( - targetOrderProviderResponse: OrderProviderResponse, - feeOrderProviderResponse: OrderProviderResponse, - zrxTokenAssetData: string, + orderProviderResponse: OrderProviderResponse, + isMakerAssetZrxToken: boolean, expiryBufferSeconds: number, orderValidator?: OrderValidatorWrapper, - ): Promise<AssetBuyerOrdersAndFillableAmounts> { + ): Promise<OrdersAndFillableAmounts> { // drop orders that are expired or not open - const filteredTargetOrders = filterOutExpiredAndNonOpenOrders( - targetOrderProviderResponse.orders, - expiryBufferSeconds, - ); - const filteredFeeOrders = filterOutExpiredAndNonOpenOrders( - feeOrderProviderResponse.orders, - expiryBufferSeconds, - ); + const filteredOrders = filterOutExpiredAndNonOpenOrders(orderProviderResponse.orders, expiryBufferSeconds); // set the orders to be sorted equal to the filtered orders - let unsortedTargetOrders = filteredTargetOrders; - let unsortedFeeOrders = filteredFeeOrders; + let unsortedOrders = filteredOrders; // if an orderValidator is provided, use on chain information to calculate remaining fillable makerAsset amounts if (!_.isUndefined(orderValidator)) { // TODO(bmillman): improvement - // try/catch these requests and throw a more domain specific error - // TODO(bmillman): optimization - // reduce this to once RPC call buy combining orders into one array and then splitting up the response - const [targetOrdersAndTradersInfo, feeOrdersAndTradersInfo] = await Promise.all( - _.map([filteredTargetOrders, filteredFeeOrders], ordersToBeValidated => { - const takerAddresses = _.map(ordersToBeValidated, () => constants.NULL_ADDRESS); - return orderValidator.getOrdersAndTradersInfoAsync(ordersToBeValidated, takerAddresses); - }), - ); - // take orders + on chain information and find the valid orders and remaining fillable maker asset amounts - unsortedTargetOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( - filteredTargetOrders, - targetOrdersAndTradersInfo, - zrxTokenAssetData, + // try/catch this request and throw a more domain specific error + const takerAddresses = _.map(filteredOrders, () => constants.NULL_ADDRESS); + const ordersAndTradersInfo = await orderValidator.getOrdersAndTradersInfoAsync( + filteredOrders, + takerAddresses, ); // take orders + on chain information and find the valid orders and remaining fillable maker asset amounts - unsortedFeeOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( - filteredFeeOrders, - feeOrdersAndTradersInfo, - zrxTokenAssetData, + unsortedOrders = getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( + filteredOrders, + ordersAndTradersInfo, + isMakerAssetZrxToken, ); } // sort orders by rate // TODO(bmillman): optimization // provide a feeRate to the sorting function to more accurately sort based on the current market for ZRX tokens - const sortedTargetOrders = sortingUtils.sortOrdersByFeeAdjustedRate(unsortedTargetOrders); - const sortedFeeOrders = sortingUtils.sortFeeOrdersByFeeAdjustedRate(unsortedFeeOrders); + const sortedOrders = isMakerAssetZrxToken + ? sortingUtils.sortFeeOrdersByFeeAdjustedRate(unsortedOrders) + : sortingUtils.sortOrdersByFeeAdjustedRate(unsortedOrders); // unbundle orders and fillable amounts and compile final result - const targetOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedTargetOrders); - const feeOrdersAndRemainingFillableMakerAssetAmounts = unbundleOrdersWithAmounts(sortedFeeOrders); - return { - orders: targetOrdersAndRemainingFillableMakerAssetAmounts.orders, - feeOrders: feeOrdersAndRemainingFillableMakerAssetAmounts.orders, - remainingFillableMakerAssetAmounts: - targetOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts, - remainingFillableFeeAmounts: - feeOrdersAndRemainingFillableMakerAssetAmounts.remainingFillableMakerAssetAmounts, - }; + const result = unbundleOrdersWithAmounts(sortedOrders); + return result; }, }; @@ -120,7 +90,7 @@ function filterOutExpiredAndNonOpenOrders( function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( inputOrders: SignedOrder[], ordersAndTradersInfo: OrderAndTraderInfo[], - zrxAssetData: string, + isMakerAssetZrxToken: boolean, ): SignedOrderWithRemainingFillableMakerAssetAmount[] { // iterate through the input orders and find the ones that are still fillable // for the orders that are still fillable, calculate the remaining fillable maker asset amount @@ -147,7 +117,7 @@ function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( const remainingFillableCalculator = new RemainingFillableCalculator( order.makerFee, order.makerAssetAmount, - order.makerAssetData === zrxAssetData, + isMakerAssetZrxToken, transferrableAssetAmount, transferrableFeeAssetAmount, remainingMakerAssetAmount, @@ -175,7 +145,7 @@ function getValidOrdersWithRemainingFillableMakerAssetAmountsFromOnChain( */ function unbundleOrdersWithAmounts( ordersWithAmounts: SignedOrderWithRemainingFillableMakerAssetAmount[], -): OrdersAndRemainingFillableMakerAssetAmounts { +): OrdersAndFillableAmounts { const result = _.reduce( ordersWithAmounts, (acc, orderWithAmount) => { |