1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-02 14:15:06 +01:00
metamask-extension/ui/components/app/edit-gas-fee-popover/network-statistics/status-slider/status-slider.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

92 lines
2.4 KiB
JavaScript

import React from 'react';
import { NETWORK_CONGESTION_THRESHOLDS } from '../../../../../../shared/constants/gas';
import { useGasFeeContext } from '../../../../../contexts/gasFee';
import I18nValue from '../../../../ui/i18n-value';
import { NetworkStabilityTooltip } from '../tooltips';
const GRADIENT_COLORS = [
'#037DD6',
'#1876C8',
'#2D70BA',
'#4369AB',
'#57629E',
'#6A5D92',
'#805683',
'#9A4D71',
'#B44561',
'#C54055',
'#D73A49',
];
const determineStatusInfo = (givenNetworkCongestion) => {
const networkCongestion = givenNetworkCongestion ?? 0.5;
const colorIndex = Math.round(networkCongestion * 10);
const color = GRADIENT_COLORS[colorIndex];
const sliderTickValue = colorIndex * 10;
if (networkCongestion >= NETWORK_CONGESTION_THRESHOLDS.BUSY) {
return {
statusLabel: 'busy',
tooltipLabel: 'highLowercase',
color,
sliderTickValue,
};
} else if (networkCongestion >= NETWORK_CONGESTION_THRESHOLDS.STABLE) {
return {
statusLabel: 'stable',
tooltipLabel: 'stableLowercase',
color,
sliderTickValue,
};
}
return {
statusLabel: 'notBusy',
tooltipLabel: 'lowLowercase',
color,
sliderTickValue,
};
};
const StatusSlider = () => {
const { gasFeeEstimates } = useGasFeeContext();
const statusInfo = determineStatusInfo(gasFeeEstimates.networkCongestion);
return (
<NetworkStabilityTooltip
color={statusInfo.color}
tooltipLabel={statusInfo.tooltipLabel}
>
<div className="status-slider">
<div className="status-slider__arrow-container">
<div
className="status-slider__arrow-border"
style={{
marginLeft: `${statusInfo.sliderTickValue}%`,
}}
data-testid="status-slider-arrow-border"
>
<div
className="status-slider__arrow"
style={{
borderTopColor: statusInfo.color,
}}
data-testid="status-slider-arrow"
/>
</div>
</div>
<div className="status-slider__line" />
<div
className="status-slider__label"
style={{ color: statusInfo.color }}
data-testid="status-slider-label"
>
<I18nValue messageKey={statusInfo.statusLabel} />
</div>
</div>
</NetworkStabilityTooltip>
);
};
export default StatusSlider;