2023-03-01 09:45:27 +01:00
|
|
|
import React, { useContext } from 'react';
|
2022-11-28 17:41:42 +01:00
|
|
|
import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import classNames from 'classnames';
|
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
|
|
|
import { useGasFeeContext } from '../../../contexts/gasFee';
|
|
|
|
import { PRIMARY, SECONDARY } from '../../../helpers/constants/common';
|
|
|
|
import UserPreferencedCurrencyDisplay from '../../../components/app/user-preferenced-currency-display';
|
|
|
|
import GasTiming from '../../../components/app/gas-timing';
|
|
|
|
import InfoTooltip from '../../../components/ui/info-tooltip';
|
|
|
|
import Typography from '../../../components/ui/typography';
|
|
|
|
import Button from '../../../components/ui/button';
|
|
|
|
import Box from '../../../components/ui/box';
|
|
|
|
import {
|
2023-02-02 21:15:26 +01:00
|
|
|
TypographyVariant,
|
2022-11-28 17:41:42 +01:00
|
|
|
DISPLAY,
|
|
|
|
FLEX_DIRECTION,
|
|
|
|
BLOCK_SIZES,
|
2023-02-02 21:15:26 +01:00
|
|
|
Color,
|
2022-11-28 17:41:42 +01:00
|
|
|
FONT_STYLE,
|
|
|
|
FONT_WEIGHT,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
2023-01-18 15:47:29 +01:00
|
|
|
import { TokenStandard } from '../../../../shared/constants/transaction';
|
2022-11-28 17:41:42 +01:00
|
|
|
import LoadingHeartBeat from '../../../components/ui/loading-heartbeat';
|
|
|
|
import TransactionDetailItem from '../../../components/app/transaction-detail-item';
|
|
|
|
import { NETWORK_TO_NAME_MAP } from '../../../../shared/constants/network';
|
|
|
|
import TransactionDetail from '../../../components/app/transaction-detail';
|
|
|
|
import ActionableMessage from '../../../components/ui/actionable-message';
|
|
|
|
import {
|
|
|
|
getProvider,
|
|
|
|
getPreferences,
|
|
|
|
getIsBuyableChain,
|
|
|
|
transactionFeeSelector,
|
|
|
|
getIsMainnet,
|
2023-01-18 16:43:40 +01:00
|
|
|
getIsTestnet,
|
2023-01-17 16:23:04 +01:00
|
|
|
getUseCurrencyRateCheck,
|
2022-11-28 17:41:42 +01:00
|
|
|
} from '../../../selectors';
|
|
|
|
|
|
|
|
import { INSUFFICIENT_TOKENS_ERROR } from '../send.constants';
|
|
|
|
import { getCurrentDraftTransaction } from '../../../ducks/send';
|
2023-02-21 14:08:47 +01:00
|
|
|
import { getNativeCurrency } from '../../../ducks/metamask/metamask';
|
2022-11-28 17:41:42 +01:00
|
|
|
import { showModal } from '../../../store/actions';
|
2023-01-20 18:04:37 +01:00
|
|
|
import {
|
|
|
|
addHexes,
|
|
|
|
hexWEIToDecETH,
|
|
|
|
hexWEIToDecGWEI,
|
|
|
|
} from '../../../../shared/modules/conversion.utils';
|
2023-03-01 09:45:27 +01:00
|
|
|
import { EVENT, EVENT_NAMES } from '../../../../shared/constants/metametrics';
|
|
|
|
import { MetaMetricsContext } from '../../../contexts/metametrics';
|
|
|
|
import useRamps from '../../../hooks/experiences/useRamps';
|
2022-11-28 17:41:42 +01:00
|
|
|
|
|
|
|
export default function GasDisplay({ gasError }) {
|
|
|
|
const t = useContext(I18nContext);
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { estimateUsed } = useGasFeeContext();
|
2023-03-01 09:45:27 +01:00
|
|
|
const trackEvent = useContext(MetaMetricsContext);
|
|
|
|
|
|
|
|
const { openBuyCryptoInPdapp } = useRamps();
|
2023-01-18 16:43:40 +01:00
|
|
|
|
2022-11-28 17:41:42 +01:00
|
|
|
const currentProvider = useSelector(getProvider);
|
|
|
|
const isMainnet = useSelector(getIsMainnet);
|
2023-01-18 16:43:40 +01:00
|
|
|
const isTestnet = useSelector(getIsTestnet);
|
2022-11-28 17:41:42 +01:00
|
|
|
const isBuyableChain = useSelector(getIsBuyableChain);
|
|
|
|
const draftTransaction = useSelector(getCurrentDraftTransaction);
|
2023-01-17 16:23:04 +01:00
|
|
|
const useCurrencyRateCheck = useSelector(getUseCurrencyRateCheck);
|
2023-01-18 16:43:40 +01:00
|
|
|
const { showFiatInTestnets, useNativeCurrencyAsPrimaryCurrency } =
|
|
|
|
useSelector(getPreferences);
|
2023-01-27 18:17:47 +01:00
|
|
|
const { provider, unapprovedTxs } = useSelector((state) => state.metamask);
|
2023-02-21 14:08:47 +01:00
|
|
|
const nativeCurrency = useSelector(getNativeCurrency);
|
2022-11-28 17:41:42 +01:00
|
|
|
const { chainId } = provider;
|
|
|
|
const networkName = NETWORK_TO_NAME_MAP[chainId];
|
|
|
|
const isInsufficientTokenError =
|
|
|
|
draftTransaction?.amount.error === INSUFFICIENT_TOKENS_ERROR;
|
|
|
|
const editingTransaction = unapprovedTxs[draftTransaction.id];
|
2022-12-16 17:17:01 +01:00
|
|
|
const currentNetworkName = networkName || currentProvider.nickname;
|
2022-11-28 17:41:42 +01:00
|
|
|
|
|
|
|
const transactionData = {
|
|
|
|
txParams: {
|
|
|
|
gasPrice: draftTransaction.gas?.gasPrice,
|
|
|
|
gas: editingTransaction?.userEditedGasLimit
|
|
|
|
? editingTransaction?.txParams?.gas
|
|
|
|
: draftTransaction.gas?.gasLimit,
|
|
|
|
maxFeePerGas: editingTransaction?.txParams?.maxFeePerGas
|
|
|
|
? editingTransaction?.txParams?.maxFeePerGas
|
|
|
|
: draftTransaction.gas?.maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: editingTransaction?.txParams?.maxPriorityFeePerGas
|
|
|
|
? editingTransaction?.txParams?.maxPriorityFeePerGas
|
|
|
|
: draftTransaction.gas?.maxPriorityFeePerGas,
|
|
|
|
value: draftTransaction.amount?.value,
|
|
|
|
type: draftTransaction.transactionType,
|
|
|
|
},
|
|
|
|
userFeeLevel: editingTransaction?.userFeeLevel,
|
|
|
|
};
|
|
|
|
|
|
|
|
const {
|
|
|
|
hexMinimumTransactionFee,
|
|
|
|
hexMaximumTransactionFee,
|
|
|
|
hexTransactionTotal,
|
|
|
|
} = useSelector((state) => transactionFeeSelector(state, transactionData));
|
|
|
|
|
|
|
|
let title;
|
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction?.asset.details?.standard === TokenStandard.ERC721 ||
|
|
|
|
draftTransaction?.asset.details?.standard === TokenStandard.ERC1155
|
2022-11-28 17:41:42 +01:00
|
|
|
) {
|
|
|
|
title = draftTransaction?.asset.details?.name;
|
2023-01-18 15:47:29 +01:00
|
|
|
} else if (
|
|
|
|
draftTransaction?.asset.details?.standard === TokenStandard.ERC20
|
|
|
|
) {
|
2022-11-28 17:41:42 +01:00
|
|
|
title = `${hexWEIToDecETH(draftTransaction.amount.value)} ${
|
|
|
|
draftTransaction?.asset.details?.symbol
|
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ethTransactionTotalMaxAmount = Number(
|
|
|
|
hexWEIToDecETH(hexMaximumTransactionFee),
|
|
|
|
);
|
|
|
|
|
|
|
|
const primaryTotalTextOverrideMaxAmount = `${title} + ${ethTransactionTotalMaxAmount} ${nativeCurrency}`;
|
|
|
|
|
2023-01-18 16:43:40 +01:00
|
|
|
const showCurrencyRateCheck =
|
|
|
|
useCurrencyRateCheck && (!isTestnet || showFiatInTestnets);
|
|
|
|
|
2022-11-28 17:41:42 +01:00
|
|
|
let detailTotal, maxAmount;
|
|
|
|
|
|
|
|
if (draftTransaction?.asset.type === 'NATIVE') {
|
|
|
|
detailTotal = (
|
|
|
|
<Box
|
|
|
|
height={BLOCK_SIZES.MAX}
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
className="gas-display__total-value"
|
|
|
|
>
|
|
|
|
<LoadingHeartBeat estimateUsed={transactionData?.userFeeLevel} />
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={PRIMARY}
|
|
|
|
key="total-detail-value"
|
|
|
|
value={hexTransactionTotal}
|
|
|
|
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
maxAmount = (
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={PRIMARY}
|
|
|
|
key="total-max-amount"
|
|
|
|
value={addHexes(
|
|
|
|
draftTransaction.amount.value,
|
|
|
|
hexMaximumTransactionFee,
|
|
|
|
)}
|
|
|
|
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else if (useNativeCurrencyAsPrimaryCurrency) {
|
|
|
|
detailTotal = primaryTotalTextOverrideMaxAmount;
|
|
|
|
maxAmount = primaryTotalTextOverrideMaxAmount;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Box className="gas-display">
|
|
|
|
<TransactionDetail
|
|
|
|
userAcknowledgedGasMissing={false}
|
|
|
|
rows={[
|
2022-12-08 19:37:06 +01:00
|
|
|
<TransactionDetailItem
|
|
|
|
key="gas-item"
|
|
|
|
detailTitle={
|
|
|
|
<Box display={DISPLAY.FLEX}>
|
|
|
|
<Box marginRight={1}>{t('gas')}</Box>
|
|
|
|
<Typography
|
|
|
|
as="span"
|
|
|
|
marginTop={0}
|
2023-02-02 21:15:26 +01:00
|
|
|
color={Color.textMuted}
|
2022-12-08 19:37:06 +01:00
|
|
|
fontStyle={FONT_STYLE.ITALIC}
|
|
|
|
fontWeight={FONT_WEIGHT.NORMAL}
|
|
|
|
className="gas-display__title__estimate"
|
2022-11-28 17:41:42 +01:00
|
|
|
>
|
2022-12-08 19:37:06 +01:00
|
|
|
({t('transactionDetailGasInfoV2')})
|
|
|
|
</Typography>
|
|
|
|
<InfoTooltip
|
|
|
|
contentText={
|
|
|
|
<>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H7}>
|
2022-12-08 19:37:06 +01:00
|
|
|
{t('transactionDetailGasTooltipIntro', [
|
|
|
|
isMainnet ? t('networkNameEthereum') : '',
|
|
|
|
])}
|
|
|
|
</Typography>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H7}>
|
2022-12-08 19:37:06 +01:00
|
|
|
{t('transactionDetailGasTooltipExplanation')}
|
|
|
|
</Typography>
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H7}>
|
2022-12-08 19:37:06 +01:00
|
|
|
<a
|
|
|
|
href="https://community.metamask.io/t/what-is-gas-why-do-transactions-take-so-long/3172"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
|
|
|
{t('transactionDetailGasTooltipConversion')}
|
|
|
|
</a>
|
|
|
|
</Typography>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
position="right"
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
}
|
2023-02-02 21:15:26 +01:00
|
|
|
detailTitleColor={Color.textDefault}
|
2022-12-08 19:37:06 +01:00
|
|
|
detailText={
|
2023-01-18 16:43:40 +01:00
|
|
|
showCurrencyRateCheck && (
|
2023-01-17 16:23:04 +01:00
|
|
|
<Box className="gas-display__currency-container">
|
|
|
|
<LoadingHeartBeat estimateUsed={estimateUsed} />
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
value={hexMinimumTransactionFee}
|
|
|
|
hideLabel={Boolean(useNativeCurrencyAsPrimaryCurrency)}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)
|
2022-12-08 19:37:06 +01:00
|
|
|
}
|
|
|
|
detailTotal={
|
|
|
|
<Box className="gas-display__currency-container">
|
|
|
|
<LoadingHeartBeat estimateUsed={estimateUsed} />
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={PRIMARY}
|
|
|
|
value={hexMinimumTransactionFee}
|
|
|
|
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
subText={
|
|
|
|
<>
|
2022-11-28 17:41:42 +01:00
|
|
|
<Box
|
2022-12-08 19:37:06 +01:00
|
|
|
key="editGasSubTextFeeLabel"
|
|
|
|
display={DISPLAY.INLINE_FLEX}
|
|
|
|
className={classNames('gas-display__gas-fee-label', {
|
|
|
|
'gas-display__gas-fee-warning': estimateUsed === 'high',
|
|
|
|
})}
|
2022-11-28 17:41:42 +01:00
|
|
|
>
|
2022-12-08 19:37:06 +01:00
|
|
|
<LoadingHeartBeat estimateUsed={estimateUsed} />
|
|
|
|
<Box marginRight={1}>
|
|
|
|
<strong>
|
|
|
|
{estimateUsed === 'high' && '⚠ '}
|
|
|
|
{t('editGasSubTextFeeLabel')}
|
|
|
|
</strong>
|
|
|
|
</Box>
|
2022-11-28 17:41:42 +01:00
|
|
|
<Box
|
2022-12-08 19:37:06 +01:00
|
|
|
key="editGasSubTextFeeValue"
|
2022-11-28 17:41:42 +01:00
|
|
|
className="gas-display__currency-container"
|
|
|
|
>
|
2022-12-08 19:37:06 +01:00
|
|
|
<LoadingHeartBeat estimateUsed={estimateUsed} />
|
2022-11-28 17:41:42 +01:00
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
key="editGasSubTextFeeAmount"
|
|
|
|
type={PRIMARY}
|
|
|
|
value={hexMaximumTransactionFee}
|
|
|
|
hideLabel={!useNativeCurrencyAsPrimaryCurrency}
|
|
|
|
/>
|
|
|
|
</Box>
|
2022-12-08 19:37:06 +01:00
|
|
|
</Box>
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
subTitle={
|
|
|
|
<GasTiming
|
|
|
|
maxPriorityFeePerGas={hexWEIToDecGWEI(
|
|
|
|
draftTransaction.gas.maxPriorityFeePerGas,
|
|
|
|
)}
|
|
|
|
maxFeePerGas={hexWEIToDecGWEI(
|
|
|
|
draftTransaction.gas.maxFeePerGas,
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>,
|
2022-11-28 17:41:42 +01:00
|
|
|
(gasError || isInsufficientTokenError) && (
|
|
|
|
<TransactionDetailItem
|
|
|
|
key="total-item"
|
|
|
|
detailTitle={t('total')}
|
|
|
|
detailText={
|
2023-01-18 16:43:40 +01:00
|
|
|
showCurrencyRateCheck && (
|
2023-01-17 16:23:04 +01:00
|
|
|
<Box
|
|
|
|
height={BLOCK_SIZES.MAX}
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
className="gas-display__total-value"
|
|
|
|
>
|
|
|
|
<LoadingHeartBeat
|
|
|
|
estimateUsed={transactionData?.userFeeLevel}
|
|
|
|
/>
|
|
|
|
<UserPreferencedCurrencyDisplay
|
|
|
|
type={SECONDARY}
|
|
|
|
key="total-detail-text"
|
|
|
|
value={hexTransactionTotal}
|
|
|
|
hideLabel={Boolean(useNativeCurrencyAsPrimaryCurrency)}
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
)
|
2022-11-28 17:41:42 +01:00
|
|
|
}
|
|
|
|
detailTotal={detailTotal}
|
|
|
|
subTitle={t('transactionDetailGasTotalSubtitle')}
|
|
|
|
subText={
|
|
|
|
<Box
|
|
|
|
height={BLOCK_SIZES.MAX}
|
|
|
|
display={DISPLAY.FLEX}
|
|
|
|
flexDirection={FLEX_DIRECTION.COLUMN}
|
|
|
|
className="gas-display__total-amount"
|
|
|
|
>
|
|
|
|
<LoadingHeartBeat
|
|
|
|
estimateUsed={
|
|
|
|
transactionData?.userFeeLevel ?? estimateUsed
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<strong key="editGasSubTextAmountLabel">
|
|
|
|
{t('editGasSubTextAmountLabel')}
|
|
|
|
</strong>{' '}
|
|
|
|
{maxAmount}
|
|
|
|
</Box>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Box>
|
2022-12-16 17:17:01 +01:00
|
|
|
{(gasError || isInsufficientTokenError) && currentNetworkName && (
|
2022-12-06 17:49:08 +01:00
|
|
|
<Box
|
|
|
|
className="gas-display__warning-message"
|
|
|
|
data-testid="gas-warning-message"
|
|
|
|
>
|
2022-11-28 17:41:42 +01:00
|
|
|
<Box
|
|
|
|
paddingTop={0}
|
|
|
|
paddingRight={4}
|
|
|
|
paddingBottom={4}
|
|
|
|
paddingLeft={4}
|
|
|
|
className="gas-display__confirm-approve-content__warning"
|
|
|
|
>
|
|
|
|
<ActionableMessage
|
|
|
|
message={
|
|
|
|
isBuyableChain && draftTransaction.asset.type === 'NATIVE' ? (
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H7} align="left">
|
2022-11-28 17:41:42 +01:00
|
|
|
{t('insufficientCurrencyBuyOrReceive', [
|
|
|
|
nativeCurrency,
|
2022-12-16 17:17:01 +01:00
|
|
|
currentNetworkName,
|
2022-11-28 17:41:42 +01:00
|
|
|
<Button
|
|
|
|
type="inline"
|
|
|
|
className="confirm-page-container-content__link"
|
|
|
|
onClick={() => {
|
2023-03-01 09:45:27 +01:00
|
|
|
openBuyCryptoInPdapp();
|
|
|
|
trackEvent({
|
|
|
|
event: EVENT_NAMES.NAV_BUY_BUTTON_CLICKED,
|
|
|
|
category: EVENT.CATEGORIES.NAVIGATION,
|
|
|
|
properties: {
|
|
|
|
location: 'Gas Warning Insufficient Funds',
|
|
|
|
text: 'Buy',
|
|
|
|
},
|
|
|
|
});
|
2022-11-28 17:41:42 +01:00
|
|
|
}}
|
|
|
|
key={`${nativeCurrency}-buy-button`}
|
|
|
|
>
|
|
|
|
{t('buyAsset', [nativeCurrency])}
|
|
|
|
</Button>,
|
|
|
|
<Button
|
|
|
|
type="inline"
|
|
|
|
className="gas-display__link"
|
|
|
|
onClick={() =>
|
|
|
|
dispatch(showModal({ name: 'ACCOUNT_DETAILS' }))
|
|
|
|
}
|
|
|
|
key="receive-button"
|
|
|
|
>
|
|
|
|
{t('deposit')}
|
|
|
|
</Button>,
|
|
|
|
])}
|
|
|
|
</Typography>
|
|
|
|
) : (
|
2023-02-02 21:15:26 +01:00
|
|
|
<Typography variant={TypographyVariant.H7} align="left">
|
2022-11-28 17:41:42 +01:00
|
|
|
{t('insufficientCurrencyBuyOrReceive', [
|
2023-02-21 14:08:47 +01:00
|
|
|
nativeCurrency,
|
2022-12-16 17:17:01 +01:00
|
|
|
currentNetworkName,
|
2023-02-21 14:08:47 +01:00
|
|
|
`${t('buyAsset', [nativeCurrency])}`,
|
2022-11-28 17:41:42 +01:00
|
|
|
<Button
|
|
|
|
type="inline"
|
|
|
|
className="gas-display__link"
|
|
|
|
onClick={() =>
|
|
|
|
dispatch(showModal({ name: 'ACCOUNT_DETAILS' }))
|
|
|
|
}
|
|
|
|
key="receive-button"
|
|
|
|
>
|
|
|
|
{t('deposit')}
|
|
|
|
</Button>,
|
|
|
|
])}
|
|
|
|
</Typography>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
useIcon
|
|
|
|
iconFillColor="var(--color-error-default)"
|
|
|
|
type="danger"
|
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
)}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
GasDisplay.propTypes = {
|
|
|
|
gasError: PropTypes.string,
|
|
|
|
};
|