1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/ui/components/app/advanced-gas-controls/advanced-gas-controls.component.js
David Walsh d34bf92fd0
Fix 11705 - Reset gas limit when radio button clicked (#11710)
* Fix 11705 - Reset gas limit when radio button clicked

* Trigger manual change when gasLimit is changed

* Coordinate gas limit with radio buttons

* Revert "Coordinate gas limit with radio buttons"

This reverts commit 910327a408e32ae989c5565a107db24ac24f2a98.

* Cleanup default gas limit

* setEstimateToUse only update gasLimit on error, add default minimum gasLimit

* add minimum gasLimit fallback

Co-authored-by: Alex <adonesky@gmail.com>
2021-08-02 17:58:30 -02:30

199 lines
5.5 KiB
JavaScript

import React, { useContext } from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { isEIP1559Network } from '../../../ducks/metamask/metamask';
import { I18nContext } from '../../../contexts/i18n';
import Typography from '../../ui/typography/typography';
import {
FONT_WEIGHT,
TYPOGRAPHY,
COLORS,
} from '../../../helpers/constants/design-system';
import FormField from '../../ui/form-field';
import {
GAS_ESTIMATE_TYPES,
GAS_RECOMMENDATIONS,
} from '../../../../shared/constants/gas';
import { getGasFormErrorText } from '../../../helpers/constants/gas';
export default function AdvancedGasControls({
estimateToUse,
gasFeeEstimates,
gasEstimateType,
maxPriorityFee,
maxFee,
setMaxPriorityFee,
setMaxFee,
onManualChange,
gasLimit,
setGasLimit,
gasPrice,
setGasPrice,
maxPriorityFeeFiat,
maxFeeFiat,
gasErrors,
networkSupportsEIP1559,
minimumGasLimit,
}) {
const t = useContext(I18nContext);
const networkSupports1559 = useSelector(isEIP1559Network);
const suggestedValues = {};
if (networkSupportsEIP1559) {
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;
}
}
const showFeeMarketFields =
networkSupports1559 &&
(gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET ||
gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE);
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
autoFocus
/>
{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
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>
</>
)
}
/>
</>
)}
</div>
);
}
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,
gasErrors: PropTypes.object,
minimumGasLimit: PropTypes.number,
networkSupportsEIP1559: PropTypes.object,
};