2022-01-19 19:34:22 +01:00
|
|
|
import React, { useMemo } from 'react';
|
2021-12-03 16:44:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2021-12-12 00:26:28 +01:00
|
|
|
import {
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
PRIORITY_LEVELS,
|
|
|
|
} from '../../../../../shared/constants/gas';
|
2021-12-03 16:44:23 +01:00
|
|
|
import {
|
|
|
|
COLORS,
|
|
|
|
FONT_WEIGHT,
|
2021-12-06 16:40:30 +01:00
|
|
|
TYPOGRAPHY,
|
2021-12-03 16:44:23 +01:00
|
|
|
} from '../../../../helpers/constants/design-system';
|
2022-01-31 06:51:43 +01:00
|
|
|
import { isMetamaskSuggestedGasEstimate } from '../../../../helpers/utils/gas';
|
2022-01-22 01:23:02 +01:00
|
|
|
import { roundToDecimalPlacesRemovingExtraZeroes } from '../../../../helpers/utils/util';
|
2021-12-03 16:44:23 +01:00
|
|
|
import Typography from '../../../ui/typography';
|
|
|
|
|
|
|
|
const EditGasToolTip = ({
|
2022-01-06 03:47:26 +01:00
|
|
|
editGasMode,
|
|
|
|
estimateGreaterThanGasUse,
|
2021-12-12 00:26:28 +01:00
|
|
|
gasLimit,
|
2021-12-03 16:44:23 +01:00
|
|
|
priorityLevel,
|
|
|
|
// maxFeePerGas & maxPriorityFeePerGas are derived from conditional logic
|
|
|
|
// related to the source of the estimates. We pass these values from the
|
|
|
|
// the parent component (edit-gas-item) rather than recalculate them
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
2021-12-12 00:26:28 +01:00
|
|
|
transaction,
|
2021-12-03 16:44:23 +01:00
|
|
|
t,
|
|
|
|
}) => {
|
2022-01-19 19:34:22 +01:00
|
|
|
const toolTipMessage = useMemo(() => {
|
2021-12-03 16:44:23 +01:00
|
|
|
switch (priorityLevel) {
|
|
|
|
case PRIORITY_LEVELS.LOW:
|
|
|
|
return t('lowGasSettingToolTipMessage', [
|
|
|
|
<span key={priorityLevel}>
|
|
|
|
<b>{t('low')}</b>
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
case PRIORITY_LEVELS.MEDIUM:
|
2022-01-06 03:47:26 +01:00
|
|
|
if (estimateGreaterThanGasUse) {
|
|
|
|
return t('disabledGasOptionToolTipMessage', [
|
|
|
|
<span key={`disabled-priority-level-${priorityLevel}`}>
|
|
|
|
{t(priorityLevel)}
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
}
|
2021-12-03 16:44:23 +01:00
|
|
|
return t('mediumGasSettingToolTipMessage', [
|
|
|
|
<span key={priorityLevel}>
|
|
|
|
<b>{t('medium')}</b>
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
case PRIORITY_LEVELS.HIGH:
|
2022-01-06 03:47:26 +01:00
|
|
|
if (estimateGreaterThanGasUse) {
|
|
|
|
return t('disabledGasOptionToolTipMessage', [
|
|
|
|
<span key={`disabled-priority-level-${priorityLevel}`}>
|
|
|
|
{t(priorityLevel)}
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
if (editGasMode === EDIT_GAS_MODES.SWAPS) {
|
|
|
|
return t('swapSuggestedGasSettingToolTipMessage');
|
|
|
|
}
|
2021-12-03 16:44:23 +01:00
|
|
|
return t('highGasSettingToolTipMessage', [
|
|
|
|
<span key={priorityLevel}>
|
|
|
|
<b>{t('high')}</b>
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
case PRIORITY_LEVELS.CUSTOM:
|
|
|
|
return t('customGasSettingToolTipMessage', [
|
|
|
|
<span key={priorityLevel}>
|
|
|
|
<b>{t('custom')}</b>
|
|
|
|
</span>,
|
|
|
|
]);
|
|
|
|
case PRIORITY_LEVELS.DAPP_SUGGESTED:
|
|
|
|
return transaction?.origin
|
|
|
|
? t('dappSuggestedGasSettingToolTipMessage', [
|
|
|
|
<span key={transaction?.origin}>{transaction?.origin}</span>,
|
|
|
|
])
|
|
|
|
: null;
|
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
2022-01-19 19:34:22 +01:00
|
|
|
}, [editGasMode, estimateGreaterThanGasUse, priorityLevel, transaction, t]);
|
|
|
|
|
2022-01-26 02:34:58 +01:00
|
|
|
let imgAltText;
|
|
|
|
if (priorityLevel === PRIORITY_LEVELS.LOW) {
|
|
|
|
imgAltText = t('curveLowGasEstimate');
|
|
|
|
} else if (priorityLevel === PRIORITY_LEVELS.MEDIUM) {
|
|
|
|
imgAltText = t('curveMediumGasEstimate');
|
|
|
|
} else if (priorityLevel === PRIORITY_LEVELS.HIGH) {
|
|
|
|
imgAltText = t('curveHighGasEstimate');
|
|
|
|
}
|
|
|
|
|
2022-01-31 06:51:43 +01:00
|
|
|
// Gas estimate curve is visible for low/medium/high gas estimates
|
|
|
|
// the curve is not visible for high estimates for swaps
|
|
|
|
// also it is not visible in case of cancel/speedup if the medium/high option is disabled
|
|
|
|
const showGasEstimateCurve =
|
|
|
|
isMetamaskSuggestedGasEstimate(priorityLevel) &&
|
|
|
|
!(
|
|
|
|
priorityLevel === PRIORITY_LEVELS.HIGH &&
|
|
|
|
editGasMode === EDIT_GAS_MODES.SWAPS
|
|
|
|
) &&
|
|
|
|
!estimateGreaterThanGasUse;
|
|
|
|
|
2021-12-03 16:44:23 +01:00
|
|
|
return (
|
|
|
|
<div className="edit-gas-tooltip__container">
|
2022-01-31 06:51:43 +01:00
|
|
|
{showGasEstimateCurve ? (
|
2022-01-26 02:34:58 +01:00
|
|
|
<img alt={imgAltText} src={`./images/curve-${priorityLevel}.svg`} />
|
2021-12-03 16:44:23 +01:00
|
|
|
) : null}
|
2022-01-19 19:34:22 +01:00
|
|
|
{toolTipMessage && (
|
|
|
|
<div className="edit-gas-tooltip__container__message">
|
|
|
|
<Typography variant={TYPOGRAPHY.H7}>{toolTipMessage}</Typography>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-01-06 03:47:26 +01:00
|
|
|
{priorityLevel === PRIORITY_LEVELS.CUSTOM ||
|
|
|
|
estimateGreaterThanGasUse ? null : (
|
2021-12-03 16:44:23 +01:00
|
|
|
<div className="edit-gas-tooltip__container__values">
|
|
|
|
<div>
|
|
|
|
<Typography
|
2021-12-06 16:40:30 +01:00
|
|
|
variant={TYPOGRAPHY.H7}
|
2021-12-03 16:44:23 +01:00
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
className="edit-gas-tooltip__container__label"
|
|
|
|
>
|
|
|
|
{t('maxBaseFee')}
|
|
|
|
</Typography>
|
2022-01-19 19:34:22 +01:00
|
|
|
{maxFeePerGas && (
|
|
|
|
<Typography
|
|
|
|
variant={TYPOGRAPHY.H7}
|
2022-03-09 16:24:53 +01:00
|
|
|
color={COLORS.TEXT_ALTERNATIVE}
|
2022-01-19 19:34:22 +01:00
|
|
|
className="edit-gas-tooltip__container__value"
|
|
|
|
>
|
2022-01-22 01:23:02 +01:00
|
|
|
{roundToDecimalPlacesRemovingExtraZeroes(maxFeePerGas, 4)}
|
2022-01-19 19:34:22 +01:00
|
|
|
</Typography>
|
|
|
|
)}
|
2021-12-03 16:44:23 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<Typography
|
2021-12-06 16:40:30 +01:00
|
|
|
variant={TYPOGRAPHY.H7}
|
2021-12-03 16:44:23 +01:00
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
className="edit-gas-tooltip__container__label"
|
|
|
|
>
|
2021-12-06 17:02:23 +01:00
|
|
|
{t('priorityFeeProperCase')}
|
2021-12-03 16:44:23 +01:00
|
|
|
</Typography>
|
2022-01-19 19:34:22 +01:00
|
|
|
{maxPriorityFeePerGas && (
|
|
|
|
<Typography
|
|
|
|
variant={TYPOGRAPHY.H7}
|
2022-03-09 16:24:53 +01:00
|
|
|
color={COLORS.TEXT_ALTERNATIVE}
|
2022-01-19 19:34:22 +01:00
|
|
|
className="edit-gas-tooltip__container__value"
|
|
|
|
>
|
2022-01-22 01:23:02 +01:00
|
|
|
{roundToDecimalPlacesRemovingExtraZeroes(
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
4,
|
|
|
|
)}
|
2022-01-19 19:34:22 +01:00
|
|
|
</Typography>
|
|
|
|
)}
|
2021-12-03 16:44:23 +01:00
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<Typography
|
2021-12-06 16:40:30 +01:00
|
|
|
variant={TYPOGRAPHY.H7}
|
2021-12-03 16:44:23 +01:00
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
className="edit-gas-tooltip__container__label"
|
|
|
|
>
|
|
|
|
{t('gasLimit')}
|
|
|
|
</Typography>
|
2022-01-19 19:34:22 +01:00
|
|
|
{gasLimit && (
|
|
|
|
<Typography
|
|
|
|
variant={TYPOGRAPHY.H7}
|
2022-03-09 16:24:53 +01:00
|
|
|
color={COLORS.TEXT_ALTERNATIVE}
|
2022-01-19 19:34:22 +01:00
|
|
|
className="edit-gas-tooltip__container__value"
|
|
|
|
>
|
2022-01-22 01:23:02 +01:00
|
|
|
{roundToDecimalPlacesRemovingExtraZeroes(gasLimit, 4)}
|
2022-01-19 19:34:22 +01:00
|
|
|
</Typography>
|
|
|
|
)}
|
2021-12-03 16:44:23 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
EditGasToolTip.propTypes = {
|
2022-01-06 03:47:26 +01:00
|
|
|
estimateGreaterThanGasUse: PropTypes.bool,
|
2021-12-03 16:44:23 +01:00
|
|
|
priorityLevel: PropTypes.string,
|
2022-01-06 03:47:26 +01:00
|
|
|
maxFeePerGas: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
|
|
|
|
maxPriorityFeePerGas: PropTypes.oneOfType([
|
|
|
|
PropTypes.number,
|
|
|
|
PropTypes.string,
|
|
|
|
]),
|
2021-12-03 16:44:23 +01:00
|
|
|
t: PropTypes.func,
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode: PropTypes.string,
|
|
|
|
gasLimit: PropTypes.number,
|
|
|
|
transaction: PropTypes.object,
|
2021-12-03 16:44:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default EditGasToolTip;
|