aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util/formatters.ts
blob: 8e0dfb09e8453b1a7a0ec4f130dea060141af8d2 (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
import { SignedOrder } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

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

export const formatters = {
    createBatchFill(
        signedOrders: SignedOrder[],
        shouldThrowOnInsufficientBalanceOrAllowance: boolean,
        fillTakerTokenAmounts: BigNumber[] = [],
    ) {
        const batchFill: BatchFillOrders = {
            orderAddresses: [],
            orderValues: [],
            fillTakerTokenAmounts,
            shouldThrowOnInsufficientBalanceOrAllowance,
            v: [],
            r: [],
            s: [],
        };
        _.forEach(signedOrders, signedOrder => {
            batchFill.orderAddresses.push([
                signedOrder.maker,
                signedOrder.taker,
                signedOrder.makerTokenAddress,
                signedOrder.takerTokenAddress,
                signedOrder.feeRecipient,
            ]);
            batchFill.orderValues.push([
                signedOrder.makerTokenAmount,
                signedOrder.takerTokenAmount,
                signedOrder.makerFee,
                signedOrder.takerFee,
                signedOrder.expirationUnixTimestampSec,
                signedOrder.salt,
            ]);
            batchFill.v.push(signedOrder.ecSignature.v);
            batchFill.r.push(signedOrder.ecSignature.r);
            batchFill.s.push(signedOrder.ecSignature.s);
            if (fillTakerTokenAmounts.length < signedOrders.length) {
                batchFill.fillTakerTokenAmounts.push(signedOrder.takerTokenAmount);
            }
        });
        return batchFill;
    },
    createFillUpTo(
        signedOrders: SignedOrder[],
        shouldThrowOnInsufficientBalanceOrAllowance: boolean,
        fillTakerTokenAmount: BigNumber,
    ) {
        const fillUpTo: FillOrdersUpTo = {
            orderAddresses: [],
            orderValues: [],
            fillTakerTokenAmount,
            shouldThrowOnInsufficientBalanceOrAllowance,
            v: [],
            r: [],
            s: [],
        };
        signedOrders.forEach(signedOrder => {
            fillUpTo.orderAddresses.push([
                signedOrder.maker,
                signedOrder.taker,
                signedOrder.makerTokenAddress,
                signedOrder.takerTokenAddress,
                signedOrder.feeRecipient,
            ]);
            fillUpTo.orderValues.push([
                signedOrder.makerTokenAmount,
                signedOrder.takerTokenAmount,
                signedOrder.makerFee,
                signedOrder.takerFee,
                signedOrder.expirationUnixTimestampSec,
                signedOrder.salt,
            ]);
            fillUpTo.v.push(signedOrder.ecSignature.v);
            fillUpTo.r.push(signedOrder.ecSignature.r);
            fillUpTo.s.push(signedOrder.ecSignature.s);
        });
        return fillUpTo;
    },
    createBatchCancel(signedOrders: SignedOrder[], cancelTakerTokenAmounts: BigNumber[] = []) {
        const batchCancel: BatchCancelOrders = {
            orderAddresses: [],
            orderValues: [],
            cancelTakerTokenAmounts,
        };
        signedOrders.forEach(signedOrder => {
            batchCancel.orderAddresses.push([
                signedOrder.maker,
                signedOrder.taker,
                signedOrder.makerTokenAddress,
                signedOrder.takerTokenAddress,
                signedOrder.feeRecipient,
            ]);
            batchCancel.orderValues.push([
                signedOrder.makerTokenAmount,
                signedOrder.takerTokenAmount,
                signedOrder.makerFee,
                signedOrder.takerFee,
                signedOrder.expirationUnixTimestampSec,
                signedOrder.salt,
            ]);
            if (cancelTakerTokenAmounts.length < signedOrders.length) {
                batchCancel.cancelTakerTokenAmounts.push(signedOrder.takerTokenAmount);
            }
        });
        return batchCancel;
    },
};