2022-03-11 19:59:58 +01:00
|
|
|
import { constant, times, uniq, zip } from 'lodash';
|
2022-01-06 03:47:26 +01:00
|
|
|
import BigNumber from 'bignumber.js';
|
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
2022-04-14 17:50:48 +02:00
|
|
|
import {
|
|
|
|
GAS_RECOMMENDATIONS,
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
} from '../../../shared/constants/gas';
|
2022-01-06 03:47:26 +01:00
|
|
|
import { multiplyCurrencies } from '../../../shared/modules/conversion.utils';
|
2022-03-11 19:59:58 +01:00
|
|
|
import {
|
|
|
|
bnGreaterThan,
|
|
|
|
isNullish,
|
|
|
|
roundToDecimalPlacesRemovingExtraZeroes,
|
|
|
|
} from './util';
|
2022-01-06 03:47:26 +01:00
|
|
|
import { hexWEIToDecGWEI } from './conversions.util';
|
|
|
|
|
|
|
|
export const gasEstimateGreaterThanGasUsedPlusTenPercent = (
|
2022-01-26 19:18:43 +01:00
|
|
|
gasUsed,
|
2022-01-06 03:47:26 +01:00
|
|
|
gasFeeEstimates,
|
|
|
|
estimate,
|
|
|
|
) => {
|
2022-01-26 19:18:43 +01:00
|
|
|
let { maxFeePerGas: maxFeePerGasInTransaction } = gasUsed;
|
2022-01-06 03:47:26 +01:00
|
|
|
maxFeePerGasInTransaction = new BigNumber(
|
|
|
|
hexWEIToDecGWEI(addTenPercentAndRound(maxFeePerGasInTransaction)),
|
|
|
|
);
|
|
|
|
|
|
|
|
const maxFeePerGasFromEstimate =
|
|
|
|
gasFeeEstimates[estimate]?.suggestedMaxFeePerGas;
|
|
|
|
return bnGreaterThan(maxFeePerGasFromEstimate, maxFeePerGasInTransaction);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple helper to save on duplication to multiply the supplied wei hex string
|
|
|
|
* by 1.10 to get bare minimum new gas fee.
|
|
|
|
*
|
|
|
|
* @param {string | undefined} hexStringValue - hex value in wei to be incremented
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param conversionOptions
|
|
|
|
* @returns {string | undefined} hex value in WEI 10% higher than the param.
|
2022-01-06 03:47:26 +01:00
|
|
|
*/
|
|
|
|
export function addTenPercent(hexStringValue, conversionOptions = {}) {
|
2022-01-06 23:56:51 +01:00
|
|
|
if (hexStringValue === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2022-01-06 03:47:26 +01:00
|
|
|
return addHexPrefix(
|
|
|
|
multiplyCurrencies(hexStringValue, 1.1, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 10,
|
|
|
|
numberOfDecimals: 0,
|
|
|
|
...conversionOptions,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple helper to save on duplication to multiply the supplied wei hex string
|
|
|
|
* by 1.10 to get bare minimum new gas fee.
|
|
|
|
*
|
|
|
|
* @param {string | undefined} hexStringValue - hex value in wei to be incremented
|
2022-01-07 16:57:33 +01:00
|
|
|
* @returns {string | undefined} hex value in WEI 10% higher than the param.
|
2022-01-06 03:47:26 +01:00
|
|
|
*/
|
|
|
|
export function addTenPercentAndRound(hexStringValue) {
|
|
|
|
return addTenPercent(hexStringValue, { numberOfDecimals: 0 });
|
|
|
|
}
|
2022-01-26 19:18:43 +01:00
|
|
|
|
2022-01-31 06:51:43 +01:00
|
|
|
export function isMetamaskSuggestedGasEstimate(estimate) {
|
2022-01-26 19:18:43 +01:00
|
|
|
return [
|
|
|
|
GAS_RECOMMENDATIONS.HIGH,
|
|
|
|
GAS_RECOMMENDATIONS.MEDIUM,
|
|
|
|
GAS_RECOMMENDATIONS.LOW,
|
|
|
|
].includes(estimate);
|
|
|
|
}
|
2022-03-11 19:59:58 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Formats a singular gas fee or a range of gas fees by rounding them to the
|
|
|
|
* given precisions and then arranging them as a string.
|
|
|
|
*
|
|
|
|
* @param {string | [string, string] | null | undefined} feeOrFeeRange - The fee
|
|
|
|
* in GWEI or range of fees in GWEI.
|
|
|
|
* @param {object} options - The options.
|
|
|
|
* @param {number | [number, number]} options.precision - The precision(s) to
|
|
|
|
* use when formatting the fee(s).
|
|
|
|
* @returns A string which represents the formatted version of the fee or fee
|
|
|
|
* range.
|
|
|
|
*/
|
|
|
|
export function formatGasFeeOrFeeRange(
|
|
|
|
feeOrFeeRange,
|
|
|
|
{ precision: precisionOrPrecisions = 2 } = {},
|
|
|
|
) {
|
|
|
|
if (
|
|
|
|
isNullish(feeOrFeeRange) ||
|
|
|
|
(Array.isArray(feeOrFeeRange) && feeOrFeeRange.length === 0)
|
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const range = Array.isArray(feeOrFeeRange)
|
|
|
|
? feeOrFeeRange.slice(0, 2)
|
|
|
|
: [feeOrFeeRange];
|
|
|
|
const precisions = Array.isArray(precisionOrPrecisions)
|
|
|
|
? precisionOrPrecisions.slice(0, 2)
|
|
|
|
: times(range.length, constant(precisionOrPrecisions));
|
|
|
|
const formattedRange = uniq(
|
|
|
|
zip(range, precisions).map(([fee, precision]) => {
|
|
|
|
return precision === undefined
|
|
|
|
? fee
|
|
|
|
: roundToDecimalPlacesRemovingExtraZeroes(fee, precision);
|
|
|
|
}),
|
|
|
|
).join(' - ');
|
|
|
|
|
|
|
|
return `${formattedRange} GWEI`;
|
|
|
|
}
|
2022-04-14 17:50:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method for determining whether an edit gas mode is either a speed up or cancel transaction
|
|
|
|
*
|
|
|
|
* @param {string | undefined} editGasMode - One of 'speed-up', 'cancel', 'modify-in-place', or 'swaps'
|
|
|
|
* @returns boolean
|
|
|
|
*/
|
|
|
|
export function editGasModeIsSpeedUpOrCancel(editGasMode) {
|
|
|
|
return (
|
|
|
|
editGasMode === EDIT_GAS_MODES.CANCEL ||
|
|
|
|
editGasMode === EDIT_GAS_MODES.SPEED_UP
|
|
|
|
);
|
|
|
|
}
|