aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/buy_quote.ts
blob: 0e880f51c56aa25df00b78f601d0d1b0aead7263 (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
import { BuyQuoteInfo } from '@0x/asset-buyer';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { oc } from 'ts-optchain';

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

export interface BuyQuoteWeiAmounts {
    assetTotalInWei: BigNumber | undefined;
    feeTotalInWei: BigNumber | undefined;
    grandTotalInWei: BigNumber | undefined;
    pricePerTokenInWei: BigNumber | undefined;
}

export const buyQuoteUtil = {
    getWeiAmounts: (
        selectedAssetUnitAmount: BigNumber | undefined,
        buyQuoteInfo: BuyQuoteInfo | undefined,
    ): BuyQuoteWeiAmounts => {
        const buyQuoteAccessor = oc(buyQuoteInfo);
        const assetTotalInWei = buyQuoteAccessor.assetEthAmount();
        const pricePerTokenInWei =
            !_.isUndefined(assetTotalInWei) &&
            !_.isUndefined(selectedAssetUnitAmount) &&
            !selectedAssetUnitAmount.eq(BIG_NUMBER_ZERO)
                ? assetTotalInWei.div(selectedAssetUnitAmount).ceil()
                : undefined;

        return {
            assetTotalInWei,
            feeTotalInWei: buyQuoteAccessor.feeEthAmount(),
            grandTotalInWei: buyQuoteAccessor.totalEthAmount(),
            pricePerTokenInWei,
        };
    },
};