1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/ui/selectors/custom-gas.js

221 lines
5.9 KiB
JavaScript
Raw Normal View History

import { addHexPrefix } from '../../app/scripts/lib/util';
import { decEthToConvertedCurrency } from '../../shared/modules/conversion.utils';
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
import { formatETHFee } from '../helpers/utils/formatters';
import { getGasPrice } from '../ducks/send';
import { GAS_ESTIMATE_TYPES as GAS_FEE_CONTROLLER_ESTIMATE_TYPES } from '../../shared/constants/gas';
import {
getGasEstimateType,
getGasFeeEstimates,
isEIP1559Network,
} from '../ducks/metamask/metamask';
import { calcGasTotal } from '../../shared/lib/transactions-controller-utils';
import { Numeric } from '../../shared/modules/Numeric';
import { EtherDenomination } from '../../shared/constants/common';
import { getIsMainnet } from '.';
2020-11-03 00:41:28 +01:00
export function getCustomGasLimit(state) {
return state.gas.customData.limit;
}
2020-11-03 00:41:28 +01:00
export function getCustomGasPrice(state) {
return state.gas.customData.price;
}
2020-11-03 00:41:28 +01:00
export function getBasicGasEstimateLoadingStatus(state) {
return getIsGasEstimatesFetched(state) === false;
}
2020-11-03 00:41:28 +01:00
export function getAveragePriceEstimateInHexWEI(state) {
const averagePriceEstimate = getAverageEstimate(state);
return getGasPriceInHexWei(averagePriceEstimate);
}
2020-11-03 00:41:28 +01:00
export function getFastPriceEstimateInHexWEI(state) {
const fastPriceEstimate = getFastPriceEstimate(state);
return getGasPriceInHexWei(fastPriceEstimate || '0x0');
}
2020-11-03 00:41:28 +01:00
export function getDefaultActiveButtonIndex(
gasButtonInfo,
customGasPriceInHex,
gasPrice,
) {
return gasButtonInfo
.map(({ priceInHexWei }) => priceInHexWei)
.lastIndexOf(addHexPrefix(customGasPriceInHex || gasPrice));
}
2020-11-03 00:41:28 +01:00
export function getSafeLowEstimate(state) {
const gasFeeEstimates = getGasFeeEstimates(state);
const gasEstimateType = getGasEstimateType(state);
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
? gasFeeEstimates?.low
: null;
}
export function getAverageEstimate(state) {
const gasFeeEstimates = getGasFeeEstimates(state);
const gasEstimateType = getGasEstimateType(state);
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
? gasFeeEstimates?.medium
: null;
}
export function getFastPriceEstimate(state) {
const gasFeeEstimates = getGasFeeEstimates(state);
const gasEstimateType = getGasEstimateType(state);
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
? gasFeeEstimates?.high
: null;
}
Switch gas price estimation in swaps to metaswap-api /gasPrices (#9599) Adds swaps-gas-customization-modal and utilize in swaps Remove swaps specific code from gas-modal-page-container/ Remove slow estimate data from swaps-gas-customization-modal.container Use average as lower safe price limit in swaps-gas-customization-modal Lint fix Fix up unit tests Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Remove stale properties from gas-modal-page-container.component.js Replace use of isCustomPrice safe with isCustomSwapsGasPriceSafe, in swaps-gas-customization-modal Remove use of averageIsSafe in isCustomPriceSafe function Stop calling resetCustomGasState in swaps Refactor 'setter' type actions and creators to 'event based', for swaps slice custom gas logic Replace use of advanced-tab-content.component with advanceGasInputs in swaps gas customization component Add validation for the gasPrices endpoint swaps custom gas price should be considered safe if >= to average Update renderDataSummary unit test Lint fix Remove customOnHideOpts for swapsGasCustomizationModal in modal.js Better handling for swaps gas price loading and failure states Improve semantics: isCustomSwapsGasPriceSafe renamed to isCustomSwapsGasPriceUnSafe Mutate state directly in swaps gas slice reducer Remove unused params More reliable tracking of speed setting for Gas Fees Changed metrics event Lint fix Throw error when fetchSwapsGasPrices response is invalid add disableSave and customTotalSupplement to swaps-gas-customization container return Update ui/app/ducks/swaps/swaps.js Co-authored-by: Mark Stacey <markjstacey@gmail.com> Improve error handling in fetchMetaSwapsGasPriceEstimates Remove metricsEvent from swaps-gas-customization-modal context Base check of gas speed type in swaps-gas-customization-modal on gasEstimateType Improve naming of variable and functions use to set customPriceIsSafe prop of AdvancedGasInputs in swaps-gas-customization-modal Simplify sinon spy/stub code in gas-price-button-group-component.test.js Remove unnecessary getSwapsFallbackGasPrice call in swaps-gas-customization-modal Remove use of getSwapsTradeTxParams and clean up related gas price logic in swaps Improve validator of SWAP_GAS_PRICE_VALIDATOR Ensure default tradeValue
2020-11-04 17:14:08 +01:00
export function isCustomPriceSafe(state) {
const safeLow = getSafeLowEstimate(state);
const customGasPrice = getCustomGasPrice(state);
if (!customGasPrice) {
return true;
}
if (!safeLow) {
return false;
}
const customPriceSafe = new Numeric(customGasPrice, 16, EtherDenomination.WEI)
.toDenomination(EtherDenomination.GWEI)
.greaterThan(safeLow, 10);
return customPriceSafe;
}
export function isCustomPriceSafeForCustomNetwork(state) {
const estimatedPrice = getAverageEstimate(state);
const customGasPrice = getCustomGasPrice(state);
if (!customGasPrice) {
return true;
}
if (!estimatedPrice) {
return false;
}
const customPriceSafe = new Numeric(customGasPrice, 16, EtherDenomination.WEI)
.toDenomination(EtherDenomination.GWEI)
.greaterThan(estimatedPrice, 10);
return customPriceSafe;
}
export function isCustomPriceExcessive(state, checkSend = false) {
const customPrice = checkSend ? getGasPrice(state) : getCustomGasPrice(state);
const fastPrice = getFastPriceEstimate(state);
if (!customPrice || !fastPrice) {
return false;
}
// Custom gas should be considered excessive when it is 1.5 times greater than the fastest estimate.
const customPriceExcessive = new Numeric(
customPrice,
16,
EtherDenomination.WEI,
)
.toDenomination(EtherDenomination.GWEI)
.greaterThan(Math.floor(fastPrice * 1.5), 10);
return customPriceExcessive;
}
2020-11-03 00:41:28 +01:00
export function basicPriceEstimateToETHTotal(
estimate,
gasLimit,
numberOfDecimals = 9,
) {
return new Numeric(
calcGasTotal(gasLimit, estimate),
16,
EtherDenomination.GWEI,
)
.round(numberOfDecimals)
.toBase(10)
.toString();
}
export function getRenderableEthFee(
estimate,
gasLimit,
numberOfDecimals = 9,
nativeCurrency = 'ETH',
) {
const value = new Numeric(estimate, 10).toBase(16).toString();
const fee = basicPriceEstimateToETHTotal(value, gasLimit, numberOfDecimals);
return formatETHFee(fee, nativeCurrency);
}
2020-11-03 00:41:28 +01:00
export function getRenderableConvertedCurrencyFee(
estimate,
gasLimit,
convertedCurrency,
conversionRate,
) {
const value = new Numeric(estimate, 10).toBase(16).toString();
const fee = basicPriceEstimateToETHTotal(value, gasLimit);
const feeInCurrency = decEthToConvertedCurrency(
2020-11-03 00:41:28 +01:00
fee,
convertedCurrency,
conversionRate,
);
return formatCurrency(feeInCurrency, convertedCurrency);
}
2020-11-03 00:41:28 +01:00
export function priceEstimateToWei(priceEstimate) {
return new Numeric(priceEstimate, 16, EtherDenomination.GWEI)
.toDenomination(EtherDenomination.WEI)
.round(9)
.toString();
}
2020-11-03 00:41:28 +01:00
export function getGasPriceInHexWei(price) {
const value = new Numeric(price, 10).toBase(16).toString();
return addHexPrefix(priceEstimateToWei(value));
}
export function getIsEthGasPriceFetched(state) {
const gasEstimateType = getGasEstimateType(state);
return (
gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.ETH_GASPRICE &&
getIsMainnet(state)
);
}
export function getIsCustomNetworkGasPriceFetched(state) {
const gasEstimateType = getGasEstimateType(state);
return (
gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.ETH_GASPRICE &&
!getIsMainnet(state)
);
}
export function getNoGasPriceFetched(state) {
const gasEstimateType = getGasEstimateType(state);
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.NONE;
}
export function getIsGasEstimatesFetched(state) {
const gasEstimateType = getGasEstimateType(state);
if (isEIP1559Network(state)) {
return false;
}
return gasEstimateType !== GAS_FEE_CONTROLLER_ESTIMATE_TYPES.NONE;
}