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

import { orderHashUtils } from './order_hash';
import { generatePseudoRandomSalt } from './salt';
import { ecSignOrderHashAsync, getVRSHexString } from './signature_utils';

const SHOULD_ADD_PERSONAL_MESSAGE_PREFIX = false;

export const orderFactory = {
    async createSignedOrderAsync(
        provider: Provider,
        makerAddress: string,
        takerAddress: string,
        senderAddress: string,
        makerFee: BigNumber,
        takerFee: BigNumber,
        makerAssetAmount: BigNumber,
        makerAssetData: string,
        takerAssetAmount: BigNumber,
        takerAssetData: string,
        exchangeAddress: string,
        feeRecipientAddress: string,
        expirationTimeSecondsIfExists?: BigNumber,
    ): Promise<SignedOrder> {
        const defaultExpirationUnixTimestampSec = new BigNumber(2524604400); // Close to infinite
        const expirationTimeSeconds = _.isUndefined(expirationTimeSecondsIfExists)
            ? defaultExpirationUnixTimestampSec
            : expirationTimeSecondsIfExists;
        const order = {
            makerAddress,
            takerAddress,
            senderAddress,
            makerFee,
            takerFee,
            makerAssetAmount,
            takerAssetAmount,
            makerAssetData,
            takerAssetData,
            salt: generatePseudoRandomSalt(),
            exchangeAddress,
            feeRecipientAddress,
            expirationTimeSeconds,
        };
        const orderHash = orderHashUtils.getOrderHashHex(order);
        const ecSignature = await ecSignOrderHashAsync(
            provider,
            orderHash,
            makerAddress,
            SHOULD_ADD_PERSONAL_MESSAGE_PREFIX,
        );
        const signature = getVRSHexString(ecSignature);
        const signedOrder: SignedOrder = _.assign(order, { signature });
        return signedOrder;
    },
};