aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/buy_quote_calculator.ts
blob: b706ea143e5334e22ca983c243efc2694227ff74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { marketUtils } from '@0xproject/order-utils';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

import { constants } from '../constants';
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: OrdersAndFillableAmounts,
        feeOrdersAndFillableAmounts: OrdersAndFillableAmounts,
        assetBuyAmount: BigNumber,
        feePercentage: number,
        slippagePercentage: number,
    ): BuyQuote {
        const orders = ordersAndFillableAmounts.orders;
        const remainingFillableMakerAssetAmounts = ordersAndFillableAmounts.remainingFillableMakerAssetAmounts;
        const feeOrders = feeOrdersAndFillableAmounts.orders;
        const remainingFillableFeeAmounts = feeOrdersAndFillableAmounts.remainingFillableMakerAssetAmounts;
        const slippageBufferAmount = assetBuyAmount.mul(slippagePercentage).round();
        const {
            resultOrders,
            remainingFillAmount,
            ordersRemainingFillableMakerAssetAmounts,
        } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(orders, assetBuyAmount, {
            remainingFillableMakerAssetAmounts,
            slippageBufferAmount,
        });
        if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
            throw new Error(AssetBuyerError.InsufficientAssetLiquidity);
        }
        // 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 {
            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;

        // 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 claimableTakerAssetAmount = orderUtils.calculateRemainingTakerAssetAmount(
                order,
                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: feeAdjustedMinRate,
            maxRate: feeAdjustedMaxRate,
            assetBuyAmount,
            feePercentage,
        };
    },
};