aboutsummaryrefslogtreecommitdiffstats
path: root/packages/order-utils/src/order_factory.ts
blob: 2759aac8143fb11a37c9e0fdcc146f1d9f134dfd (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
import { Provider, SignedOrder } from '@0xproject/types';
import { BigNumber } from '@0xproject/utils';
import * as _ from 'lodash';

import { getOrderHashHex } from './order_hash';
import { generatePseudoRandomSalt } from './salt';
import { signOrderHashAsync } from './signature_utils';

const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false;

export const orderFactory = {
    async createSignedOrderAsync(
        provider: Provider,
        maker: string,
        taker: string,
        makerFee: BigNumber,
        takerFee: BigNumber,
        makerTokenAmount: BigNumber,
        makerTokenAddress: string,
        takerTokenAmount: BigNumber,
        takerTokenAddress: string,
        exchangeContractAddress: string,
        feeRecipient: string,
        expirationUnixTimestampSecIfExists?: BigNumber,
    ): Promise<SignedOrder> {
        const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite
        const expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSecIfExists)
            ? defaultExpirationUnixTimestampSec
            : expirationUnixTimestampSecIfExists;
        const order = {
            maker,
            taker,
            makerFee,
            takerFee,
            makerTokenAmount,
            takerTokenAmount,
            makerTokenAddress,
            takerTokenAddress,
            salt: generatePseudoRandomSalt(),
            exchangeContractAddress,
            feeRecipient,
            expirationUnixTimestampSec,
        };
        const orderHash = getOrderHashHex(order);
        const ecSignature = await signOrderHashAsync(provider, orderHash, maker, SHOULD_ADD_PERSONAL_MESSAGE_PREFIX);
        const signedOrder: SignedOrder = _.assign(order, { ecSignature });
        return signedOrder;
    },
};