1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-26 20:39:08 +01:00
metamask-extension/ui/hooks/useGasFeeEstimates.js
Elliot Winkler 7b963cabd7
Alert users when the network is busy (#12268)
When a lot of transactions are occurring on the network, such as during
an NFT drop, it drives gas fees up. When this happens, we want to not
only inform the user about this, but also dissuade them from using a
higher gas fee (as we have proved in testing that high gas fees can
cause bidding wars and exacerbate the situation).

The method for determining whether the network is "busy" is already
handled by GasFeeController, which exposes a `networkCongestion`
property within the gas fee estimate data. If this number exceeds 0.66 —
meaning that the current base fee is above the 66th percentile among the
base fees over the last several days — then we determine that the
network is "busy".
2022-01-07 12:18:02 -07:00

53 lines
1.8 KiB
JavaScript

import isEqual from 'lodash/isEqual';
import { shallowEqual, useSelector } from 'react-redux';
import {
getEstimatedGasFeeTimeBounds,
getGasEstimateType,
getGasFeeEstimates,
getIsGasEstimatesLoading,
getIsNetworkBusy,
} from '../ducks/metamask/metamask';
import { useSafeGasEstimatePolling } from './useSafeGasEstimatePolling';
/**
* @typedef {object} GasEstimates
* @property {GasEstimateTypes} gasEstimateType - The type of estimate provided
* @property {import(
* '@metamask/controllers'
* ).GasFeeState['gasFeeEstimates']} gasFeeEstimates - The estimate object
* @property {import(
* '@metamask/controllers'
* ).GasFeeState['estimatedGasFeeTimeBounds']} [estimatedGasFeeTimeBounds] -
* estimated time boundaries for fee-market type estimates
* @property {boolean} isGasEstimateLoading - indicates whether the gas
* estimates are currently loading.
*/
/**
* Gets the current gasFeeEstimates from state and begins polling for new
* estimates. When this hook is removed from the tree it will signal to the
* GasFeeController that it is done requiring new gas estimates. Also checks
* the returned gas estimate for validity on the current network.
*
* @returns {GasFeeEstimates} GasFeeEstimates object
*/
export function useGasFeeEstimates() {
const gasEstimateType = useSelector(getGasEstimateType);
const gasFeeEstimates = useSelector(getGasFeeEstimates, isEqual);
const estimatedGasFeeTimeBounds = useSelector(
getEstimatedGasFeeTimeBounds,
shallowEqual,
);
const isGasEstimatesLoading = useSelector(getIsGasEstimatesLoading);
const isNetworkBusy = useSelector(getIsNetworkBusy);
useSafeGasEstimatePolling();
return {
gasFeeEstimates,
gasEstimateType,
estimatedGasFeeTimeBounds,
isGasEstimatesLoading,
isNetworkBusy,
};
}