2021-07-12 18:16:03 +02:00
|
|
|
import React, { useContext } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2021-07-31 03:29:21 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
2021-06-28 16:45:08 +02:00
|
|
|
|
2021-06-24 01:39:44 +02:00
|
|
|
import { I18nContext } from '../../../contexts/i18n';
|
|
|
|
import Typography from '../../ui/typography/typography';
|
|
|
|
import {
|
|
|
|
FONT_WEIGHT,
|
|
|
|
TYPOGRAPHY,
|
|
|
|
COLORS,
|
|
|
|
} from '../../../helpers/constants/design-system';
|
2021-07-07 00:19:11 +02:00
|
|
|
import FormField from '../../ui/form-field';
|
2021-07-12 18:16:03 +02:00
|
|
|
import {
|
|
|
|
GAS_ESTIMATE_TYPES,
|
|
|
|
GAS_RECOMMENDATIONS,
|
|
|
|
} from '../../../../shared/constants/gas';
|
2021-07-20 21:34:32 +02:00
|
|
|
import { getGasFormErrorText } from '../../../helpers/constants/gas';
|
2021-08-03 00:52:18 +02:00
|
|
|
import { checkNetworkAndAccountSupports1559 } from '../../../selectors';
|
2021-07-12 18:16:03 +02:00
|
|
|
|
|
|
|
export default function AdvancedGasControls({
|
|
|
|
estimateToUse,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
maxPriorityFee,
|
|
|
|
maxFee,
|
|
|
|
setMaxPriorityFee,
|
|
|
|
setMaxFee,
|
|
|
|
onManualChange,
|
|
|
|
gasLimit,
|
|
|
|
setGasLimit,
|
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
maxPriorityFeeFiat,
|
|
|
|
maxFeeFiat,
|
2021-07-20 21:34:32 +02:00
|
|
|
gasErrors,
|
2021-07-31 15:02:14 +02:00
|
|
|
minimumGasLimit,
|
2021-07-12 18:16:03 +02:00
|
|
|
}) {
|
2021-06-24 01:39:44 +02:00
|
|
|
const t = useContext(I18nContext);
|
2021-08-03 00:52:18 +02:00
|
|
|
const networkAndAccountSupport1559 = useSelector(
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
);
|
2021-06-24 01:39:44 +02:00
|
|
|
|
2021-07-12 18:16:03 +02:00
|
|
|
const suggestedValues = {};
|
|
|
|
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupport1559) {
|
2021-07-31 02:45:18 +02:00
|
|
|
suggestedValues.maxFeePerGas =
|
|
|
|
gasFeeEstimates?.[estimateToUse]?.suggestedMaxFeePerGas ||
|
|
|
|
gasFeeEstimates?.gasPrice;
|
|
|
|
suggestedValues.maxPriorityFeePerGas =
|
|
|
|
gasFeeEstimates?.[estimateToUse]?.suggestedMaxPriorityFeePerGas ||
|
|
|
|
suggestedValues.maxFeePerGas;
|
|
|
|
} else {
|
|
|
|
switch (gasEstimateType) {
|
|
|
|
case GAS_ESTIMATE_TYPES.LEGACY:
|
|
|
|
suggestedValues.gasPrice = gasFeeEstimates?.[estimateToUse];
|
|
|
|
break;
|
|
|
|
case GAS_ESTIMATE_TYPES.ETH_GASPRICE:
|
|
|
|
suggestedValues.gasPrice = gasFeeEstimates?.gasPrice;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-12 18:16:03 +02:00
|
|
|
}
|
2021-06-24 01:39:44 +02:00
|
|
|
|
2021-07-31 03:29:21 +02:00
|
|
|
const showFeeMarketFields =
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupport1559 &&
|
2021-07-31 03:29:21 +02:00
|
|
|
(gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET ||
|
|
|
|
gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE);
|
2021-06-28 16:45:08 +02:00
|
|
|
|
2021-06-24 01:39:44 +02:00
|
|
|
return (
|
|
|
|
<div className="advanced-gas-controls">
|
2021-07-07 00:19:11 +02:00
|
|
|
<FormField
|
2021-06-24 01:39:44 +02:00
|
|
|
titleText={t('gasLimit')}
|
2021-07-20 21:34:32 +02:00
|
|
|
error={
|
|
|
|
gasErrors?.gasLimit
|
2021-07-30 13:35:30 +02:00
|
|
|
? getGasFormErrorText(gasErrors.gasLimit, t, { minimumGasLimit })
|
2021-07-20 21:34:32 +02:00
|
|
|
: null
|
|
|
|
}
|
2021-08-02 22:28:30 +02:00
|
|
|
onChange={(value) => {
|
|
|
|
onManualChange?.();
|
|
|
|
setGasLimit(value);
|
|
|
|
}}
|
2021-07-15 19:23:37 +02:00
|
|
|
tooltipText={t('editGasLimitTooltip')}
|
2021-06-24 01:39:44 +02:00
|
|
|
value={gasLimit}
|
2021-07-29 17:59:14 +02:00
|
|
|
allowDecimals={false}
|
2021-07-07 00:19:11 +02:00
|
|
|
numeric
|
2021-07-12 18:16:03 +02:00
|
|
|
autoFocus
|
2021-06-24 01:39:44 +02:00
|
|
|
/>
|
2021-07-12 18:16:03 +02:00
|
|
|
{showFeeMarketFields ? (
|
2021-06-28 16:45:08 +02:00
|
|
|
<>
|
2021-07-07 00:19:11 +02:00
|
|
|
<FormField
|
2021-06-28 16:45:08 +02:00
|
|
|
titleText={t('maxPriorityFee')}
|
2021-07-01 16:37:00 +02:00
|
|
|
titleUnit="(GWEI)"
|
2021-07-15 19:23:37 +02:00
|
|
|
tooltipText={t('editGasMaxPriorityFeeTooltip')}
|
2021-07-12 18:16:03 +02:00
|
|
|
onChange={(value) => {
|
|
|
|
onManualChange?.();
|
2021-07-22 18:32:59 +02:00
|
|
|
setMaxPriorityFee(value);
|
2021-07-12 18:16:03 +02:00
|
|
|
}}
|
2021-06-28 16:45:08 +02:00
|
|
|
value={maxPriorityFee}
|
2021-07-12 18:16:03 +02:00
|
|
|
detailText={maxPriorityFeeFiat}
|
2021-07-07 00:19:11 +02:00
|
|
|
numeric
|
2021-07-20 21:34:32 +02:00
|
|
|
error={
|
|
|
|
gasErrors?.maxPriorityFee
|
|
|
|
? getGasFormErrorText(gasErrors.maxPriorityFee, t)
|
|
|
|
: null
|
|
|
|
}
|
2021-06-28 16:45:08 +02:00
|
|
|
/>
|
2021-07-07 00:19:11 +02:00
|
|
|
<FormField
|
2021-06-28 16:45:08 +02:00
|
|
|
titleText={t('maxFee')}
|
2021-07-01 16:37:00 +02:00
|
|
|
titleUnit="(GWEI)"
|
2021-07-15 19:23:37 +02:00
|
|
|
tooltipText={t('editGasMaxFeeTooltip')}
|
2021-07-12 18:16:03 +02:00
|
|
|
onChange={(value) => {
|
|
|
|
onManualChange?.();
|
2021-07-22 18:32:59 +02:00
|
|
|
setMaxFee(value);
|
2021-07-12 18:16:03 +02:00
|
|
|
}}
|
2021-06-28 16:45:08 +02:00
|
|
|
value={maxFee}
|
2021-07-07 00:19:11 +02:00
|
|
|
numeric
|
2021-07-12 18:16:03 +02:00
|
|
|
detailText={maxFeeFiat}
|
2021-07-20 21:34:32 +02:00
|
|
|
error={
|
|
|
|
gasErrors?.maxFee
|
|
|
|
? getGasFormErrorText(gasErrors.maxFee, t)
|
|
|
|
: null
|
|
|
|
}
|
2021-06-28 16:45:08 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2021-07-07 00:19:11 +02:00
|
|
|
<FormField
|
2021-07-15 19:23:54 +02:00
|
|
|
titleText={t('advancedGasPriceTitle')}
|
2021-07-01 16:37:00 +02:00
|
|
|
titleUnit="(GWEI)"
|
2021-07-12 18:16:03 +02:00
|
|
|
onChange={(value) => {
|
|
|
|
onManualChange?.();
|
2021-07-22 18:32:59 +02:00
|
|
|
setGasPrice(value);
|
2021-07-12 18:16:03 +02:00
|
|
|
}}
|
2021-07-02 21:07:34 +02:00
|
|
|
tooltipText={t('editGasPriceTooltip')}
|
2021-06-28 16:45:08 +02:00
|
|
|
value={gasPrice}
|
2021-07-07 00:19:11 +02:00
|
|
|
numeric
|
2021-07-12 18:16:03 +02:00
|
|
|
titleDetail={
|
|
|
|
suggestedValues.gasPrice && (
|
|
|
|
<>
|
|
|
|
<Typography
|
|
|
|
tag="span"
|
|
|
|
color={COLORS.UI4}
|
|
|
|
variant={TYPOGRAPHY.H8}
|
|
|
|
fontWeight={FONT_WEIGHT.BOLD}
|
|
|
|
>
|
|
|
|
{t('gasFeeEstimate')}:
|
|
|
|
</Typography>{' '}
|
|
|
|
<Typography
|
|
|
|
tag="span"
|
|
|
|
color={COLORS.UI4}
|
|
|
|
variant={TYPOGRAPHY.H8}
|
|
|
|
>
|
|
|
|
{suggestedValues.gasPrice}
|
|
|
|
</Typography>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
2021-06-28 16:45:08 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2021-06-24 01:39:44 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-07-12 18:16:03 +02:00
|
|
|
|
|
|
|
AdvancedGasControls.propTypes = {
|
|
|
|
estimateToUse: PropTypes.oneOf(Object.values(GAS_RECOMMENDATIONS)),
|
|
|
|
gasFeeEstimates: PropTypes.oneOf([
|
|
|
|
PropTypes.shape({
|
|
|
|
gasPrice: PropTypes.string,
|
|
|
|
}),
|
|
|
|
PropTypes.shape({
|
|
|
|
low: PropTypes.string,
|
|
|
|
medium: PropTypes.string,
|
|
|
|
high: PropTypes.string,
|
|
|
|
}),
|
|
|
|
PropTypes.shape({
|
|
|
|
low: PropTypes.object,
|
|
|
|
medium: PropTypes.object,
|
|
|
|
high: PropTypes.object,
|
|
|
|
estimatedBaseFee: PropTypes.string,
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
gasEstimateType: PropTypes.oneOf(Object.values(GAS_ESTIMATE_TYPES)),
|
|
|
|
setMaxPriorityFee: PropTypes.func,
|
|
|
|
setMaxFee: PropTypes.func,
|
|
|
|
maxPriorityFee: PropTypes.number,
|
|
|
|
maxFee: PropTypes.number,
|
|
|
|
onManualChange: PropTypes.func,
|
|
|
|
gasLimit: PropTypes.number,
|
|
|
|
setGasLimit: PropTypes.func,
|
|
|
|
gasPrice: PropTypes.number,
|
|
|
|
setGasPrice: PropTypes.func,
|
|
|
|
maxPriorityFeeFiat: PropTypes.string,
|
|
|
|
maxFeeFiat: PropTypes.string,
|
2021-07-20 21:34:32 +02:00
|
|
|
gasErrors: PropTypes.object,
|
2021-07-30 13:35:30 +02:00
|
|
|
minimumGasLimit: PropTypes.number,
|
2021-07-12 18:16:03 +02:00
|
|
|
};
|