2021-04-28 21:53:59 +02:00
|
|
|
import { addHexPrefix } from '../../app/scripts/lib/util';
|
2018-08-16 14:28:27 +02:00
|
|
|
import {
|
|
|
|
conversionUtil,
|
2018-12-10 22:51:00 +01:00
|
|
|
conversionGreaterThan,
|
2021-07-06 19:48:49 +02:00
|
|
|
} from '../../shared/modules/conversion.utils';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { formatCurrency } from '../helpers/utils/confirm-tx.util';
|
|
|
|
import { decEthToConvertedCurrency as ethTotalToConvertedCurrency } from '../helpers/utils/conversions.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';
|
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
|
|
|
}
|
|
|
|
|
2018-12-10 22:51:00 +01:00
|
|
|
const customPriceSafe = conversionGreaterThan(
|
|
|
|
{
|
|
|
|
value: customGasPrice,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
},
|
2020-11-04 17:14:08 +01:00
|
|
|
{ value: safeLow, fromNumericBase: 'dec' },
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-24 18:59:47 +02:00
|
|
|
const customPriceSafe = conversionGreaterThan(
|
|
|
|
{
|
|
|
|
value: customGasPrice,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
},
|
|
|
|
{ value: estimatedPrice, fromNumericBase: 'dec' },
|
|
|
|
);
|
|
|
|
|
|
|
|
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.
|
|
|
|
const customPriceExcessive = conversionGreaterThan(
|
|
|
|
{
|
|
|
|
value: customPrice,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromDenomination: 'WEI',
|
|
|
|
toDenomination: 'GWEI',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
value: Math.floor(fastPrice * 1.5),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return customPriceExcessive;
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function basicPriceEstimateToETHTotal(
|
|
|
|
estimate,
|
|
|
|
gasLimit,
|
|
|
|
numberOfDecimals = 9,
|
|
|
|
) {
|
2018-08-16 14:28:27 +02:00
|
|
|
return conversionUtil(calcGasTotal(gasLimit, estimate), {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'dec',
|
|
|
|
fromDenomination: 'GWEI',
|
2018-09-13 14:35:17 +02:00
|
|
|
numberOfDecimals,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
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',
|
|
|
|
) {
|
2020-11-03 00:41:28 +01:00
|
|
|
const value = conversionUtil(estimate, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
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,
|
|
|
|
) {
|
|
|
|
const value = conversionUtil(estimate, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
const fee = basicPriceEstimateToETHTotal(value, gasLimit);
|
2020-11-03 00:41:28 +01:00
|
|
|
const feeInCurrency = ethTotalToConvertedCurrency(
|
|
|
|
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) {
|
2018-08-16 14:28:27 +02:00
|
|
|
return conversionUtil(priceEstimate, {
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
fromDenomination: 'GWEI',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
numberOfDecimals: 9,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2018-08-16 14:28:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export function getGasPriceInHexWei(price) {
|
|
|
|
const value = conversionUtil(price, {
|
|
|
|
fromNumericBase: 'dec',
|
|
|
|
toNumericBase: 'hex',
|
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
|
|
|
}
|