aboutsummaryrefslogtreecommitdiffstats
path: root/packages/forwarder-helper/src/forwarder_helper_impl.ts
blob: 8f4ec1c02350ed42019d34372adaafd9af5962d6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { marketUtils, sortingUtils } from '@0xproject/order-utils';
import { SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

import { constants } from './constants';
import { ForwarderHelper, ForwarderHelperError, MarketBuyOrdersInfo, MarketBuyOrdersInfoRequest } from './types';

const SLIPPAGE_PERCENTAGE = new BigNumber(0.2); // 20% slippage protection, possibly move this into request interface

interface SignedOrderWithAmount extends SignedOrder {
    remainingFillAmount?: BigNumber;
}

export interface ForwarderHelperImplConfig {
    orders: SignedOrder[];
    feeOrders: SignedOrder[];
    remainingFillableMakerAssetAmounts?: BigNumber[];
    remainingFillableFeeAmounts?: BigNumber[];
}

export class ForwarderHelperImpl implements ForwarderHelper {
    private _config: ForwarderHelperImplConfig;
    private static _createSignedOrderWithAmounts(
        orders: SignedOrder[],
        amounts?: BigNumber[],
    ): SignedOrderWithAmount[] {
        const ordersAndAmounts = _.map(orders, (order, index) => {
            return {
                ...order,
                remainingFillAmount: _.nth(amounts, index),
            };
        });
        return ordersAndAmounts;
    }
    private static _unbundleSignedOrderWithAmounts(
        signedOrderWithAmounts: SignedOrderWithAmount[],
    ): { orders: SignedOrder[]; amounts?: BigNumber[] } {
        const orders = _.map(signedOrderWithAmounts, order => {
            const { remainingFillAmount, ...rest } = order;
            return rest;
        });
        const amounts = _.map(signedOrderWithAmounts, order => {
            const { remainingFillAmount, ...rest } = order;
            return remainingFillAmount;
        });
        const compactAmounts = _.compact(amounts);
        return {
            orders,
            amounts: compactAmounts.length > 0 ? compactAmounts : undefined,
        };
    }
    private static _sortConfig(opts: ForwarderHelperImplConfig): ForwarderHelperImplConfig {
        const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = opts;
        const orderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts(
            orders,
            remainingFillableMakerAssetAmounts,
        );
        const sortedOrderWithAmounts = sortingUtils.sortOrdersByFeeAdjustedRate(orderWithAmounts);
        const unbundledSortedOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts(
            sortedOrderWithAmounts,
        );
        const feeOrderWithAmounts = ForwarderHelperImpl._createSignedOrderWithAmounts(
            feeOrders,
            remainingFillableFeeAmounts,
        );
        const sortedFeeOrderWithAmounts = sortingUtils.sortFeeOrdersByFeeAdjustedRate(feeOrderWithAmounts);
        const unbundledSortedFeeOrderWithAmounts = ForwarderHelperImpl._unbundleSignedOrderWithAmounts(
            sortedFeeOrderWithAmounts,
        );
        return {
            orders: unbundledSortedOrderWithAmounts.orders,
            feeOrders: unbundledSortedFeeOrderWithAmounts.orders,
            remainingFillableMakerAssetAmounts: unbundledSortedOrderWithAmounts.amounts,
            remainingFillableFeeAmounts: unbundledSortedFeeOrderWithAmounts.amounts,
        };
    }
    constructor(opts: ForwarderHelperImplConfig) {
        this._config = ForwarderHelperImpl._sortConfig(opts);
    }
    public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo {
        const { makerAssetFillAmount, feePercentage } = request;
        const { orders, feeOrders, remainingFillableMakerAssetAmounts, remainingFillableFeeAmounts } = this._config;
        // TODO: make the slippage percentage customizable
        const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE);
        const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
            orders,
            makerAssetFillAmount,
            {
                remainingFillableMakerAssetAmounts,
                slippageBufferAmount,
            },
        );
        if (remainingFillAmount.gt(constants.ZERO_AMOUNT)) {
            throw new Error(ForwarderHelperError.InsufficientLiquidity);
        }
        // TODO: 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,
            },
        );
        if (remainingFeeAmount.gt(constants.ZERO_AMOUNT)) {
            throw new Error(ForwarderHelperError.InsufficientZrxLiquidity);
        }
        // TODO: calculate min and max eth usage
        return {
            makerAssetFillAmount,
            orders: resultOrders,
            feeOrders: resultFeeOrders,
            minEthAmount: constants.ZERO_AMOUNT,
            maxEthAmount: constants.ZERO_AMOUNT,
            feePercentage,
        };
    }
}