1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-04 23:14:56 +01:00
metamask-extension/ui/components/app/advanced-gas-fee-popover/advanced-gas-fee-inputs/priority-fee-input/priority-fee-input.js
Elliot Winkler f8f4397339
Degrade gracefully when gas API is down (#13865)
When the gas API is down, the logic we use will no longer compute all of
the data that the gas API returns in order to reduce the burden on
Infura. Specifically, only estimated fees for different priority levels,
as well as the latest base fee, will be available; all other data
points, such as the latest and historical priority fee range and network
stability, will be missing. This commit updates the frontend logic to
account for this lack of data by merely hiding the relevant pieces of
the UI that would otherwise be shown.
2022-03-11 11:59:58 -07:00

128 lines
3.8 KiB
JavaScript

import React, { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { HIGH_FEE_WARNING_MULTIPLIER } from '../../../../../pages/send/send.constants';
import {
EDIT_GAS_MODES,
PRIORITY_LEVELS,
} from '../../../../../../shared/constants/gas';
import { PRIMARY } from '../../../../../helpers/constants/common';
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';
import Box from '../../../../ui/box';
import { bnGreaterThan, bnLessThan } from '../../../../../helpers/utils/util';
import { useAdvancedGasFeePopoverContext } from '../../context';
import AdvancedGasFeeInputSubtext from '../../advanced-gas-fee-input-subtext';
const validatePriorityFee = (value, gasFeeEstimates) => {
if (value <= 0) {
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;
};
const PriorityFeeInput = () => {
const t = useI18nContext();
const advancedGasFeeValues = useSelector(getAdvancedGasFeeValues);
const {
gasLimit,
setErrorValue,
setMaxPriorityFeePerGas,
} = useAdvancedGasFeePopoverContext();
const {
editGasMode,
estimateUsed,
gasFeeEstimates,
maxPriorityFeePerGas,
} = useGasFeeContext();
const {
latestPriorityFeeRange,
historicalPriorityFeeRange,
priorityFeeTrend,
} = gasFeeEstimates;
const [priorityFeeError, setPriorityFeeError] = useState();
const [priorityFee, setPriorityFee] = useState(() => {
if (
estimateUsed !== PRIORITY_LEVELS.CUSTOM &&
advancedGasFeeValues?.priorityFee &&
editGasMode !== EDIT_GAS_MODES.SWAPS
) {
return advancedGasFeeValues.priorityFee;
}
return maxPriorityFeePerGas;
});
const { currency, numberOfDecimals } = useUserPreferencedCurrency(PRIMARY);
const [priorityFeeInPrimaryCurrency] = useCurrencyDisplay(
decGWEIToHexWEI(priorityFee * gasLimit),
{ currency, numberOfDecimals },
);
const updatePriorityFee = (value) => {
setPriorityFee(value);
};
useEffect(() => {
setMaxPriorityFeePerGas(priorityFee);
const error = validatePriorityFee(priorityFee, gasFeeEstimates);
setErrorValue(
'maxPriorityFeePerGas',
error === 'editGasMaxPriorityFeeBelowMinimumV2',
);
setPriorityFeeError(error);
}, [
gasFeeEstimates,
priorityFee,
setErrorValue,
setMaxPriorityFeePerGas,
setPriorityFeeError,
]);
return (
<Box margin={[4, 2, 0, 2]} className="priority-fee-input">
<FormField
dataTestId="priority-fee-input"
error={priorityFeeError ? t(priorityFeeError) : ''}
onChange={updatePriorityFee}
titleText={t('priorityFeeProperCase')}
titleUnit={`(${t('gwei')})`}
tooltipText={t('advancedPriorityFeeToolTip')}
value={priorityFee}
detailText={`${priorityFeeInPrimaryCurrency}`}
numeric
/>
<AdvancedGasFeeInputSubtext
latest={latestPriorityFeeRange}
historical={historicalPriorityFeeRange}
trend={priorityFeeTrend}
/>
</Box>
);
};
export default PriorityFeeInput;