aboutsummaryrefslogtreecommitdiffstats
path: root/packages/forwarder-helper/src/forwarder_helper_impl.ts
blob: 0d03c9f76f2abb69813f8c002139608d12e01dab (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
import { marketUtils } from '@0xproject/order-utils';
import { SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';

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

export class ForwarderHelperImpl implements ForwarderHelper {
    private _orders: SignedOrder[];
    private _feeOrders: SignedOrder[];
    private _remainingFillableMakerAssetAmountsIfExists?: BigNumber[];
    private _remainingFillableFeeAmountsIfExists?: BigNumber[];
    constructor(
        orders: SignedOrder[],
        feeOrders: SignedOrder[] = [] as SignedOrder[],
        remainingFillableMakerAssetAmounts?: BigNumber[],
        remainingFillableFeeAmounts?: BigNumber[],
    ) {
        this._orders = orders;
        this._feeOrders = feeOrders;
        this._remainingFillableMakerAssetAmountsIfExists = remainingFillableMakerAssetAmounts;
        this._remainingFillableFeeAmountsIfExists = remainingFillableFeeAmounts;
    }
    public getMarketBuyOrdersInfo(request: MarketBuyOrdersInfoRequest): MarketBuyOrdersInfo {
        const { makerAssetFillAmount, feePercentage } = request;
        // TODO: make the slippage percentage customizable
        const slippageBufferAmount = makerAssetFillAmount.mul(SLIPPAGE_PERCENTAGE);
        const { resultOrders, remainingFillAmount } = marketUtils.findOrdersThatCoverMakerAssetFillAmount(
            this._orders,
            makerAssetFillAmount,
            {
                remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
                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,
            this._feeOrders,
            {
                remainingFillableMakerAssetAmounts: this._remainingFillableMakerAssetAmountsIfExists,
                remainingFillableFeeAmounts: this._remainingFillableFeeAmountsIfExists,
            },
        );
        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,
        };
    }
}