2021-02-04 19:15:23 +01:00
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2023-01-20 18:04:37 +01:00
|
|
|
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getCurrentCurrency } from '../selectors';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getConversionRate,
|
|
|
|
getNativeCurrency,
|
2021-06-08 18:03:59 +02:00
|
|
|
} from '../ducks/metamask/metamask';
|
2020-05-12 21:07:35 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
import { getValueFromWeiHex } from '../../shared/modules/conversion.utils';
|
2022-04-21 21:09:41 +02:00
|
|
|
import { TEST_NETWORK_TICKER_MAP } from '../../shared/constants/network';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { Numeric } from '../../shared/modules/Numeric';
|
|
|
|
import { EtherDenomination } from '../../shared/constants/common';
|
2021-06-24 01:28:49 +02:00
|
|
|
|
2020-05-12 21:07:35 +02:00
|
|
|
/**
|
|
|
|
* Defines the shape of the options parameter for useCurrencyDisplay
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} UseCurrencyOptions
|
2022-01-07 16:57:33 +01:00
|
|
|
* @property {string} [displayValue] - When present is used in lieu of formatting the inputValue
|
|
|
|
* @property {string} [prefix] - String to prepend to the final result
|
2020-05-12 21:07:35 +02:00
|
|
|
* @property {number} [numberOfDecimals] - Number of significant decimals to display
|
2022-01-07 16:57:33 +01:00
|
|
|
* @property {string} [denomination] - Denomination (wei, gwei) to convert to for display
|
|
|
|
* @property {string} [currency] - Currency type to convert to. Will override nativeCurrency
|
2020-05-12 21:07:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines the return shape of the second value in the tuple
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} CurrencyDisplayParts
|
2022-01-07 16:57:33 +01:00
|
|
|
* @property {string} [prefix] - string to prepend to the value for display
|
|
|
|
* @property {string} value - string representing the value, formatted for display
|
|
|
|
* @property {string} [suffix] - string to append to the value for display
|
2020-05-12 21:07:35 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* useCurrencyDisplay hook
|
|
|
|
*
|
|
|
|
* Given a hexadecimal encoded value string and an object of parameters used for formatting the
|
|
|
|
* display, produce both a fully formed string and the pieces of that string used for displaying
|
|
|
|
* the currency to the user
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param {string} inputValue - The value to format for display
|
|
|
|
* @param {UseCurrencyOptions} opts - An object for options to format the inputValue
|
|
|
|
* @returns {[string, CurrencyDisplayParts]}
|
2020-05-12 21:07:35 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useCurrencyDisplay(
|
|
|
|
inputValue,
|
|
|
|
{ displayValue, prefix, numberOfDecimals, denomination, currency, ...opts },
|
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
|
|
|
const nativeCurrency = useSelector(getNativeCurrency);
|
|
|
|
const conversionRate = useSelector(getConversionRate);
|
2021-06-24 01:28:49 +02:00
|
|
|
const isUserPreferredCurrency = currency === currentCurrency;
|
2020-05-12 21:07:35 +02:00
|
|
|
|
|
|
|
const value = useMemo(() => {
|
|
|
|
if (displayValue) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return displayValue;
|
2020-05-12 21:07:35 +02:00
|
|
|
}
|
2021-06-24 01:28:49 +02:00
|
|
|
if (
|
|
|
|
currency === nativeCurrency ||
|
|
|
|
(!isUserPreferredCurrency && !nativeCurrency)
|
|
|
|
) {
|
2023-01-24 15:44:49 +01:00
|
|
|
return new Numeric(inputValue, 16, EtherDenomination.WEI)
|
|
|
|
.toDenomination(denomination || EtherDenomination.ETH)
|
|
|
|
.round(numberOfDecimals || 2)
|
|
|
|
.toBase(10)
|
|
|
|
.toString();
|
2021-06-24 01:28:49 +02:00
|
|
|
} else if (isUserPreferredCurrency && conversionRate) {
|
|
|
|
return formatCurrency(
|
|
|
|
getValueFromWeiHex({
|
|
|
|
value: inputValue,
|
|
|
|
fromCurrency: nativeCurrency,
|
|
|
|
toCurrency: currency,
|
|
|
|
conversionRate,
|
|
|
|
numberOfDecimals: numberOfDecimals || 2,
|
|
|
|
toDenomination: denomination,
|
|
|
|
}),
|
|
|
|
currency,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
inputValue,
|
|
|
|
nativeCurrency,
|
|
|
|
conversionRate,
|
|
|
|
displayValue,
|
|
|
|
numberOfDecimals,
|
|
|
|
denomination,
|
2021-06-24 01:28:49 +02:00
|
|
|
currency,
|
|
|
|
isUserPreferredCurrency,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-05-12 21:07:35 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let suffix;
|
2020-05-12 21:07:35 +02:00
|
|
|
|
|
|
|
if (!opts.hideLabel) {
|
2022-09-29 05:26:01 +02:00
|
|
|
// if the currency we are displaying is the native currency of one of our preloaded test-nets (goerli, sepolia etc.)
|
2022-04-21 21:09:41 +02:00
|
|
|
// then we allow lowercase characters, otherwise we force to uppercase any suffix passed as a currency
|
|
|
|
const currencyTickerSymbol = Object.values(
|
|
|
|
TEST_NETWORK_TICKER_MAP,
|
|
|
|
).includes(currency)
|
|
|
|
? currency
|
|
|
|
: currency?.toUpperCase();
|
|
|
|
|
|
|
|
suffix = opts.suffix || currencyTickerSymbol;
|
2020-05-12 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
return [
|
|
|
|
`${prefix || ''}${value}${suffix ? ` ${suffix}` : ''}`,
|
|
|
|
{ prefix, value, suffix },
|
2021-02-04 19:15:23 +01:00
|
|
|
];
|
2020-05-12 21:07:35 +02:00
|
|
|
}
|