aboutsummaryrefslogtreecommitdiffstats
path: root/test/utils/order_factory.ts
blob: c6c6ed9275afab2082242e3e1ba2fc533a37c003 (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
import {SignedOrder, Token} from '../../src/types';
import * as BigNumber from 'bignumber.js';
import * as _ from 'lodash';
import {ZeroEx} from '../../src/0x.js';
import {constants} from './constants';
import * as ExchangeArtifacts from '../../src/artifacts/Exchange.json';

export const orderFactory = {
    async createSignedOrderAsync(
        zeroEx: ZeroEx,
        networkId: number,
        maker: string,
        taker: string,
        makerTokenAmount: BigNumber.BigNumber|number,
        makerTokenAddress: string,
        takerTokenAmount: BigNumber.BigNumber|number,
        takerTokenAddress: string,
        expirationUnixTimestampSec?: BigNumber.BigNumber): Promise<SignedOrder> {
        // TODO refactor and check
        const exchangeAddress: string = (ExchangeArtifacts as any).networks[networkId].address;
        const INF_TIMESTAMP = new BigNumber(2524604400);
        expirationUnixTimestampSec = _.isUndefined(expirationUnixTimestampSec) ?
            INF_TIMESTAMP :
            expirationUnixTimestampSec;
        const order = {
            maker,
            taker,
            makerFee: new BigNumber(0),
            takerFee: new BigNumber(0),
            makerTokenAmount: _.isNumber(makerTokenAmount) ? new BigNumber(makerTokenAmount) : makerTokenAmount,
            takerTokenAmount: _.isNumber(takerTokenAmount) ? new BigNumber(takerTokenAmount) : takerTokenAmount,
            makerTokenAddress,
            takerTokenAddress,
            salt: ZeroEx.generatePseudoRandomSalt(),
            feeRecipient: constants.NULL_ADDRESS,
            expirationUnixTimestampSec,
        };
        const orderHash = ZeroEx.getOrderHashHex(exchangeAddress, order);
        const ecSignature = await zeroEx.signOrderHashAsync(orderHash);
        const signedOrder: SignedOrder = _.assign(order, {ecSignature});
        return signedOrder;
    }
}