1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/app/hooks/useTokenFiatAmount.js
Mark Stacey a100c55e64
Hide token fiat amounts on testnets (#8792)
The token amount is no longer shown in fiat on testnets, unless the
user has enabled the "Show fiat on testnets" setting.
2020-06-12 15:47:23 -03:00

39 lines
1.3 KiB
JavaScript

import { useMemo } from 'react'
import { useSelector } from 'react-redux'
import { getTokenExchangeRates, getConversionRate, getCurrentCurrency, getShouldShowFiat } from '../selectors'
import { getFormattedTokenFiatAmount } from '../helpers/utils/token-util'
/**
* Get the token balance converted to fiat and formatted for display
*
* @param {string} [tokenAddress] - The token address
* @param {string} [tokenAmount] - The token balance
* @param {string} [tokenSymbol] - The token symbol
* @return {string} - The formatted token amount in the user's chosen fiat currency
*/
export function useTokenFiatAmount (tokenAddress, tokenAmount, tokenSymbol) {
const contractExchangeRates = useSelector(getTokenExchangeRates)
const conversionRate = useSelector(getConversionRate)
const currentCurrency = useSelector(getCurrentCurrency)
const showFiat = useSelector(getShouldShowFiat)
const tokenExchangeRate = contractExchangeRates[tokenAddress]
const formattedFiat = useMemo(
() => getFormattedTokenFiatAmount(
tokenExchangeRate,
conversionRate,
currentCurrency,
tokenAmount,
tokenSymbol
),
[tokenExchangeRate, conversionRate, currentCurrency, tokenAmount, tokenSymbol]
)
if (!showFiat || currentCurrency.toUpperCase() === tokenSymbol) {
return undefined
}
return formattedFiat
}