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