aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util/buy_quote.ts
blob: acd4d389c7113242c0362ea5e254b52e69548cde (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { BuyQuoteInfo } from '@0x/asset-buyer';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { oc } from 'ts-optchain';

import { format } from '../util/format';

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

export interface DisplayAmounts {
    pricePerToken: React.ReactNode;
    assetTotal: React.ReactNode;
    feeTotal: React.ReactNode;
    primaryGrandTotal: React.ReactNode;
    secondaryGrandTotal?: React.ReactNode;
}

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

const ethDisplayFormat = (amountInWei?: BigNumber) => {
    return format.ethBaseUnitAmount(amountInWei, 4, '');
};
const usdDisplayFormat = (amountInWei?: BigNumber, ethUsdPrice?: BigNumber) => {
    return format.ethBaseUnitAmountInUsd(amountInWei, ethUsdPrice, 2, '');
};

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,
        };
    },
    displayAmountsEth: (weiAmounts: BuyQuoteWeiAmounts, ethUsdPrice?: BigNumber): DisplayAmounts => {
        return {
            pricePerToken: ethDisplayFormat(weiAmounts.pricePerTokenInWei),
            assetTotal: ethDisplayFormat(weiAmounts.assetTotalInWei),
            feeTotal: ethDisplayFormat(weiAmounts.feeTotalInWei),
            primaryGrandTotal: ethDisplayFormat(weiAmounts.grandTotalInWei),
            secondaryGrandTotal: usdDisplayFormat(weiAmounts.grandTotalInWei, ethUsdPrice),
        };
    },
    displayAmountsUsd: (weiAmounts: BuyQuoteWeiAmounts, ethUsdPrice?: BigNumber): DisplayAmounts => {
        return {
            pricePerToken: usdDisplayFormat(weiAmounts.pricePerTokenInWei, ethUsdPrice),
            assetTotal: usdDisplayFormat(weiAmounts.assetTotalInWei, ethUsdPrice),
            feeTotal: usdDisplayFormat(weiAmounts.feeTotalInWei, ethUsdPrice),
            primaryGrandTotal: usdDisplayFormat(weiAmounts.grandTotalInWei, ethUsdPrice),
            secondaryGrandTotal: ethDisplayFormat(weiAmounts.grandTotalInWei),
        };
    },
};