You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.2 KiB
65 lines
2.2 KiB
4 years ago
|
import { useMemo } from 'react';
|
||
|
import { useSelector } from 'react-redux';
|
||
4 years ago
|
import {
|
||
|
getTokenExchangeRates,
|
||
|
getConversionRate,
|
||
|
getCurrentCurrency,
|
||
|
getShouldShowFiat,
|
||
4 years ago
|
} from '../selectors';
|
||
|
import { getTokenFiatAmount } from '../helpers/utils/token-util';
|
||
5 years ago
|
|
||
|
/**
|
||
|
* Get the token balance converted to fiat and formatted for display
|
||
|
*
|
||
5 years ago
|
* @param {string} [tokenAddress] - The token address
|
||
5 years ago
|
* @param {string} [tokenAmount] - The token balance
|
||
|
* @param {string} [tokenSymbol] - The token symbol
|
||
4 years ago
|
* @param {Object} [overrides] - A configuration object that allows the caller to explicitly pass an exchange rate or
|
||
4 years ago
|
* 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
|
||
|
* @param {boolean} hideCurrencySymbol Indicates whether the returned formatted amount should include the trailing currency symbol
|
||
5 years ago
|
* @return {string} - The formatted token amount in the user's chosen fiat currency
|
||
|
*/
|
||
4 years ago
|
export function useTokenFiatAmount(
|
||
|
tokenAddress,
|
||
|
tokenAmount,
|
||
|
tokenSymbol,
|
||
|
overrides = {},
|
||
|
hideCurrencySymbol,
|
||
|
) {
|
||
4 years ago
|
const contractExchangeRates = useSelector(getTokenExchangeRates);
|
||
|
const conversionRate = useSelector(getConversionRate);
|
||
|
const currentCurrency = useSelector(getCurrentCurrency);
|
||
|
const userPrefersShownFiat = useSelector(getShouldShowFiat);
|
||
|
const showFiat = overrides.showFiat ?? userPrefersShownFiat;
|
||
4 years ago
|
const tokenExchangeRate =
|
||
4 years ago
|
overrides.exchangeRate ?? contractExchangeRates[tokenAddress];
|
||
5 years ago
|
const formattedFiat = useMemo(
|
||
4 years ago
|
() =>
|
||
|
getTokenFiatAmount(
|
||
|
tokenExchangeRate,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
|
tokenAmount,
|
||
|
tokenSymbol,
|
||
|
true,
|
||
|
hideCurrencySymbol,
|
||
|
),
|
||
|
[
|
||
5 years ago
|
tokenExchangeRate,
|
||
|
conversionRate,
|
||
|
currentCurrency,
|
||
|
tokenAmount,
|
||
4 years ago
|
tokenSymbol,
|
||
4 years ago
|
hideCurrencySymbol,
|
||
4 years ago
|
],
|
||
4 years ago
|
);
|
||
5 years ago
|
|
||
5 years ago
|
if (!showFiat || currentCurrency.toUpperCase() === tokenSymbol) {
|
||
4 years ago
|
return undefined;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
return formattedFiat;
|
||
5 years ago
|
}
|