2021-04-28 21:53:59 +02:00
|
|
|
import { addHexPrefix } from '../../app/scripts/lib/util';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { decEthToConvertedCurrency } from '../../shared/modules/conversion.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
|
|
|
|
import { formatETHFee } from '../helpers/utils/formatters';
|
2018-08-16 14:28:27 +02:00
|
|
|
|
2022-09-28 22:00:57 +02:00
|
|
|
import { getGasPrice } from '../ducks/send';
|
|
|
|
import { GAS_ESTIMATE_TYPES as GAS_FEE_CONTROLLER_ESTIMATE_TYPES } from '../../shared/constants/gas';
|
2021-07-16 18:06:32 +02:00
|
|
|
import {
|
|
|
|
getGasEstimateType,
|
|
|
|
getGasFeeEstimates,
|
|
|
|
isEIP1559Network,
|
|
|
|
} from '../ducks/metamask/metamask';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { calcGasTotal } from '../../shared/lib/transactions-controller-utils';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { Numeric } from '../../shared/modules/Numeric';
|
|
|
|
import { EtherDenomination } from '../../shared/constants/common';
|
2022-09-28 22:00:57 +02:00
|
|
|
import { getIsMainnet } from '.';
|
2018-09-13 14:35:17 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getCustomGasLimit(state) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return state.gas.customData.limit;
|
2018-08-02 18:02:22 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getCustomGasPrice(state) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return state.gas.customData.price;
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getBasicGasEstimateLoadingStatus(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
return getIsGasEstimatesFetched(state) === false;
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getAveragePriceEstimateInHexWEI(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const averagePriceEstimate = getAverageEstimate(state);
|
|
|
|
|
2021-04-28 20:02:01 +02:00
|
|
|
return getGasPriceInHexWei(averagePriceEstimate);
|
2018-09-13 10:47:05 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getFastPriceEstimateInHexWEI(state) {
|
2021-03-05 18:32:09 +01:00
|
|
|
const fastPriceEstimate = getFastPriceEstimate(state);
|
2021-02-04 19:15:23 +01:00
|
|
|
return getGasPriceInHexWei(fastPriceEstimate || '0x0');
|
2018-11-13 17:36:52 +01:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getDefaultActiveButtonIndex(
|
|
|
|
gasButtonInfo,
|
|
|
|
customGasPriceInHex,
|
|
|
|
gasPrice,
|
|
|
|
) {
|
2020-10-12 18:51:17 +02:00
|
|
|
return gasButtonInfo
|
|
|
|
.map(({ priceInHexWei }) => priceInHexWei)
|
2021-02-04 19:15:23 +01:00
|
|
|
.lastIndexOf(addHexPrefix(customGasPriceInHex || gasPrice));
|
2018-09-13 10:47:05 +02:00
|
|
|
}
|
2018-08-16 14:28:27 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getSafeLowEstimate(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasFeeEstimates = getGasFeeEstimates(state);
|
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
|
|
|
|
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
|
|
|
|
? gasFeeEstimates?.low
|
|
|
|
: null;
|
|
|
|
}
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
export function getAverageEstimate(state) {
|
|
|
|
const gasFeeEstimates = getGasFeeEstimates(state);
|
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
|
|
|
|
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
|
|
|
|
? gasFeeEstimates?.medium
|
|
|
|
: null;
|
2018-12-10 22:51:00 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 18:32:09 +01:00
|
|
|
export function getFastPriceEstimate(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasFeeEstimates = getGasFeeEstimates(state);
|
2021-03-05 18:32:09 +01:00
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
|
|
|
|
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.LEGACY
|
|
|
|
? gasFeeEstimates?.high
|
|
|
|
: null;
|
2021-03-05 18:32:09 +01:00
|
|
|
}
|
|
|
|
|
2020-11-04 17:14:08 +01:00
|
|
|
export function isCustomPriceSafe(state) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const safeLow = getSafeLowEstimate(state);
|
2020-10-17 11:13:51 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const customGasPrice = getCustomGasPrice(state);
|
2018-12-10 22:51:00 +01:00
|
|
|
|
|
|
|
if (!customGasPrice) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return true;
|
2018-12-10 22:51:00 +01:00
|
|
|
}
|
|
|
|
|
2021-03-02 23:19:56 +01:00
|
|
|
if (!safeLow) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return false;
|
2019-06-18 14:17:14 +02:00
|
|
|
}
|
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
const customPriceSafe = new Numeric(customGasPrice, 16, EtherDenomination.WEI)
|
|
|
|
.toDenomination(EtherDenomination.GWEI)
|
|
|
|
.greaterThan(safeLow, 10);
|
2018-12-10 22:51:00 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return customPriceSafe;
|
2018-12-10 22:51:00 +01:00
|
|
|
}
|
|
|
|
|
2021-06-24 18:59:47 +02:00
|
|
|
export function isCustomPriceSafeForCustomNetwork(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const estimatedPrice = getAverageEstimate(state);
|
2021-06-24 18:59:47 +02:00
|
|
|
|
|
|
|
const customGasPrice = getCustomGasPrice(state);
|
|
|
|
|
|
|
|
if (!customGasPrice) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:26:34 +02:00
|
|
|
if (!estimatedPrice) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
const customPriceSafe = new Numeric(customGasPrice, 16, EtherDenomination.WEI)
|
|
|
|
.toDenomination(EtherDenomination.GWEI)
|
|
|
|
.greaterThan(estimatedPrice, 10);
|
2021-06-24 18:59:47 +02:00
|
|
|
|
|
|
|
return customPriceSafe;
|
|
|
|
}
|
|
|
|
|
2021-03-05 18:32:09 +01:00
|
|
|
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.
|
2023-01-24 15:44:49 +01:00
|
|
|
const customPriceExcessive = new Numeric(
|
|
|
|
customPrice,
|
|
|
|
16,
|
|
|
|
EtherDenomination.WEI,
|
|
|
|
)
|
|
|
|
.toDenomination(EtherDenomination.GWEI)
|
|
|
|
.greaterThan(Math.floor(fastPrice * 1.5), 10);
|
2021-03-05 18:32:09 +01:00
|
|
|
|
|
|
|
return customPriceExcessive;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function basicPriceEstimateToETHTotal(
|
|
|
|
estimate,
|
|
|
|
gasLimit,
|
|
|
|
numberOfDecimals = 9,
|
|
|
|
) {
|
2023-01-24 15:44:49 +01:00
|
|
|
return new Numeric(
|
|
|
|
calcGasTotal(gasLimit, estimate),
|
|
|
|
16,
|
|
|
|
EtherDenomination.GWEI,
|
|
|
|
)
|
|
|
|
.round(numberOfDecimals)
|
|
|
|
.toBase(10)
|
|
|
|
.toString();
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:30:06 +02:00
|
|
|
export function getRenderableEthFee(
|
|
|
|
estimate,
|
|
|
|
gasLimit,
|
|
|
|
numberOfDecimals = 9,
|
|
|
|
nativeCurrency = 'ETH',
|
|
|
|
) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const value = new Numeric(estimate, 10).toBase(16).toString();
|
2021-02-04 19:15:23 +01:00
|
|
|
const fee = basicPriceEstimateToETHTotal(value, gasLimit, numberOfDecimals);
|
2021-03-29 21:30:06 +02:00
|
|
|
return formatETHFee(fee, nativeCurrency);
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getRenderableConvertedCurrencyFee(
|
|
|
|
estimate,
|
|
|
|
gasLimit,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
|
|
|
) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const value = new Numeric(estimate, 10).toBase(16).toString();
|
2021-02-04 19:15:23 +01:00
|
|
|
const fee = basicPriceEstimateToETHTotal(value, gasLimit);
|
2023-01-20 18:04:37 +01:00
|
|
|
const feeInCurrency = decEthToConvertedCurrency(
|
2020-11-03 00:41:28 +01:00
|
|
|
fee,
|
|
|
|
convertedCurrency,
|
|
|
|
conversionRate,
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
return formatCurrency(feeInCurrency, convertedCurrency);
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function priceEstimateToWei(priceEstimate) {
|
2023-01-24 15:44:49 +01:00
|
|
|
return new Numeric(priceEstimate, 16, EtherDenomination.GWEI)
|
|
|
|
.toDenomination(EtherDenomination.WEI)
|
|
|
|
.round(9)
|
|
|
|
.toString();
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getGasPriceInHexWei(price) {
|
2023-01-24 15:44:49 +01:00
|
|
|
const value = new Numeric(price, 10).toBase(16).toString();
|
2021-02-04 19:15:23 +01:00
|
|
|
return addHexPrefix(priceEstimateToWei(value));
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2021-04-28 20:02:01 +02:00
|
|
|
export function getIsEthGasPriceFetched(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
return (
|
|
|
|
gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.ETH_GASPRICE &&
|
|
|
|
getIsMainnet(state)
|
2021-04-28 20:02:01 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-30 20:26:34 +02:00
|
|
|
export function getIsCustomNetworkGasPriceFetched(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
return (
|
|
|
|
gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.ETH_GASPRICE &&
|
|
|
|
!getIsMainnet(state)
|
2021-06-30 20:26:34 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-28 20:02:01 +02:00
|
|
|
export function getNoGasPriceFetched(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
return gasEstimateType === GAS_FEE_CONTROLLER_ESTIMATE_TYPES.NONE;
|
2021-05-20 17:32:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getIsGasEstimatesFetched(state) {
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
|
|
|
if (isEIP1559Network(state)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return gasEstimateType !== GAS_FEE_CONTROLLER_ESTIMATE_TYPES.NONE;
|
2021-04-28 20:02:01 +02:00
|
|
|
}
|