Create `useTokenFiatAmount` hook (#8778)
This hook is responsible for converting a token balance to fiat. It has been extracted from the `TokenCell` component, and will be used elsewhere in a future PR.feature/default_network_editable
parent
e124a9b467
commit
0b86283c10
@ -0,0 +1,37 @@ |
|||||||
|
import { useMemo } from 'react' |
||||||
|
import { useSelector } from 'react-redux' |
||||||
|
import { getTokenExchangeRates, getConversionRate, getCurrentCurrency } 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 tokenExchangeRate = contractExchangeRates[tokenAddress] |
||||||
|
|
||||||
|
const formattedFiat = useMemo( |
||||||
|
() => getFormattedTokenFiatAmount( |
||||||
|
tokenExchangeRate, |
||||||
|
conversionRate, |
||||||
|
currentCurrency, |
||||||
|
tokenAmount, |
||||||
|
tokenSymbol |
||||||
|
), |
||||||
|
[tokenExchangeRate, conversionRate, currentCurrency, tokenAmount, tokenSymbol] |
||||||
|
) |
||||||
|
|
||||||
|
if (currentCurrency.toUpperCase() === tokenSymbol) { |
||||||
|
return undefined |
||||||
|
} |
||||||
|
|
||||||
|
return formattedFiat |
||||||
|
} |
Loading…
Reference in new issue