aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/util/order_factory.ts
blob: fa12889ca393b1a30ef7709cf7513038179e98ff (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
import { ZeroEx } from '0x.js';
import { BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as _ from 'lodash';

import { Order } from './order';
import { DefaultOrderParams, OptionalOrderParams, OrderParams } from './types';

export class OrderFactory {
    private _defaultOrderParams: DefaultOrderParams;
    private _web3Wrapper: Web3Wrapper;
    constructor(web3Wrapper: Web3Wrapper, defaultOrderParams: DefaultOrderParams) {
        this._defaultOrderParams = defaultOrderParams;
        this._web3Wrapper = web3Wrapper;
    }
    public async newSignedOrderAsync(customOrderParams: OptionalOrderParams = {}) {
        const randomExpiration = new BigNumber(Math.floor((Date.now() + Math.random() * 100000000000) / 1000));
        const orderParams: OrderParams = _.assign(
            {},
            {
                expirationTimestampInSec: randomExpiration,
                salt: ZeroEx.generatePseudoRandomSalt(),
                taker: ZeroEx.NULL_ADDRESS,
            },
            this._defaultOrderParams,
            customOrderParams,
        );
        const order = new Order(this._web3Wrapper, orderParams);
        await order.signAsync();
        return order;
    }
}