2021-12-01 01:31:21 +01:00
|
|
|
import React, { 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';
|
2021-12-01 01:31:21 +01:00
|
|
|
import { decGWEIToHexWEI } from '../../../../../helpers/utils/conversions.util';
|
|
|
|
import { getAdvancedGasFeeValues } from '../../../../../selectors';
|
|
|
|
import { useCurrencyDisplay } from '../../../../../hooks/useCurrencyDisplay';
|
|
|
|
import { useGasFeeContext } from '../../../../../contexts/gasFee';
|
|
|
|
import { useI18nContext } from '../../../../../hooks/useI18nContext';
|
|
|
|
import { useUserPreferencedCurrency } from '../../../../../hooks/useUserPreferencedCurrency';
|
|
|
|
import FormField from '../../../../ui/form-field';
|
2021-12-21 20:45:28 +01:00
|
|
|
import Box from '../../../../ui/box';
|
2021-12-06 19:47:26 +01:00
|
|
|
import { bnGreaterThan, bnLessThan } from '../../../../../helpers/utils/util';
|
2021-12-01 01:31:21 +01:00
|
|
|
|
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';
|
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
const validatePriorityFee = (value, gasFeeEstimates) => {
|
2021-12-08 23:56:10 +01:00
|
|
|
if (value <= 0) {
|
2021-12-06 19:47:26 +01:00
|
|
|
return 'editGasMaxPriorityFeeBelowMinimumV2';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
gasFeeEstimates?.low &&
|
|
|
|
bnLessThan(value, gasFeeEstimates.low.suggestedMaxPriorityFeePerGas)
|
|
|
|
) {
|
|
|
|
return 'editGasMaxPriorityFeeLowV2';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
gasFeeEstimates?.high &&
|
|
|
|
bnGreaterThan(
|
|
|
|
value,
|
|
|
|
gasFeeEstimates.high.suggestedMaxPriorityFeePerGas *
|
|
|
|
HIGH_FEE_WARNING_MULTIPLIER,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return 'editGasMaxPriorityFeeHighV2';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2021-12-01 01:31:21 +01:00
|
|
|
const PriorityFeeInput = () => {
|
|
|
|
const t = useI18nContext();
|
|
|
|
const advancedGasFeeValues = useSelector(getAdvancedGasFeeValues);
|
2022-07-31 20:26:40 +02:00
|
|
|
const { gasLimit, setErrorValue, setMaxPriorityFeePerGas } =
|
|
|
|
useAdvancedGasFeePopoverContext();
|
|
|
|
const { editGasMode, estimateUsed, gasFeeEstimates, maxPriorityFeePerGas } =
|
|
|
|
useGasFeeContext();
|
2021-12-17 19:26:20 +01:00
|
|
|
const {
|
|
|
|
latestPriorityFeeRange,
|
|
|
|
historicalPriorityFeeRange,
|
2021-12-21 20:01:43 +01:00
|
|
|
priorityFeeTrend,
|
2021-12-17 19:26:20 +01:00
|
|
|
} = gasFeeEstimates;
|
2021-12-06 19:47:26 +01:00
|
|
|
const [priorityFeeError, setPriorityFeeError] = useState();
|
2021-12-01 01:31:21 +01:00
|
|
|
|
|
|
|
const [priorityFee, setPriorityFee] = useState(() => {
|
|
|
|
if (
|
|
|
|
estimateUsed !== PRIORITY_LEVELS.CUSTOM &&
|
2022-02-14 20:07:46 +01:00
|
|
|
advancedGasFeeValues?.priorityFee &&
|
|
|
|
editGasMode !== EDIT_GAS_MODES.SWAPS
|
2022-01-06 23:56:51 +01:00
|
|
|
) {
|
2021-12-01 01:31:21 +01:00
|
|
|
return advancedGasFeeValues.priorityFee;
|
2022-01-06 23:56:51 +01:00
|
|
|
}
|
2021-12-01 01:31:21 +01:00
|
|
|
return maxPriorityFeePerGas;
|
|
|
|
});
|
|
|
|
|
2022-03-07 18:52:14 +01:00
|
|
|
const { currency, numberOfDecimals } = useUserPreferencedCurrency(PRIMARY);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
2022-03-07 18:52:14 +01:00
|
|
|
const [priorityFeeInPrimaryCurrency] = useCurrencyDisplay(
|
|
|
|
decGWEIToHexWEI(priorityFee * gasLimit),
|
2021-12-01 01:31:21 +01:00
|
|
|
{ currency, numberOfDecimals },
|
|
|
|
);
|
|
|
|
|
|
|
|
const updatePriorityFee = (value) => {
|
|
|
|
setPriorityFee(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setMaxPriorityFeePerGas(priorityFee);
|
2021-12-06 19:47:26 +01:00
|
|
|
const error = validatePriorityFee(priorityFee, gasFeeEstimates);
|
2021-12-08 23:56:10 +01:00
|
|
|
setErrorValue(
|
|
|
|
'maxPriorityFeePerGas',
|
|
|
|
error === 'editGasMaxPriorityFeeBelowMinimumV2',
|
|
|
|
);
|
2021-12-06 19:47:26 +01:00
|
|
|
setPriorityFeeError(error);
|
|
|
|
}, [
|
|
|
|
gasFeeEstimates,
|
|
|
|
priorityFee,
|
2021-12-08 23:56:10 +01:00
|
|
|
setErrorValue,
|
2021-12-06 19:47:26 +01:00
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
setPriorityFeeError,
|
|
|
|
]);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
|
|
|
return (
|
2022-07-20 22:47:51 +02:00
|
|
|
<Box
|
|
|
|
marginTop={4}
|
|
|
|
marginLeft={2}
|
|
|
|
marginRight={2}
|
|
|
|
className="priority-fee-input"
|
|
|
|
>
|
2021-12-01 01:31:21 +01:00
|
|
|
<FormField
|
2022-01-19 00:08:41 +01:00
|
|
|
dataTestId="priority-fee-input"
|
2021-12-06 19:47:26 +01:00
|
|
|
error={priorityFeeError ? t(priorityFeeError) : ''}
|
2021-12-01 01:31:21 +01:00
|
|
|
onChange={updatePriorityFee}
|
2021-12-06 17:02:23 +01:00
|
|
|
titleText={t('priorityFeeProperCase')}
|
2022-01-10 20:34:54 +01:00
|
|
|
titleUnit={`(${t('gwei')})`}
|
2021-12-01 01:31:21 +01:00
|
|
|
tooltipText={t('advancedPriorityFeeToolTip')}
|
|
|
|
value={priorityFee}
|
2022-03-07 18:52:14 +01:00
|
|
|
detailText={`≈ ${priorityFeeInPrimaryCurrency}`}
|
2021-12-01 01:31:21 +01:00
|
|
|
numeric
|
|
|
|
/>
|
2021-12-17 19:26:20 +01:00
|
|
|
<AdvancedGasFeeInputSubtext
|
2022-03-11 19:59:58 +01:00
|
|
|
latest={latestPriorityFeeRange}
|
|
|
|
historical={historicalPriorityFeeRange}
|
|
|
|
trend={priorityFeeTrend}
|
2021-12-17 19:26:20 +01:00
|
|
|
/>
|
2021-12-21 20:45:28 +01:00
|
|
|
</Box>
|
2021-12-01 01:31:21 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PriorityFeeInput;
|