aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contract-wrappers/test/calldata_optimization_utils_test.ts
blob: 94e55bffa7b22de6c5e4c02091889a51661ed841 (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
import { orderFactory } from '@0xproject/order-utils/lib/src/order_factory';
import * as chai from 'chai';
import * as _ from 'lodash';
import 'mocha';

import { calldataOptimizationUtils } from '../src/utils/calldata_optimization_utils';
import { constants } from '../src/utils/constants';

import { chaiSetup } from './utils/chai_setup';

chaiSetup.configure();
const expect = chai.expect;

// utility for generating a set of order objects with mostly NULL values
// except for a specified makerAssetData and takerAssetData
const FAKE_ORDERS_COUNT = 5;
const generateFakeOrders = (makerAssetData: string, takerAssetData: string) =>
    _.map(_.range(FAKE_ORDERS_COUNT), index => {
        const order = orderFactory.createOrder(
            constants.NULL_ADDRESS,
            constants.ZERO_AMOUNT,
            makerAssetData,
            constants.ZERO_AMOUNT,
            takerAssetData,
            constants.NULL_ADDRESS,
        );
        return {
            ...order,
            signature: 'dummy signature',
        };
    });

describe('calldataOptimizationUtils', () => {
    const fakeMakerAssetData = 'fakeMakerAssetData';
    const fakeTakerAssetData = 'fakeTakerAssetData';
    const orders = generateFakeOrders(fakeMakerAssetData, fakeTakerAssetData);
    describe('#optimizeForwarderOrders', () => {
        it('should make makerAssetData `0x` unless first order', () => {
            const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
            expect(optimizedOrders[0].makerAssetData).to.equal(fakeMakerAssetData);
            const ordersWithoutHead = _.slice(optimizedOrders, 1);
            _.forEach(ordersWithoutHead, order => expect(order.makerAssetData).to.equal(constants.NULL_BYTES));
        });
        it('should make all takerAssetData `0x`', () => {
            const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
            _.forEach(optimizedOrders, order => expect(order.takerAssetData).to.equal(constants.NULL_BYTES));
        });
    });
    describe('#optimizeForwarderFeeOrders', () => {
        it('should make all makerAssetData `0x`', () => {
            const optimizedOrders = calldataOptimizationUtils.optimizeForwarderFeeOrders(orders);
            _.forEach(optimizedOrders, order => expect(order.makerAssetData).to.equal(constants.NULL_BYTES));
        });
        it('should make all takerAssetData `0x`', () => {
            const optimizedOrders = calldataOptimizationUtils.optimizeForwarderFeeOrders(orders);
            _.forEach(optimizedOrders, order => expect(order.takerAssetData).to.equal(constants.NULL_BYTES));
        });
    });
});