1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-30 16:18:07 +01:00
metamask-extension/ui/components/app/edit-gas-fee-popover/edit-gas-item/useCustomTimeEstimate.js

85 lines
2.3 KiB
JavaScript
Raw Normal View History

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 };
};