1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 08:09:15 +01:00
metamask-extension/ui/pages/confirm-token-transaction-base/confirm-token-transaction-base.component.js

113 lines
3.3 KiB
JavaScript
Raw Normal View History

import React, { useContext, useMemo } from 'react';
import PropTypes from 'prop-types';
import BigNumber from 'bignumber.js';
import { I18nContext } from '../../contexts/i18n';
import ConfirmTransactionBase from '../confirm-transaction-base';
import UserPreferencedCurrencyDisplay from '../../components/app/user-preferenced-currency-display';
import {
formatCurrency,
convertTokenToFiat,
addFiat,
2018-07-26 21:15:34 +02:00
roundExponential,
} from '../../helpers/utils/confirm-tx.util';
import { getWeiHexFromDecimalValue } from '../../helpers/utils/conversions.util';
import { ETH, PRIMARY } from '../../helpers/constants/common';
2020-11-03 00:41:28 +01:00
export default function ConfirmTokenTransactionBase({
toAddress,
tokenAddress,
tokenAmount = '0',
tokenSymbol,
fiatTransactionTotal,
ethTransactionTotal,
ethTransactionTotalMaxAmount,
contractExchangeRate,
conversionRate,
currentCurrency,
nativeCurrency,
onEdit,
}) {
const t = useContext(I18nContext);
const hexWeiValue = useMemo(() => {
if (tokenAmount === '0' || !contractExchangeRate) {
return '0';
}
2020-11-03 00:41:28 +01:00
const decimalEthValue = new BigNumber(tokenAmount)
.times(
new BigNumber(contractExchangeRate ? String(contractExchangeRate) : 0),
)
.toFixed();
return getWeiHexFromDecimalValue({
value: decimalEthValue,
fromCurrency: ETH,
fromDenomination: ETH,
});
}, [tokenAmount, contractExchangeRate]);
const secondaryTotalTextOverride = useMemo(() => {
if (typeof contractExchangeRate === 'undefined') {
return formatCurrency(fiatTransactionTotal, currentCurrency);
}
const fiatTransactionAmount = convertTokenToFiat({
value: tokenAmount,
toCurrency: currentCurrency,
conversionRate,
contractExchangeRate,
});
const fiatTotal = addFiat(fiatTransactionAmount, fiatTransactionTotal);
const roundedFiatTotal = roundExponential(fiatTotal);
return formatCurrency(roundedFiatTotal, currentCurrency);
2020-11-03 00:41:28 +01:00
}, [
currentCurrency,
conversionRate,
contractExchangeRate,
fiatTransactionTotal,
tokenAmount,
]);
const tokensText = `${tokenAmount} ${tokenSymbol}`;
return (
<ConfirmTransactionBase
toAddress={toAddress}
onEdit={onEdit}
identiconAddress={tokenAddress}
title={tokensText}
subtitleComponent={
2020-11-03 00:41:28 +01:00
contractExchangeRate === undefined ? (
<span>{t('noConversionRateAvailable')}</span>
) : (
<UserPreferencedCurrencyDisplay
value={hexWeiValue}
type={PRIMARY}
showEthLogo
hideLabel
/>
)
}
primaryTotalTextOverride={`${tokensText} + ${ethTransactionTotal} ${nativeCurrency}`}
primaryTotalTextOverrideMaxAmount={`${tokensText} + ${ethTransactionTotalMaxAmount} ${nativeCurrency}`}
secondaryTotalTextOverride={secondaryTotalTextOverride}
/>
);
}
ConfirmTokenTransactionBase.propTypes = {
tokenAddress: PropTypes.string,
toAddress: PropTypes.string,
tokenAmount: PropTypes.string,
tokenSymbol: PropTypes.string,
fiatTransactionTotal: PropTypes.string,
ethTransactionTotal: PropTypes.string,
contractExchangeRate: PropTypes.number,
conversionRate: PropTypes.number,
currentCurrency: PropTypes.string,
onEdit: PropTypes.func,
nativeCurrency: PropTypes.string,
ethTransactionTotalMaxAmount: PropTypes.string,
};