mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-30 08:09:15 +01:00
3732c5f71e
ESLint rules have been added to enforce our JSDoc conventions. These rules were introduced by updating `@metamask/eslint-config` to v9. Some of the rules have been disabled because the effort to fix all lint errors was too high. It might be easiest to enable these rules one directory at a time, or one rule at a time. Most of the changes in this PR were a result of running `yarn lint:fix`. There were a handful of manual changes that seemed obvious and simple to make. Anything beyond that and the rule was left disabled.
74 lines
2.5 KiB
JavaScript
74 lines
2.5 KiB
JavaScript
import { useMemo } from 'react';
|
|
import { shallowEqual, useSelector } from 'react-redux';
|
|
import {
|
|
getTokenExchangeRates,
|
|
getCurrentCurrency,
|
|
getShouldShowFiat,
|
|
} from '../selectors';
|
|
import { getTokenFiatAmount } from '../helpers/utils/token-util';
|
|
import { getConversionRate } from '../ducks/metamask/metamask';
|
|
import { isEqualCaseInsensitive } from '../helpers/utils/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
|
|
* @param {Object} [overrides] - A configuration object that allows the caller to explicitly pass an exchange rate or
|
|
* 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
|
|
* @returns {string} The formatted token amount in the user's chosen fiat currency
|
|
*/
|
|
export function useTokenFiatAmount(
|
|
tokenAddress,
|
|
tokenAmount,
|
|
tokenSymbol,
|
|
overrides = {},
|
|
hideCurrencySymbol,
|
|
) {
|
|
const contractExchangeRates = useSelector(
|
|
getTokenExchangeRates,
|
|
shallowEqual,
|
|
);
|
|
const conversionRate = useSelector(getConversionRate);
|
|
const currentCurrency = useSelector(getCurrentCurrency);
|
|
const userPrefersShownFiat = useSelector(getShouldShowFiat);
|
|
const showFiat = overrides.showFiat ?? userPrefersShownFiat;
|
|
const contractExchangeTokenKey = Object.keys(
|
|
contractExchangeRates,
|
|
).find((key) => isEqualCaseInsensitive(key, tokenAddress));
|
|
const tokenExchangeRate =
|
|
overrides.exchangeRate ??
|
|
(contractExchangeTokenKey &&
|
|
contractExchangeRates[contractExchangeTokenKey]);
|
|
const formattedFiat = useMemo(
|
|
() =>
|
|
getTokenFiatAmount(
|
|
tokenExchangeRate,
|
|
conversionRate,
|
|
currentCurrency,
|
|
tokenAmount,
|
|
tokenSymbol,
|
|
true,
|
|
hideCurrencySymbol,
|
|
),
|
|
[
|
|
tokenExchangeRate,
|
|
conversionRate,
|
|
currentCurrency,
|
|
tokenAmount,
|
|
tokenSymbol,
|
|
hideCurrencySymbol,
|
|
],
|
|
);
|
|
|
|
if (!showFiat || currentCurrency.toUpperCase() === tokenSymbol) {
|
|
return undefined;
|
|
}
|
|
|
|
return formattedFiat;
|
|
}
|