aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/src/utils/calldata_optimization_utils.ts
blob: 3172cf531510b51a6a7a2d991b8641d8e9a49a75 (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
import { SignedOrder } from '@0xproject/types';
import * as _ from 'lodash';

import { constants } from './constants';

export const calldataOptimizationUtils = {
    /**
     * Takes an array of orders and outputs an array of equivalent orders where all takerAssetData are '0x' and
     * all makerAssetData are '0x' except for that of the first order, which retains its original value
     * @param   orders         An array of SignedOrder objects
     * @returns optimized orders
     */
    optimizeForwarderOrders(orders: SignedOrder[]): SignedOrder[] {
        const optimizedOrders = _.map(orders, (order, index) =>
            transformOrder(order, {
                makerAssetData: index === 0 ? order.makerAssetData : constants.NULL_BYTES,
                takerAssetData: constants.NULL_BYTES,
            }),
        );
        return optimizedOrders;
    },
    /**
     * Takes an array of orders and outputs an array of equivalent orders where all takerAssetData are '0x' and
     * all makerAssetData are '0x'
     * @param   orders         An array of SignedOrder objects
     * @returns optimized orders
     */
    optimizeForwarderFeeOrders(orders: SignedOrder[]): SignedOrder[] {
        const optimizedOrders = _.map(orders, (order, index) =>
            transformOrder(order, {
                makerAssetData: constants.NULL_BYTES,
                takerAssetData: constants.NULL_BYTES,
            }),
        );
        return optimizedOrders;
    },
};

const transformOrder = (order: SignedOrder, partialOrder: Partial<SignedOrder>) => {
    return {
        ...order,
        ...partialOrder,
    };
};