aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer
diff options
context:
space:
mode:
authorfragosti <francesco.agosti93@gmail.com>2018-09-19 21:58:30 +0800
committerfragosti <francesco.agosti93@gmail.com>2018-09-19 21:58:30 +0800
commit60e2dfdbda1e089ee4d4419243167eaeb769ff6a (patch)
tree5a6e4bec2771a8b9a8ba5f0bc19bbe91cb117a07 /packages/asset-buyer
parent93f7e33f6a1d0a056198a9f22bcb10ef3e4f4f25 (diff)
downloaddexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.gz
dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.tar.zst
dexon-sol-tools-60e2dfdbda1e089ee4d4419243167eaeb769ff6a.zip
Calculate min and max rates in buy quote
Diffstat (limited to 'packages/asset-buyer')
-rw-r--r--packages/asset-buyer/src/utils/buy_quote_calculator.ts62
1 files changed, 43 insertions, 19 deletions
diff --git a/packages/asset-buyer/src/utils/buy_quote_calculator.ts b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
index e05ab1e55..52cecf8ad 100644
--- a/packages/asset-buyer/src/utils/buy_quote_calculator.ts
+++ b/packages/asset-buyer/src/utils/buy_quote_calculator.ts
@@ -1,5 +1,6 @@
import { marketUtils } from '@0xproject/order-utils';
import { BigNumber } from '@0xproject/utils';
+import * as _ from 'lodash';
import { constants } from '../constants';
import { AssetBuyerError, AssetBuyerOrdersAndFillableAmounts, BuyQuote } from '../types';
@@ -18,41 +19,64 @@ export const buyQuoteCalculator = {
remainingFillableFeeAmounts,
} = ordersAndFillableAmounts;
const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round();
- const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
- orders,
- assetBuyAmount,
- {
- remainingFillableMakerAssetAmounts,
- slippageBufferAmount,
- },
- );
+ const {
+ resultOrders,
+ remainingFillAmount,
+ ordersRemainingFillableMakerAssetAmounts,
+ } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(orders, assetBuyAmount, {
+ remainingFillableMakerAssetAmounts,
+ slippageBufferAmount,
+ });
if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
throw new Error(AssetBuyerError.InsufficientAssetLiquidity);
}
// TODO: 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 { resultFeeOrders, remainingFeeAmount } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(
- resultOrders,
- feeOrders,
- {
- remainingFillableMakerAssetAmounts,
- remainingFillableFeeAmounts,
- },
- );
+ const {
+ resultFeeOrders,
+ remainingFeeAmount,
+ feeOrdersRemainingFillableMakerAssetAmounts,
+ } = marketUtils.findFeeOrdersThatCoverFeesForTargetOrders(resultOrders, feeOrders, {
+ remainingFillableMakerAssetAmounts,
+ remainingFillableFeeAmounts,
+ });
if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
throw new Error(AssetBuyerError.InsufficientZrxLiquidity);
}
const assetData = orders[0].makerAssetData;
- // TODO: critical
+
// calculate minRate and maxRate by calculating min and max eth usage and then dividing into
// assetBuyAmount to get assetData / WETH, needs to take into account feePercentage as well
+ // minEthAmount = (sum(takerAssetAmount[i]) until sum(makerAssetAmount[i]) >= assetBuyAmount ) * (1 + feePercentage)
+ // maxEthAmount = (sum(takerAssetAmount[i]) until i == orders.length) * (1 + feePercentage)
+ const allOrders = _.concat(resultOrders, resultFeeOrders);
+ const allRemainingAmounts = _.concat(
+ ordersRemainingFillableMakerAssetAmounts,
+ feeOrdersRemainingFillableMakerAssetAmounts,
+ );
+ let minEthAmount = constants.ZERO_AMOUNT;
+ let maxEthAmount = constants.ZERO_AMOUNT;
+ let cumulativeMakerAmount = constants.ZERO_AMOUNT;
+ _.forEach(allOrders, (order, index) => {
+ const remainingFillableMakerAssetAmount = allRemainingAmounts[index];
+ const orderRate = order.takerAssetAmount.div(order.makerAssetAmount);
+ const claimableTakerAssetAmount = orderRate.mul(remainingFillableMakerAssetAmount);
+ // taker asset is always assumed to be WETH
+ maxEthAmount = maxEthAmount.plus(claimableTakerAssetAmount);
+ if (cumulativeMakerAmount.lessThan(assetBuyAmount)) {
+ minEthAmount = minEthAmount.plus(claimableTakerAssetAmount);
+ }
+ cumulativeMakerAmount = cumulativeMakerAmount.plus(remainingFillableMakerAssetAmount);
+ });
+ const feeAdjustedMinRate = minEthAmount.mul(feePercentage + 1).div(assetBuyAmount);
+ const feeAdjustedMaxRate = minEthAmount.mul(feePercentage + 1).div(assetBuyAmount);
return {
assetData,
orders: resultOrders,
feeOrders: resultFeeOrders,
- minRate: constants.ZERO_AMOUNT,
- maxRate: constants.ZERO_AMOUNT,
+ minRate: feeAdjustedMinRate,
+ maxRate: feeAdjustedMaxRate,
assetBuyAmount,
feePercentage,
};