2021-12-01 01:31:21 +01:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
import { HIGH_FEE_WARNING_MULTIPLIER } from '../../../../../pages/send/send.constants';
|
2022-02-14 20:07:46 +01:00
|
|
|
import {
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
PRIORITY_LEVELS,
|
|
|
|
} from '../../../../../../shared/constants/gas';
|
2022-03-07 18:52:14 +01:00
|
|
|
import { PRIMARY } from '../../../../../helpers/constants/common';
|
2022-03-11 19:59:58 +01:00
|
|
|
import { bnGreaterThan, bnLessThan } from '../../../../../helpers/utils/util';
|
2021-12-01 01:31:21 +01:00
|
|
|
import { decGWEIToHexWEI } from '../../../../../helpers/utils/conversions.util';
|
|
|
|
import { getAdvancedGasFeeValues } from '../../../../../selectors';
|
|
|
|
import { useGasFeeContext } from '../../../../../contexts/gasFee';
|
|
|
|
import { useI18nContext } from '../../../../../hooks/useI18nContext';
|
|
|
|
import { useUserPreferencedCurrency } from '../../../../../hooks/useUserPreferencedCurrency';
|
|
|
|
import { useCurrencyDisplay } from '../../../../../hooks/useCurrencyDisplay';
|
|
|
|
import Box from '../../../../ui/box';
|
|
|
|
import FormField from '../../../../ui/form-field';
|
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
import { useAdvancedGasFeePopoverContext } from '../../context';
|
2021-12-01 01:31:21 +01:00
|
|
|
import AdvancedGasFeeInputSubtext from '../../advanced-gas-fee-input-subtext';
|
|
|
|
|
2022-01-10 20:34:54 +01:00
|
|
|
const validateBaseFee = (value, gasFeeEstimates, maxPriorityFeePerGas) => {
|
2021-12-06 19:47:26 +01:00
|
|
|
if (bnGreaterThan(maxPriorityFeePerGas, value)) {
|
2022-01-10 20:34:54 +01:00
|
|
|
return 'editGasMaxBaseFeeGWEIImbalance';
|
2021-12-06 19:47:26 +01:00
|
|
|
}
|
|
|
|
if (
|
|
|
|
gasFeeEstimates?.low &&
|
|
|
|
bnLessThan(value, gasFeeEstimates.low.suggestedMaxFeePerGas)
|
|
|
|
) {
|
|
|
|
return 'editGasMaxBaseFeeLow';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
gasFeeEstimates?.high &&
|
|
|
|
bnGreaterThan(
|
|
|
|
value,
|
|
|
|
gasFeeEstimates.high.suggestedMaxFeePerGas * HIGH_FEE_WARNING_MULTIPLIER,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return 'editGasMaxBaseFeeHigh';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2021-12-01 01:31:21 +01:00
|
|
|
const BaseFeeInput = () => {
|
|
|
|
const t = useI18nContext();
|
2021-12-21 20:45:28 +01:00
|
|
|
|
2022-02-14 20:07:46 +01:00
|
|
|
const {
|
|
|
|
gasFeeEstimates,
|
|
|
|
estimateUsed,
|
|
|
|
maxFeePerGas,
|
|
|
|
editGasMode,
|
|
|
|
} = useGasFeeContext();
|
2021-12-06 19:47:26 +01:00
|
|
|
const {
|
2022-03-07 18:52:14 +01:00
|
|
|
gasLimit,
|
2021-12-06 19:47:26 +01:00
|
|
|
maxPriorityFeePerGas,
|
2021-12-08 23:56:10 +01:00
|
|
|
setErrorValue,
|
|
|
|
setMaxFeePerGas,
|
2022-01-10 20:34:54 +01:00
|
|
|
setMaxBaseFee,
|
2021-12-06 19:47:26 +01:00
|
|
|
} = useAdvancedGasFeePopoverContext();
|
|
|
|
|
2021-12-21 20:01:43 +01:00
|
|
|
const {
|
|
|
|
estimatedBaseFee,
|
|
|
|
historicalBaseFeeRange,
|
|
|
|
baseFeeTrend,
|
|
|
|
} = gasFeeEstimates;
|
2021-12-06 19:47:26 +01:00
|
|
|
const [baseFeeError, setBaseFeeError] = useState();
|
2022-03-07 18:52:14 +01:00
|
|
|
const { currency, numberOfDecimals } = useUserPreferencedCurrency(PRIMARY);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
|
|
|
const advancedGasFeeValues = useSelector(getAdvancedGasFeeValues);
|
|
|
|
|
2022-01-10 20:34:54 +01:00
|
|
|
const [baseFee, setBaseFee] = useState(() => {
|
2021-12-01 01:31:21 +01:00
|
|
|
if (
|
|
|
|
estimateUsed !== PRIORITY_LEVELS.CUSTOM &&
|
2022-02-14 20:07:46 +01:00
|
|
|
advancedGasFeeValues?.maxBaseFee &&
|
|
|
|
editGasMode !== EDIT_GAS_MODES.SWAPS
|
2021-12-01 01:31:21 +01:00
|
|
|
) {
|
|
|
|
return advancedGasFeeValues.maxBaseFee;
|
|
|
|
}
|
2022-01-10 20:34:54 +01:00
|
|
|
|
|
|
|
return maxFeePerGas;
|
2021-12-01 01:31:21 +01:00
|
|
|
});
|
|
|
|
|
2022-03-07 18:52:14 +01:00
|
|
|
const [baseFeeInPrimaryCurrency] = useCurrencyDisplay(
|
|
|
|
decGWEIToHexWEI(baseFee * gasLimit),
|
2022-01-22 01:23:02 +01:00
|
|
|
{ currency, numberOfDecimals },
|
2021-12-01 01:31:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
const updateBaseFee = useCallback(
|
|
|
|
(value) => {
|
2022-01-10 20:34:54 +01:00
|
|
|
setBaseFee(value);
|
2021-12-01 01:31:21 +01:00
|
|
|
},
|
2022-01-10 20:34:54 +01:00
|
|
|
[setBaseFee],
|
2021-12-01 01:31:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2022-01-10 20:34:54 +01:00
|
|
|
setMaxFeePerGas(baseFee);
|
2021-12-06 19:47:26 +01:00
|
|
|
const error = validateBaseFee(
|
2022-01-10 20:34:54 +01:00
|
|
|
baseFee,
|
2021-12-06 19:47:26 +01:00
|
|
|
gasFeeEstimates,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
);
|
2021-12-08 23:56:10 +01:00
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
setBaseFeeError(error);
|
2022-01-10 20:34:54 +01:00
|
|
|
setErrorValue('maxFeePerGas', error === 'editGasMaxBaseFeeGWEIImbalance');
|
|
|
|
setMaxBaseFee(baseFee);
|
2021-12-06 19:47:26 +01:00
|
|
|
}, [
|
2022-01-10 20:34:54 +01:00
|
|
|
baseFee,
|
2021-12-06 19:47:26 +01:00
|
|
|
gasFeeEstimates,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
setBaseFeeError,
|
2021-12-08 23:56:10 +01:00
|
|
|
setErrorValue,
|
2021-12-06 19:47:26 +01:00
|
|
|
setMaxFeePerGas,
|
2022-01-10 20:34:54 +01:00
|
|
|
setMaxBaseFee,
|
2021-12-06 19:47:26 +01:00
|
|
|
]);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
|
|
|
return (
|
2021-12-21 20:45:28 +01:00
|
|
|
<Box className="base-fee-input" margin={[0, 2]}>
|
2021-12-01 01:31:21 +01:00
|
|
|
<FormField
|
2022-01-19 00:08:41 +01:00
|
|
|
dataTestId="base-fee-input"
|
2021-12-06 19:47:26 +01:00
|
|
|
error={baseFeeError ? t(baseFeeError) : ''}
|
2021-12-01 01:31:21 +01:00
|
|
|
onChange={updateBaseFee}
|
|
|
|
titleText={t('maxBaseFee')}
|
2022-01-10 20:34:54 +01:00
|
|
|
titleUnit={`(${t('gwei')})`}
|
2021-12-01 01:31:21 +01:00
|
|
|
tooltipText={t('advancedBaseGasFeeToolTip')}
|
2022-01-10 20:34:54 +01:00
|
|
|
value={baseFee}
|
2022-03-07 18:52:14 +01:00
|
|
|
detailText={`≈ ${baseFeeInPrimaryCurrency}`}
|
2021-12-01 01:31:21 +01:00
|
|
|
numeric
|
|
|
|
/>
|
|
|
|
<AdvancedGasFeeInputSubtext
|
2022-03-11 19:59:58 +01:00
|
|
|
latest={estimatedBaseFee}
|
|
|
|
historical={historicalBaseFeeRange}
|
|
|
|
trend={baseFeeTrend}
|
2021-12-01 01:31:21 +01:00
|
|
|
/>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default BaseFeeInput;
|