diff options
author | fragosti <francesco.agosti93@gmail.com> | 2018-09-19 21:58:30 +0800 |
---|---|---|
committer | fragosti <francesco.agosti93@gmail.com> | 2018-09-19 21:58:30 +0800 |
commit | 60e2dfdbda1e089ee4d4419243167eaeb769ff6a (patch) | |
tree | 5a6e4bec2771a8b9a8ba5f0bc19bbe91cb117a07 /packages/order-utils | |
parent | 93f7e33f6a1d0a056198a9f22bcb10ef3e4f4f25 (diff) | |
download | dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.gz dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.bz2 dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.lz dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.xz dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.zst dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.zip |
Calculate min and max rates in buy quote
Diffstat (limited to 'packages/order-utils')
-rw-r--r-- | packages/order-utils/src/market_utils.ts | 39 | ||||
-rw-r--r-- | packages/order-utils/src/types.ts | 2 |
2 files changed, 27 insertions, 14 deletions
diff --git a/packages/order-utils/src/market_utils.ts b/packages/order-utils/src/market_utils.ts index 4a664cb14..ed6af7d85 100644 --- a/packages/order-utils/src/market_utils.ts +++ b/packages/order-utils/src/market_utils.ts @@ -51,17 +51,23 @@ export const marketUtils = { // iterate through the orders input from left to right until we have enough makerAsset to fill totalFillAmount const result = _.reduce( orders, - ({ resultOrders, remainingFillAmount }, order, index) => { + ({ resultOrders, remainingFillAmount, ordersRemainingFillableMakerAssetAmounts }, order, index) => { if (remainingFillAmount.lessThanOrEqualTo(constants.ZERO_AMOUNT)) { - return { resultOrders, remainingFillAmount: constants.ZERO_AMOUNT }; + return { + resultOrders, + remainingFillAmount: constants.ZERO_AMOUNT, + ordersRemainingFillableMakerAssetAmounts, + }; } else { const makerAssetAmountAvailable = remainingFillableMakerAssetAmounts[index]; + const shouldIncludeOrder = makerAssetAmountAvailable.gt(constants.ZERO_AMOUNT); // if there is no makerAssetAmountAvailable do not append order to resultOrders // if we have exceeded the total amount we want to fill set remainingFillAmount to 0 return { - resultOrders: makerAssetAmountAvailable.gt(constants.ZERO_AMOUNT) - ? _.concat(resultOrders, order) - : resultOrders, + resultOrders: shouldIncludeOrder ? _.concat(resultOrders, order) : resultOrders, + ordersRemainingFillableMakerAssetAmounts: shouldIncludeOrder + ? _.concat(ordersRemainingFillableMakerAssetAmounts, makerAssetAmountAvailable) + : ordersRemainingFillableMakerAssetAmounts, remainingFillAmount: BigNumber.max( constants.ZERO_AMOUNT, remainingFillAmount.minus(makerAssetAmountAvailable), @@ -69,7 +75,11 @@ export const marketUtils = { }; } }, - { resultOrders: [] as T[], remainingFillAmount: totalFillAmount }, + { + resultOrders: [] as T[], + remainingFillAmount: totalFillAmount, + ordersRemainingFillableMakerAssetAmounts: [] as BigNumber[], + }, ); return result; }, @@ -133,17 +143,18 @@ export const marketUtils = { }, constants.ZERO_AMOUNT, ); - const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount( - feeOrders, - totalFeeAmount, - { - remainingFillableMakerAssetAmounts: remainingFillableFeeAmounts, - slippageBufferAmount, - }, - ); + const { + resultOrders, + remainingFillAmount, + ordersRemainingFillableMakerAssetAmounts, + } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(feeOrders, totalFeeAmount, { + remainingFillableMakerAssetAmounts: remainingFillableFeeAmounts, + slippageBufferAmount, + }); return { resultFeeOrders: resultOrders, remainingFeeAmount: remainingFillAmount, + feeOrdersRemainingFillableMakerAssetAmounts: ordersRemainingFillableMakerAssetAmounts, }; // TODO: add more orders here to cover rounding // https://github.com/0xProject/0x-protocol-specification/blob/master/v2/forwarding-contract-specification.md#over-buying-zrx diff --git a/packages/order-utils/src/types.ts b/packages/order-utils/src/types.ts index 09292e557..a843efaa4 100644 --- a/packages/order-utils/src/types.ts +++ b/packages/order-utils/src/types.ts @@ -72,10 +72,12 @@ export interface FindFeeOrdersThatCoverFeesForTargetOrdersOpts { export interface FeeOrdersAndRemainingFeeAmount<T> { resultFeeOrders: T[]; + feeOrdersRemainingFillableMakerAssetAmounts: BigNumber[]; remainingFeeAmount: BigNumber; } export interface OrdersAndRemainingFillAmount<T> { resultOrders: T[]; + ordersRemainingFillableMakerAssetAmounts: BigNumber[]; remainingFillAmount: BigNumber; } |