diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-09-21 05:39:55 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-09-21 05:39:55 +0800 |
commit | fcca63a2dc17f7745304d02dfaffe60f0d6785c4 (patch) | |
tree | c5d9b41de38bc87911f2ac331d027728f8cb4d31 /packages | |
parent | 35c324f67c8022f3a71bc723bf69763bf651cb05 (diff) | |
download | dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar.gz dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar.bz2 dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar.lz dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar.xz dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.tar.zst dexon-sol-tools-fcca63a2dc17f7745304d02dfaffe60f0d6785c4.zip |
Move some logic to order utils
Diffstat (limited to 'packages')
-rw-r--r-- | packages/asset-buyer/src/utils/buy_quote_calculator.ts | 10 | ||||
-rw-r--r-- | packages/asset-buyer/src/utils/order_utils.ts | 14 |
2 files changed, 16 insertions, 8 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts index 31823cc02..aa7159e6c 100644 --- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts +++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts @@ -3,6 +3,7 @@ import { BigNumber } from '@0xproject/utils'; import * as _ from 'lodash'; import { constants } from '../constants'; +import { orderUtils } from './order_utils'; import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types'; export const buyQuoteCalculator = { @@ -30,7 +31,7 @@ export const buyQuoteCalculator = { if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) { throw new Error(AssetBuyerError.InsufficientAssetLiquidity); } - // TODO: optimization + // TODO(bmillman): optimization // 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 { @@ -59,10 +60,11 @@ export const buyQuoteCalculator = { let maxEthAmount = constants.ZERO_AMOUNT; let cumulativeMakerAmount = constants.ZERO_AMOUNT; _.forEach(allOrders, (order, index) => { - // TODO: Move this logic to order_utils const remainingFillableMakerAssetAmount = allRemainingAmounts[index]; - const orderRate = order.takerAssetAmount.div(order.makerAssetAmount); - const claimableTakerAssetAmount = orderRate.mul(remainingFillableMakerAssetAmount); + const claimableTakerAssetAmount = orderUtils.calculateRemainingTakerAssetAmount( + order, + remainingFillableMakerAssetAmount, + ); // taker asset is always assumed to be WETH maxEthAmount = maxEthAmount.plus(claimableTakerAssetAmount); if (cumulativeMakerAmount.lessThan(assetBuyAmount)) { diff --git a/packages/asset-buyer/src/utils/order_utils.ts b/packages/asset-buyer/src/utils/order_utils.ts index bb0bdb80f..27db73257 100644 --- a/packages/asset-buyer/src/utils/order_utils.ts +++ b/packages/asset-buyer/src/utils/order_utils.ts @@ -10,10 +10,16 @@ export const orderUtils = { return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec); }, calculateRemainingMakerAssetAmount(order: SignedOrder, remainingTakerAssetAmount: BigNumber): BigNumber { - const result = remainingTakerAssetAmount.eq(0) - ? constants.ZERO_AMOUNT - : remainingTakerAssetAmount.times(order.makerAssetAmount).dividedToIntegerBy(order.takerAssetAmount); - return result; + if (remainingTakerAssetAmount.eq(0)) { + return constants.ZERO_AMOUNT; + } + return remainingTakerAssetAmount.times(order.makerAssetAmount).dividedToIntegerBy(order.takerAssetAmount); + }, + calculateRemainingTakerAssetAmount(order: SignedOrder, remainingMakerAssetAmount: BigNumber): BigNumber { + if (remainingMakerAssetAmount.eq(0)) { + return constants.ZERO_AMOUNT; + } + return remainingMakerAssetAmount.times(order.takerAssetAmount).dividedToIntegerBy(order.makerAssetAmount); }, isOpenOrder(order: SignedOrder): boolean { return order.takerAddress === constants.NULL_ADDRESS; |