aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/utils/formatters.ts
blob: 4f6116f067910a8f84cc313400a6b3ba416c8ee7 (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
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

import { BatchCancelOrders, BatchFillOrders, MarketFillOrders, SignedOrder } from './types';

export const formatters = {
    createBatchFill(signedOrders: SignedOrder[], takerTokenFillAmounts: BigNumber[] = []) {
        const batchFill: BatchFillOrders = {
            orders: [],
            signatures: [],
            takerTokenFillAmounts,
        };
        _.forEach(signedOrders, signedOrder => {
            batchFill.orders.push({
                makerAddress: signedOrder.makerAddress,
                takerAddress: signedOrder.takerAddress,
                makerTokenAddress: signedOrder.makerTokenAddress,
                takerTokenAddress: signedOrder.takerTokenAddress,
                feeRecipientAddress: signedOrder.feeRecipientAddress,
                makerTokenAmount: signedOrder.makerTokenAmount,
                takerTokenAmount: signedOrder.takerTokenAmount,
                makerFeeAmount: signedOrder.makerFeeAmount,
                takerFeeAmount: signedOrder.takerFeeAmount,
                expirationTimeSeconds: signedOrder.expirationTimeSeconds,
                salt: signedOrder.salt,
            });
            batchFill.signatures.push(signedOrder.signature);
            if (takerTokenFillAmounts.length < signedOrders.length) {
                batchFill.takerTokenFillAmounts.push(signedOrder.takerTokenAmount);
            }
        });
        return batchFill;
    },
    createMarketFillOrders(signedOrders: SignedOrder[], takerTokenFillAmount: BigNumber) {
        const marketFillOrders: MarketFillOrders = {
            orders: [],
            signatures: [],
            takerTokenFillAmount,
        };
        _.forEach(signedOrders, signedOrder => {
            marketFillOrders.orders.push({
                makerAddress: signedOrder.makerAddress,
                takerAddress: signedOrder.takerAddress,
                makerTokenAddress: signedOrder.makerTokenAddress,
                takerTokenAddress: signedOrder.takerTokenAddress,
                feeRecipientAddress: signedOrder.feeRecipientAddress,
                makerTokenAmount: signedOrder.makerTokenAmount,
                takerTokenAmount: signedOrder.takerTokenAmount,
                makerFeeAmount: signedOrder.makerFeeAmount,
                takerFeeAmount: signedOrder.takerFeeAmount,
                expirationTimeSeconds: signedOrder.expirationTimeSeconds,
                salt: signedOrder.salt,
            });
            marketFillOrders.signatures.push(signedOrder.signature);
        });
        return marketFillOrders;
    },
    createBatchCancel(signedOrders: SignedOrder[], takerTokenCancelAmounts: BigNumber[] = []) {
        const batchCancel: BatchCancelOrders = {
            orders: [],
            takerTokenCancelAmounts,
        };
        _.forEach(signedOrders, signedOrder => {
            batchCancel.orders.push({
                makerAddress: signedOrder.makerAddress,
                takerAddress: signedOrder.takerAddress,
                makerTokenAddress: signedOrder.makerTokenAddress,
                takerTokenAddress: signedOrder.takerTokenAddress,
                feeRecipientAddress: signedOrder.feeRecipientAddress,
                makerTokenAmount: signedOrder.makerTokenAmount,
                takerTokenAmount: signedOrder.takerTokenAmount,
                makerFeeAmount: signedOrder.makerFeeAmount,
                takerFeeAmount: signedOrder.takerFeeAmount,
                expirationTimeSeconds: signedOrder.expirationTimeSeconds,
                salt: signedOrder.salt,
            });
            if (takerTokenCancelAmounts.length < signedOrders.length) {
                batchCancel.takerTokenCancelAmounts.push(signedOrder.takerTokenAmount);
            }
        });
        return batchCancel;
    },
};