mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-27 12:56:01 +01:00
af971cd5b6
The ESLint config for the extension explicitly includes support for Prettier. However, this is already being provided by our global ESLint config (`@metamask/eslint-config`). Therefore there is no need to include it here. In fact, this is causing weird issues where the `curly` option is getting overridden somehow. After this change, these syntaxes are invalid: ``` javascript if (foo) return; ``` ``` javascript if (foo) return 'bar'; ```
85 lines
2.3 KiB
JavaScript
85 lines
2.3 KiB
JavaScript
import { useEffect, useState } from 'react';
|
|
import { useSelector } from 'react-redux';
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { GAS_ESTIMATE_TYPES } from '../../../../../shared/constants/gas';
|
|
import {
|
|
getGasEstimateType,
|
|
getIsGasEstimatesLoading,
|
|
} from '../../../../ducks/metamask/metamask';
|
|
import { getGasFeeTimeEstimate } from '../../../../store/actions';
|
|
|
|
export const useCustomTimeEstimate = ({
|
|
gasFeeEstimates,
|
|
maxFeePerGas,
|
|
maxPriorityFeePerGas,
|
|
}) => {
|
|
const gasEstimateType = useSelector(getGasEstimateType);
|
|
const isGasEstimatesLoading = useSelector(getIsGasEstimatesLoading);
|
|
|
|
const [customEstimatedTime, setCustomEstimatedTime] = useState(null);
|
|
|
|
const returnNoEstimates =
|
|
isGasEstimatesLoading ||
|
|
gasEstimateType !== GAS_ESTIMATE_TYPES.FEE_MARKET ||
|
|
!maxPriorityFeePerGas;
|
|
|
|
// If the user has chosen a value lower than the low gas fee estimate,
|
|
// We'll need to use the useEffect hook below to make a call to calculate
|
|
// the time to show
|
|
const isUnknownLow =
|
|
gasFeeEstimates?.low &&
|
|
Number(maxPriorityFeePerGas) <
|
|
Number(gasFeeEstimates.low.suggestedMaxPriorityFeePerGas);
|
|
|
|
useEffect(() => {
|
|
if (
|
|
isGasEstimatesLoading ||
|
|
gasEstimateType !== GAS_ESTIMATE_TYPES.FEE_MARKET ||
|
|
!maxPriorityFeePerGas
|
|
) {
|
|
return;
|
|
}
|
|
if (isUnknownLow) {
|
|
// getGasFeeTimeEstimate requires parameters in string format
|
|
getGasFeeTimeEstimate(
|
|
new BigNumber(maxPriorityFeePerGas, 10).toString(10),
|
|
new BigNumber(maxFeePerGas, 10).toString(10),
|
|
).then((result) => {
|
|
setCustomEstimatedTime(result);
|
|
});
|
|
}
|
|
}, [
|
|
gasEstimateType,
|
|
isUnknownLow,
|
|
isGasEstimatesLoading,
|
|
maxFeePerGas,
|
|
maxPriorityFeePerGas,
|
|
returnNoEstimates,
|
|
]);
|
|
|
|
if (returnNoEstimates) {
|
|
return {};
|
|
}
|
|
|
|
const { low = {}, medium = {}, high = {} } = gasFeeEstimates;
|
|
let waitTimeEstimate = '';
|
|
|
|
if (
|
|
isUnknownLow &&
|
|
customEstimatedTime &&
|
|
customEstimatedTime !== 'unknown' &&
|
|
customEstimatedTime?.upperTimeBound !== 'unknown'
|
|
) {
|
|
waitTimeEstimate = Number(customEstimatedTime?.upperTimeBound);
|
|
} else if (
|
|
Number(maxPriorityFeePerGas) >= Number(medium.suggestedMaxPriorityFeePerGas)
|
|
) {
|
|
waitTimeEstimate = high.minWaitTimeEstimate;
|
|
} else {
|
|
waitTimeEstimate = low.maxWaitTimeEstimate;
|
|
}
|
|
|
|
return { waitTimeEstimate };
|
|
};
|