1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-27 12:56:01 +01:00
metamask-extension/ui/components/app/advanced-gas-controls/advanced-gas-controls.component.js
Elliot Winkler 7b963cabd7
Alert users when the network is busy (#12268)
When a lot of transactions are occurring on the network, such as during
an NFT drop, it drives gas fees up. When this happens, we want to not
only inform the user about this, but also dissuade them from using a
higher gas fee (as we have proved in testing that high gas fees can
cause bidding wars and exacerbate the situation).

The method for determining whether the network is "busy" is already
handled by GasFeeController, which exposes a `networkCongestion`
property within the gas fee estimate data. If this number exceeds 0.66 —
meaning that the current base fee is above the 66th percentile among the
base fees over the last several days — then we determine that the
network is "busy".
2022-01-07 12:18:02 -07:00

139 lines
3.9 KiB
JavaScript

import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { I18nContext } from '../../../contexts/i18n';
import FormField from '../../ui/form-field';
import { GAS_ESTIMATE_TYPES } from '../../../../shared/constants/gas';
import { getGasFormErrorText } from '../../../helpers/constants/gas';
import { getIsGasEstimatesLoading } from '../../../ducks/metamask/metamask';
import { getNetworkSupportsSettingGasPrice } from '../../../selectors';
export default function AdvancedGasControls({
gasEstimateType,
maxPriorityFee,
maxFee,
setMaxPriorityFee,
setMaxFee,
onManualChange,
gasLimit,
setGasLimit,
gasPrice,
setGasPrice,
maxPriorityFeeFiat,
maxFeeFiat,
gasErrors,
minimumGasLimit,
supportsEIP1559,
}) {
const t = useContext(I18nContext);
const isGasEstimatesLoading = useSelector(getIsGasEstimatesLoading);
const showFeeMarketFields =
supportsEIP1559 &&
(gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET ||
gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE ||
isGasEstimatesLoading);
const networkSupportsSettingGasPrice = useSelector(
getNetworkSupportsSettingGasPrice,
);
return (
<div className="advanced-gas-controls">
<FormField
titleText={t('gasLimit')}
error={
gasErrors?.gasLimit
? getGasFormErrorText(gasErrors.gasLimit, t, { minimumGasLimit })
: null
}
onChange={(value) => {
onManualChange?.();
setGasLimit(value);
}}
tooltipText={t('editGasLimitTooltip')}
value={gasLimit}
allowDecimals={false}
numeric
/>
{showFeeMarketFields ? (
<>
<FormField
titleText={t('maxPriorityFee')}
titleUnit="(GWEI)"
tooltipText={t('editGasMaxPriorityFeeTooltip')}
onChange={(value) => {
onManualChange?.();
setMaxPriorityFee(value);
}}
value={maxPriorityFee}
detailText={maxPriorityFeeFiat}
numeric
error={
gasErrors?.maxPriorityFee
? getGasFormErrorText(gasErrors.maxPriorityFee, t)
: null
}
/>
<FormField
titleText={t('maxFee')}
titleUnit="(GWEI)"
tooltipText={t('editGasMaxFeeTooltip')}
onChange={(value) => {
onManualChange?.();
setMaxFee(value);
}}
value={maxFee}
numeric
detailText={maxFeeFiat}
error={
gasErrors?.maxFee
? getGasFormErrorText(gasErrors.maxFee, t)
: null
}
/>
</>
) : (
<>
<FormField
titleText={t('advancedGasPriceTitle')}
titleUnit="(GWEI)"
onChange={(value) => {
onManualChange?.();
setGasPrice(value);
}}
tooltipText={t('editGasPriceTooltip')}
value={gasPrice}
numeric
error={
gasErrors?.gasPrice
? getGasFormErrorText(gasErrors.gasPrice, t)
: null
}
disabled={!networkSupportsSettingGasPrice}
/>
</>
)}
</div>
);
}
AdvancedGasControls.propTypes = {
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,
gasErrors: PropTypes.object,
minimumGasLimit: PropTypes.string,
supportsEIP1559: PropTypes.bool,
};