2021-02-04 19:15:23 +01:00
|
|
|
import { useMemo } from 'react';
|
2021-12-01 17:25:09 +01:00
|
|
|
import { shallowEqual, useSelector } from 'react-redux';
|
2020-11-03 00:41:28 +01:00
|
|
|
import {
|
|
|
|
getTokenExchangeRates,
|
|
|
|
getCurrentCurrency,
|
|
|
|
getShouldShowFiat,
|
2021-02-04 19:15:23 +01:00
|
|
|
} from '../selectors';
|
|
|
|
import { getTokenFiatAmount } from '../helpers/utils/token-util';
|
2021-06-08 18:03:59 +02:00
|
|
|
import { getConversionRate } from '../ducks/metamask/metamask';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
|
2020-06-10 21:04:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the token balance converted to fiat and formatted for display
|
|
|
|
*
|
2020-06-12 19:37:06 +02:00
|
|
|
* @param {string} [tokenAddress] - The token address
|
2020-06-10 21:04:29 +02:00
|
|
|
* @param {string} [tokenAmount] - The token balance
|
|
|
|
* @param {string} [tokenSymbol] - The token symbol
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {Object} [overrides] - A configuration object that allows the caller to explicitly pass an exchange rate or
|
2020-10-06 20:28:38 +02:00
|
|
|
* ensure fiat is shown even if the property is not set in state.
|
|
|
|
* @param {number} [overrides.exchangeRate] - An exhchange rate to use instead of the one selected from state
|
|
|
|
* @param {boolean} [overrides.showFiat] - If truthy, ensures the fiat value is shown even if the showFiat value from state is falsey
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {boolean} hideCurrencySymbol - Indicates whether the returned formatted amount should include the trailing currency symbol
|
|
|
|
* @returns {string} The formatted token amount in the user's chosen fiat currency
|
2020-06-10 21:04:29 +02:00
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useTokenFiatAmount(
|
|
|
|
tokenAddress,
|
|
|
|
tokenAmount,
|
|
|
|
tokenSymbol,
|
|
|
|
overrides = {},
|
|
|
|
hideCurrencySymbol,
|
|
|
|
) {
|
2021-12-01 17:25:09 +01:00
|
|
|
const contractExchangeRates = useSelector(
|
|
|
|
getTokenExchangeRates,
|
|
|
|
shallowEqual,
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
const conversionRate = useSelector(getConversionRate);
|
|
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
|
|
|
const userPrefersShownFiat = useSelector(getShouldShowFiat);
|
|
|
|
const showFiat = overrides.showFiat ?? userPrefersShownFiat;
|
2021-09-27 16:06:07 +02:00
|
|
|
const contractExchangeTokenKey = Object.keys(
|
|
|
|
contractExchangeRates,
|
|
|
|
).find((key) => isEqualCaseInsensitive(key, tokenAddress));
|
2020-11-03 00:41:28 +01:00
|
|
|
const tokenExchangeRate =
|
2021-09-27 16:06:07 +02:00
|
|
|
overrides.exchangeRate ??
|
|
|
|
(contractExchangeTokenKey &&
|
|
|
|
contractExchangeRates[contractExchangeTokenKey]);
|
2020-06-10 21:04:29 +02:00
|
|
|
const formattedFiat = useMemo(
|
2020-11-03 00:41:28 +01:00
|
|
|
() =>
|
|
|
|
getTokenFiatAmount(
|
|
|
|
tokenExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
tokenAmount,
|
|
|
|
tokenSymbol,
|
|
|
|
true,
|
|
|
|
hideCurrencySymbol,
|
|
|
|
),
|
|
|
|
[
|
2020-06-10 21:04:29 +02:00
|
|
|
tokenExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
tokenAmount,
|
2020-07-14 17:20:41 +02:00
|
|
|
tokenSymbol,
|
2020-10-06 20:28:38 +02:00
|
|
|
hideCurrencySymbol,
|
2020-11-03 00:41:28 +01:00
|
|
|
],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-10 21:04:29 +02:00
|
|
|
|
2020-06-12 20:47:23 +02:00
|
|
|
if (!showFiat || currentCurrency.toUpperCase() === tokenSymbol) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return undefined;
|
2020-06-10 21:04:29 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return formattedFiat;
|
2020-06-10 21:04:29 +02:00
|
|
|
}
|