2020-06-10 21:04:29 +02:00
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { useSelector } from 'react-redux'
|
2020-06-12 20:47:23 +02:00
|
|
|
import { getTokenExchangeRates, getConversionRate, getCurrentCurrency, getShouldShowFiat } from '../selectors'
|
2020-06-10 21:04:29 +02:00
|
|
|
import { getFormattedTokenFiatAmount } from '../helpers/utils/token-util'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @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)
|
2020-06-12 20:47:23 +02:00
|
|
|
const showFiat = useSelector(getShouldShowFiat)
|
2020-06-10 21:04:29 +02:00
|
|
|
|
|
|
|
const tokenExchangeRate = contractExchangeRates[tokenAddress]
|
|
|
|
|
|
|
|
const formattedFiat = useMemo(
|
|
|
|
() => getFormattedTokenFiatAmount(
|
|
|
|
tokenExchangeRate,
|
|
|
|
conversionRate,
|
|
|
|
currentCurrency,
|
|
|
|
tokenAmount,
|
2020-07-14 17:20:41 +02:00
|
|
|
tokenSymbol,
|
2020-06-10 21:04:29 +02:00
|
|
|
),
|
2020-07-14 17:20:41 +02:00
|
|
|
[tokenExchangeRate, conversionRate, currentCurrency, tokenAmount, tokenSymbol],
|
2020-06-10 21:04:29 +02:00
|
|
|
)
|
|
|
|
|
2020-06-12 20:47:23 +02:00
|
|
|
if (!showFiat || currentCurrency.toUpperCase() === tokenSymbol) {
|
2020-06-10 21:04:29 +02:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedFiat
|
|
|
|
}
|