aboutsummaryrefslogtreecommitdiffstats
path: root/packages/asset-buyer/src/utils/order_utils.ts
blob: ff47eb7c5e5ab0f133102f751978f36e7634510a (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
import { SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';

import { constants } from '../constants';

export const orderUtils = {
    isOrderExpired(order: SignedOrder): boolean {
        return orderUtils.willOrderExpire(order, 0);
    },
    willOrderExpire(order: SignedOrder, secondsFromNow: number): boolean {
        const millisecondsInSecond = 1000;
        const currentUnixTimestampSec = new BigNumber(Date.now() / millisecondsInSecond).round();
        return order.expirationTimeSeconds.lessThan(currentUnixTimestampSec.plus(secondsFromNow));
    },
    calculateRemainingMakerAssetAmount(order: SignedOrder, remainingTakerAssetAmount: BigNumber): BigNumber {
        if (remainingTakerAssetAmount.eq(0)) {
            return constants.ZERO_AMOUNT;
        }
        return remainingTakerAssetAmount.times(order.makerAssetAmount).dividedToIntegerBy(order.takerAssetAmount);
    },
    calculateRemainingTakerAssetAmount(order: SignedOrder, remainingMakerAssetAmount: BigNumber): BigNumber {
        if (remainingMakerAssetAmount.eq(0)) {
            return constants.ZERO_AMOUNT;
        }
        return remainingMakerAssetAmount.times(order.takerAssetAmount).dividedToIntegerBy(order.makerAssetAmount);
    },
    isOpenOrder(order: SignedOrder): boolean {
        return order.takerAddress === constants.NULL_ADDRESS;
    },
};