2021-07-09 20:26:54 +02:00
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
|
|
|
import { useCallback, useState } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2021-08-03 04:09:21 +02:00
|
|
|
import { findKey, isEqual } from 'lodash';
|
2021-07-30 13:35:30 +02:00
|
|
|
import {
|
|
|
|
GAS_ESTIMATE_TYPES,
|
|
|
|
EDIT_GAS_MODES,
|
|
|
|
GAS_LIMITS,
|
|
|
|
} from '../../shared/constants/gas';
|
|
|
|
import {
|
|
|
|
multiplyCurrencies,
|
|
|
|
conversionLessThan,
|
2021-07-31 00:46:31 +02:00
|
|
|
conversionGreaterThan,
|
2021-07-30 13:35:30 +02:00
|
|
|
} from '../../shared/modules/conversion.utils';
|
2021-07-09 20:26:54 +02:00
|
|
|
import {
|
|
|
|
getMaximumGasTotalInHexWei,
|
|
|
|
getMinimumGasTotalInHexWei,
|
|
|
|
} from '../../shared/modules/gas.utils';
|
|
|
|
import { PRIMARY, SECONDARY } from '../helpers/constants/common';
|
2021-08-03 00:52:18 +02:00
|
|
|
import {
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
getShouldShowFiat,
|
|
|
|
getSelectedAccount,
|
|
|
|
} from '../selectors';
|
2021-07-31 02:45:18 +02:00
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
import {
|
2021-07-27 17:12:56 +02:00
|
|
|
hexWEIToDecGWEI,
|
2021-07-09 20:26:54 +02:00
|
|
|
decGWEIToHexWEI,
|
|
|
|
decimalToHex,
|
2021-07-27 17:12:56 +02:00
|
|
|
hexToDecimal,
|
2021-07-31 00:46:31 +02:00
|
|
|
addHexes,
|
2021-07-09 20:26:54 +02:00
|
|
|
} from '../helpers/utils/conversions.util';
|
2021-07-20 21:34:32 +02:00
|
|
|
import { GAS_FORM_ERRORS } from '../helpers/constants/gas';
|
2021-08-03 00:52:18 +02:00
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
import { useCurrencyDisplay } from './useCurrencyDisplay';
|
|
|
|
import { useGasFeeEstimates } from './useGasFeeEstimates';
|
|
|
|
import { useUserPreferencedCurrency } from './useUserPreferencedCurrency';
|
|
|
|
|
2021-07-20 21:34:32 +02:00
|
|
|
const HIGH_FEE_WARNING_MULTIPLIER = 1.5;
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
/**
|
|
|
|
* Opaque string type representing a decimal (base 10) number in GWEI
|
|
|
|
* @typedef {`${number}`} DecGweiString
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* String value representing the active estimate level to use
|
|
|
|
* @typedef {'low' | 'medium' | 'high'} EstimateLevel
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls out gasPrice estimate from either of the two gasPrice estimation
|
|
|
|
* sources, based on the gasEstimateType and current estimateToUse.
|
|
|
|
* @param {{import(
|
|
|
|
* '@metamask/controllers'
|
|
|
|
* ).GasFeeState['gasFeeEstimates']}} gasFeeEstimates - estimates returned from
|
|
|
|
* the controller
|
|
|
|
* @param {import(
|
|
|
|
* './useGasFeeEstimates'
|
|
|
|
* ).GasEstimates} gasEstimateType - type of estimate returned from controller
|
|
|
|
* @param {EstimateLevel} estimateToUse - current estimate level to use
|
|
|
|
* @returns {[DecGweiString]} - gasPrice estimate to use or null
|
|
|
|
*/
|
|
|
|
function getGasPriceEstimate(gasFeeEstimates, gasEstimateType, estimateToUse) {
|
|
|
|
if (gasEstimateType === GAS_ESTIMATE_TYPES.LEGACY) {
|
|
|
|
return gasFeeEstimates?.[estimateToUse] ?? '0';
|
|
|
|
} else if (gasEstimateType === GAS_ESTIMATE_TYPES.ETH_GASPRICE) {
|
|
|
|
return gasFeeEstimates?.gasPrice ?? '0';
|
|
|
|
}
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pulls out gas fee estimate from the estimates returned from controller,
|
|
|
|
* based on the gasEstimateType and current estimateToUse.
|
|
|
|
* @param {'maxFeePerGas' | 'maxPriorityFeePerGas'} field - field to select
|
|
|
|
* @param {{import(
|
|
|
|
* '@metamask/controllers'
|
|
|
|
* ).GasFeeState['gasFeeEstimates']}} gasFeeEstimates - estimates returned from
|
|
|
|
* the controller
|
|
|
|
* @param {import(
|
|
|
|
* './useGasFeeEstimates'
|
|
|
|
* ).GasEstimates} gasEstimateType - type of estimate returned from controller
|
|
|
|
* @param {EstimateLevel} estimateToUse - current estimate level to use
|
|
|
|
* @returns {[DecGweiString]} - gas fee estimate to use or null
|
|
|
|
*/
|
|
|
|
function getGasFeeEstimate(
|
|
|
|
field,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimateToUse,
|
|
|
|
) {
|
|
|
|
if (gasEstimateType === GAS_ESTIMATE_TYPES.FEE_MARKET) {
|
|
|
|
return gasFeeEstimates?.[estimateToUse]?.[field] ?? '0';
|
|
|
|
}
|
|
|
|
return '0';
|
|
|
|
}
|
|
|
|
|
2021-07-27 17:12:56 +02:00
|
|
|
/**
|
|
|
|
* This method tries to determine if any estimate level matches the
|
|
|
|
* current maxFeePerGas and maxPriorityFeePerGas values. If we find
|
|
|
|
* a match, we can pre-select a radio button in the RadioGroup
|
|
|
|
*/
|
|
|
|
function getMatchingEstimateFromGasFees(
|
|
|
|
gasFeeEstimates,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gasPrice,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559,
|
2021-07-27 17:12:56 +02:00
|
|
|
) {
|
|
|
|
return (
|
|
|
|
findKey(gasFeeEstimates, (estimate) => {
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-27 17:12:56 +02:00
|
|
|
return (
|
2021-07-29 20:30:21 +02:00
|
|
|
Number(estimate?.suggestedMaxPriorityFeePerGas) ===
|
|
|
|
Number(maxPriorityFeePerGas) &&
|
|
|
|
Number(estimate?.suggestedMaxFeePerGas) === Number(maxFeePerGas)
|
2021-07-27 17:12:56 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return estimate?.gasPrice === gasPrice;
|
|
|
|
}) || null
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
/**
|
|
|
|
* @typedef {Object} GasFeeInputReturnType
|
|
|
|
* @property {DecGweiString} [maxFeePerGas] - the maxFeePerGas input value.
|
|
|
|
* @property {string} [maxFeePerGasFiat] - the maxFeePerGas converted to the
|
|
|
|
* user's preferred currency.
|
|
|
|
* @property {(DecGweiString) => void} setMaxFeePerGas - state setter method to
|
|
|
|
* update the maxFeePerGas.
|
|
|
|
* @property {DecGweiString} [maxPriorityFeePerGas] - the maxPriorityFeePerGas
|
|
|
|
* input value.
|
|
|
|
* @property {string} [maxPriorityFeePerGasFiat] - the maxPriorityFeePerGas
|
|
|
|
* converted to the user's preferred currency.
|
|
|
|
* @property {(DecGweiString) => void} setMaxPriorityFeePerGas - state setter
|
|
|
|
* method to update the maxPriorityFeePerGas.
|
|
|
|
* @property {DecGweiString} [gasPrice] - the gasPrice input value.
|
|
|
|
* @property {(DecGweiString) => void} setGasPrice - state setter method to
|
|
|
|
* update the gasPrice.
|
|
|
|
* @property {DecGweiString} gasLimit - the gasLimit input value.
|
|
|
|
* @property {(DecGweiString) => void} setGasLimit - state setter method to
|
|
|
|
* update the gasLimit.
|
|
|
|
* @property {EstimateLevel} [estimateToUse] - the estimate level currently
|
|
|
|
* selected. This will be null if the user has ejected from using the
|
|
|
|
* estimates.
|
|
|
|
* @property {([EstimateLevel]) => void} setEstimateToUse - Setter method for
|
|
|
|
* choosing which EstimateLevel to use.
|
|
|
|
* @property {string} [estimatedMinimumFiat] - The amount estimated to be paid
|
|
|
|
* based on current network conditions. Expressed in user's preferred
|
|
|
|
* currency.
|
|
|
|
* @property {string} [estimatedMaximumFiat] - the maximum amount estimated to be
|
|
|
|
* paid if current network transaction volume increases. Expressed in user's
|
|
|
|
* preferred currency.
|
|
|
|
* @property {string} [estimatedMaximumNative] - the maximum amount estimated to
|
|
|
|
* be paid if the current network transaction volume increases. Expressed in
|
|
|
|
* the network's native currency.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Uses gasFeeEstimates and state to keep track of user gas fee inputs.
|
|
|
|
* Will update the gas fee state when estimates update if the user has not yet
|
|
|
|
* modified the fields.
|
|
|
|
* @param {EstimateLevel} defaultEstimateToUse - which estimate
|
|
|
|
* level to default the 'estimateToUse' state variable to.
|
|
|
|
* @returns {GasFeeInputReturnType & import(
|
|
|
|
* './useGasFeeEstimates'
|
|
|
|
* ).GasEstimates} - gas fee input state and the GasFeeEstimates object
|
|
|
|
*/
|
2021-07-30 13:35:30 +02:00
|
|
|
export function useGasFeeInputs(
|
|
|
|
defaultEstimateToUse = 'medium',
|
|
|
|
transaction,
|
2021-08-02 22:28:30 +02:00
|
|
|
minimumGasLimit = '0x5208',
|
2021-07-30 13:35:30 +02:00
|
|
|
editGasMode,
|
|
|
|
) {
|
2021-07-31 00:46:31 +02:00
|
|
|
const { balance: ethBalance } = useSelector(getSelectedAccount);
|
2021-08-03 00:52:18 +02:00
|
|
|
const networkAndAccountSupports1559 = useSelector(
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
);
|
2021-07-09 20:26:54 +02:00
|
|
|
// We need to know whether to show fiat conversions or not, so that we can
|
|
|
|
// default our fiat values to empty strings if showing fiat is not wanted or
|
|
|
|
// possible.
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
|
|
|
|
|
|
|
// We need to know the current network's currency and its decimal precision
|
|
|
|
// to calculate the amount to display to the user.
|
|
|
|
const {
|
|
|
|
currency: primaryCurrency,
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
|
|
|
} = useUserPreferencedCurrency(PRIMARY);
|
|
|
|
|
|
|
|
// For calculating the value of gas fees in the user's preferred currency we
|
|
|
|
// first have to know what that currency is and its decimal precision
|
|
|
|
const {
|
|
|
|
currency: fiatCurrency,
|
|
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
|
|
} = useUserPreferencedCurrency(SECONDARY);
|
|
|
|
|
|
|
|
// We need the gas estimates from the GasFeeController in the background.
|
|
|
|
// Calling this hooks initiates polling for new gas estimates and returns the
|
|
|
|
// current estimate.
|
|
|
|
const {
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
isGasEstimatesLoading,
|
|
|
|
estimatedGasFeeTimeBounds,
|
|
|
|
} = useGasFeeEstimates();
|
|
|
|
|
2021-07-27 17:12:56 +02:00
|
|
|
// This hook keeps track of a few pieces of transitional state. It is
|
|
|
|
// transitional because it is only used to modify a transaction in the
|
|
|
|
// metamask (background) state tree.
|
|
|
|
const [maxFeePerGas, setMaxFeePerGas] = useState(
|
|
|
|
transaction?.txParams?.maxFeePerGas
|
|
|
|
? Number(hexWEIToDecGWEI(transaction.txParams.maxFeePerGas))
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
const [maxPriorityFeePerGas, setMaxPriorityFeePerGas] = useState(
|
|
|
|
transaction?.txParams?.maxPriorityFeePerGas
|
|
|
|
? Number(hexWEIToDecGWEI(transaction.txParams.maxPriorityFeePerGas))
|
|
|
|
: null,
|
|
|
|
);
|
2021-08-03 04:09:21 +02:00
|
|
|
const [gasPriceHasBeenManuallySet, setGasPriceHasBeenManuallySet] = useState(
|
|
|
|
false,
|
|
|
|
);
|
2021-07-27 17:12:56 +02:00
|
|
|
const [gasPrice, setGasPrice] = useState(
|
|
|
|
transaction?.txParams?.gasPrice
|
|
|
|
? Number(hexWEIToDecGWEI(transaction.txParams.gasPrice))
|
|
|
|
: null,
|
|
|
|
);
|
|
|
|
const [gasLimit, setGasLimit] = useState(
|
2021-08-02 22:28:30 +02:00
|
|
|
Number(hexToDecimal(transaction?.txParams?.gas ?? minimumGasLimit)),
|
2021-07-27 17:12:56 +02:00
|
|
|
);
|
2021-08-02 22:28:30 +02:00
|
|
|
|
2021-07-27 17:12:56 +02:00
|
|
|
const [estimateToUse, setInternalEstimateToUse] = useState(
|
|
|
|
transaction
|
|
|
|
? getMatchingEstimateFromGasFees(
|
|
|
|
gasFeeEstimates,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gasPrice,
|
2021-08-03 00:52:18 +02:00
|
|
|
networkAndAccountSupports1559,
|
2021-07-27 17:12:56 +02:00
|
|
|
)
|
|
|
|
: defaultEstimateToUse,
|
|
|
|
);
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
// We specify whether to use the estimate value by checking if the state
|
|
|
|
// value has been set. The state value is only set by user input and is wiped
|
|
|
|
// when the user selects an estimate. Default here is '0' to avoid bignumber
|
|
|
|
// errors in later calculations for nullish values.
|
|
|
|
const maxFeePerGasToUse =
|
|
|
|
maxFeePerGas ??
|
|
|
|
getGasFeeEstimate(
|
|
|
|
'suggestedMaxFeePerGas',
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimateToUse,
|
|
|
|
);
|
|
|
|
|
|
|
|
const maxPriorityFeePerGasToUse =
|
|
|
|
maxPriorityFeePerGas ??
|
|
|
|
getGasFeeEstimate(
|
|
|
|
'suggestedMaxPriorityFeePerGas',
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimateToUse,
|
|
|
|
);
|
|
|
|
|
2021-08-03 04:09:21 +02:00
|
|
|
const [initialGasPriceEstimates] = useState(gasFeeEstimates);
|
|
|
|
const gasPriceEstimatesHaveNotChanged = isEqual(
|
|
|
|
initialGasPriceEstimates,
|
|
|
|
gasFeeEstimates,
|
|
|
|
);
|
2021-07-09 20:26:54 +02:00
|
|
|
const gasPriceToUse =
|
2021-08-03 04:09:21 +02:00
|
|
|
gasPrice !== null &&
|
|
|
|
(gasPriceHasBeenManuallySet || gasPriceEstimatesHaveNotChanged)
|
|
|
|
? gasPrice
|
|
|
|
: getGasPriceEstimate(
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimateToUse || defaultEstimateToUse,
|
|
|
|
);
|
2021-07-09 20:26:54 +02:00
|
|
|
|
|
|
|
// We have two helper methods that take an object that can have either
|
|
|
|
// gasPrice OR the EIP-1559 fields on it, plus gasLimit. This object is
|
|
|
|
// conditionally set to the appropriate fields to compute the minimum
|
|
|
|
// and maximum cost of a transaction given the current estimates or selected
|
|
|
|
// gas fees.
|
2021-07-30 13:35:30 +02:00
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
const gasSettings = {
|
|
|
|
gasLimit: decimalToHex(gasLimit),
|
|
|
|
};
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-07-31 02:45:18 +02:00
|
|
|
gasSettings.maxFeePerGas = maxFeePerGasToUse
|
|
|
|
? decGWEIToHexWEI(maxFeePerGasToUse)
|
|
|
|
: decGWEIToHexWEI(gasPriceToUse || '0');
|
|
|
|
gasSettings.maxPriorityFeePerGas = maxPriorityFeePerGasToUse
|
|
|
|
? decGWEIToHexWEI(maxPriorityFeePerGasToUse)
|
|
|
|
: gasSettings.maxFeePerGas;
|
2021-07-09 20:26:54 +02:00
|
|
|
gasSettings.baseFeePerGas = decGWEIToHexWEI(
|
|
|
|
gasFeeEstimates.estimatedBaseFee ?? '0',
|
|
|
|
);
|
|
|
|
} else if (gasEstimateType === GAS_ESTIMATE_TYPES.NONE) {
|
|
|
|
gasSettings.gasPrice = '0x0';
|
|
|
|
} else {
|
|
|
|
gasSettings.gasPrice = decGWEIToHexWEI(gasPriceToUse);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The maximum amount this transaction will cost
|
|
|
|
const maximumCostInHexWei = getMaximumGasTotalInHexWei(gasSettings);
|
2021-07-30 13:35:30 +02:00
|
|
|
|
|
|
|
// If in swaps, we want to calculate the minimum gas fee differently than the max
|
|
|
|
const minGasSettings = {};
|
|
|
|
if (editGasMode === EDIT_GAS_MODES.SWAPS) {
|
|
|
|
minGasSettings.gasLimit = decimalToHex(minimumGasLimit);
|
|
|
|
}
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
// The minimum amount this transaction will cost's
|
2021-07-30 13:35:30 +02:00
|
|
|
const minimumCostInHexWei = getMinimumGasTotalInHexWei({
|
|
|
|
...gasSettings,
|
|
|
|
...minGasSettings,
|
|
|
|
});
|
2021-07-09 20:26:54 +02:00
|
|
|
|
|
|
|
// We need to display the estimated fiat currency impact of the
|
|
|
|
// maxPriorityFeePerGas field to the user. This hook calculates that amount.
|
|
|
|
const [, { value: maxPriorityFeePerGasFiat }] = useCurrencyDisplay(
|
|
|
|
addHexPrefix(
|
|
|
|
multiplyCurrencies(maxPriorityFeePerGasToUse, gasLimit, {
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
fromDenomination: 'GWEI',
|
|
|
|
toDenomination: 'WEI',
|
|
|
|
multiplicandBase: 10,
|
|
|
|
multiplierBase: 10,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
{
|
|
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
|
|
currency: fiatCurrency,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// We need to display thee estimated fiat currency impact of the maxFeePerGas
|
|
|
|
// field to the user. This hook calculates that amount. This also works for
|
|
|
|
// the gasPrice amount because in legacy transactions cost is always gasPrice
|
|
|
|
// * gasLimit.
|
|
|
|
const [, { value: maxFeePerGasFiat }] = useCurrencyDisplay(
|
|
|
|
maximumCostInHexWei,
|
|
|
|
{
|
|
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
|
|
currency: fiatCurrency,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
// We need to display the total amount of native currency will be expended
|
|
|
|
// given the selected gas fees.
|
|
|
|
const [estimatedMaximumNative] = useCurrencyDisplay(maximumCostInHexWei, {
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
|
|
|
currency: primaryCurrency,
|
|
|
|
});
|
|
|
|
|
2021-08-03 19:51:02 +02:00
|
|
|
const [estimatedMinimumNative] = useCurrencyDisplay(minimumCostInHexWei, {
|
|
|
|
numberOfDecimals: primaryNumberOfDecimals,
|
|
|
|
currency: primaryCurrency,
|
|
|
|
});
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
// We also need to display our closest estimate of the low end of estimation
|
|
|
|
// in fiat.
|
|
|
|
const [, { value: estimatedMinimumFiat }] = useCurrencyDisplay(
|
|
|
|
minimumCostInHexWei,
|
|
|
|
{
|
|
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
|
|
currency: fiatCurrency,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-08-02 23:49:07 +02:00
|
|
|
let estimatesUnavailableWarning = null;
|
|
|
|
|
2021-07-20 21:34:32 +02:00
|
|
|
// Separating errors from warnings so we can know which value problems
|
|
|
|
// are blocking or simply useful information for the users
|
|
|
|
const gasErrors = {};
|
|
|
|
const gasWarnings = {};
|
|
|
|
|
2021-07-30 13:35:30 +02:00
|
|
|
const gasLimitTooLow = conversionLessThan(
|
|
|
|
{ value: gasLimit, fromNumericBase: 'dec' },
|
|
|
|
{ value: minimumGasLimit || GAS_LIMITS.SIMPLE, fromNumericBase: 'hex' },
|
|
|
|
);
|
|
|
|
|
|
|
|
if (gasLimitTooLow) {
|
2021-07-20 21:34:32 +02:00
|
|
|
gasErrors.gasLimit = GAS_FORM_ERRORS.GAS_LIMIT_OUT_OF_BOUNDS;
|
|
|
|
}
|
2021-07-09 20:26:54 +02:00
|
|
|
|
|
|
|
switch (gasEstimateType) {
|
|
|
|
case GAS_ESTIMATE_TYPES.FEE_MARKET:
|
2021-07-20 21:34:32 +02:00
|
|
|
if (maxPriorityFeePerGasToUse < 1) {
|
|
|
|
gasErrors.maxPriorityFee = GAS_FORM_ERRORS.MAX_PRIORITY_FEE_ZERO;
|
|
|
|
} else if (
|
2021-07-09 20:26:54 +02:00
|
|
|
!isGasEstimatesLoading &&
|
|
|
|
maxPriorityFeePerGasToUse <
|
2021-07-20 21:34:32 +02:00
|
|
|
gasFeeEstimates?.low?.suggestedMaxPriorityFeePerGas
|
|
|
|
) {
|
|
|
|
gasErrors.maxPriorityFee = GAS_FORM_ERRORS.MAX_PRIORITY_FEE_TOO_LOW;
|
2021-08-02 16:07:29 +02:00
|
|
|
} else if (maxPriorityFeePerGasToUse >= maxFeePerGasToUse) {
|
|
|
|
gasErrors.maxFee = GAS_FORM_ERRORS.MAX_FEE_IMBALANCE;
|
2021-07-20 21:34:32 +02:00
|
|
|
} else if (
|
|
|
|
gasFeeEstimates?.high &&
|
|
|
|
maxPriorityFeePerGasToUse >
|
|
|
|
gasFeeEstimates.high.suggestedMaxPriorityFeePerGas *
|
|
|
|
HIGH_FEE_WARNING_MULTIPLIER
|
|
|
|
) {
|
|
|
|
gasWarnings.maxPriorityFee =
|
|
|
|
GAS_FORM_ERRORS.MAX_PRIORITY_FEE_HIGH_WARNING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
2021-07-09 20:26:54 +02:00
|
|
|
!isGasEstimatesLoading &&
|
2021-07-20 21:34:32 +02:00
|
|
|
maxFeePerGasToUse < gasFeeEstimates?.low?.suggestedMaxFeePerGas
|
|
|
|
) {
|
|
|
|
gasErrors.maxFee = GAS_FORM_ERRORS.MAX_FEE_TOO_LOW;
|
|
|
|
} else if (
|
|
|
|
gasFeeEstimates?.high &&
|
|
|
|
maxFeePerGasToUse >
|
|
|
|
gasFeeEstimates.high.suggestedMaxFeePerGas *
|
|
|
|
HIGH_FEE_WARNING_MULTIPLIER
|
|
|
|
) {
|
|
|
|
gasWarnings.maxFee = GAS_FORM_ERRORS.MAX_FEE_HIGH_WARNING;
|
|
|
|
}
|
2021-07-09 20:26:54 +02:00
|
|
|
break;
|
2021-08-02 23:49:07 +02:00
|
|
|
case GAS_ESTIMATE_TYPES.LEGACY:
|
|
|
|
case GAS_ESTIMATE_TYPES.ETH_GASPRICE:
|
|
|
|
case GAS_ESTIMATE_TYPES.NONE:
|
2021-08-03 00:52:18 +02:00
|
|
|
if (networkAndAccountSupports1559) {
|
2021-08-02 23:49:07 +02:00
|
|
|
estimatesUnavailableWarning = true;
|
|
|
|
}
|
2021-08-03 17:54:49 +02:00
|
|
|
if (gasPriceToUse <= 0) {
|
|
|
|
gasErrors.gasPrice = GAS_FORM_ERRORS.GAS_PRICE_TOO_LOW;
|
|
|
|
}
|
2021-08-02 23:49:07 +02:00
|
|
|
break;
|
2021-07-09 20:26:54 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-07-20 21:34:32 +02:00
|
|
|
// Determine if we have any errors which should block submission
|
|
|
|
const hasBlockingGasErrors = Boolean(Object.keys(gasErrors).length);
|
|
|
|
|
|
|
|
// Now that we've determined errors that block submission, we can pool the warnings
|
|
|
|
// and errors into one object for easier use within the UI. This object should have
|
|
|
|
// no effect on whether or not the user can submit the form
|
|
|
|
const errorsAndWarnings = {
|
|
|
|
...gasWarnings,
|
2021-08-02 16:07:29 +02:00
|
|
|
...gasErrors,
|
2021-07-20 21:34:32 +02:00
|
|
|
};
|
2021-07-09 20:26:54 +02:00
|
|
|
|
2021-07-31 00:46:31 +02:00
|
|
|
const minimumTxCostInHexWei = addHexes(
|
|
|
|
minimumCostInHexWei,
|
2021-07-31 15:01:29 +02:00
|
|
|
transaction?.txParams?.value || '0x0',
|
2021-07-31 00:46:31 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const balanceError = conversionGreaterThan(
|
|
|
|
{ value: minimumTxCostInHexWei, fromNumericBase: 'hex' },
|
|
|
|
{ value: ethBalance, fromNumericBase: 'hex' },
|
|
|
|
);
|
|
|
|
|
2021-08-02 22:28:30 +02:00
|
|
|
// When a user selects an estimate level, it will wipe out what they have
|
|
|
|
// previously put in the inputs. This returns the inputs to the estimated
|
|
|
|
// values at the level specified.
|
|
|
|
const setEstimateToUse = useCallback(
|
|
|
|
(estimateLevel) => {
|
|
|
|
setInternalEstimateToUse(estimateLevel);
|
|
|
|
if (gasErrors.gasLimit === GAS_FORM_ERRORS.GAS_LIMIT_OUT_OF_BOUNDS) {
|
|
|
|
const transactionGasLimit = hexToDecimal(transaction?.txParams?.gas);
|
|
|
|
const minimumGasLimitDec = hexToDecimal(minimumGasLimit);
|
|
|
|
setGasLimit(
|
|
|
|
transactionGasLimit > minimumGasLimitDec
|
|
|
|
? transactionGasLimit
|
|
|
|
: minimumGasLimitDec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
setMaxFeePerGas(null);
|
|
|
|
setMaxPriorityFeePerGas(null);
|
|
|
|
setGasPrice(null);
|
2021-08-03 04:09:21 +02:00
|
|
|
setGasPriceHasBeenManuallySet(false);
|
2021-08-02 22:28:30 +02:00
|
|
|
},
|
|
|
|
[minimumGasLimit, gasErrors.gasLimit, transaction],
|
|
|
|
);
|
|
|
|
|
2021-07-09 20:26:54 +02:00
|
|
|
return {
|
|
|
|
maxFeePerGas: maxFeePerGasToUse,
|
|
|
|
maxFeePerGasFiat: showFiat ? maxFeePerGasFiat : '',
|
|
|
|
setMaxFeePerGas,
|
|
|
|
maxPriorityFeePerGas: maxPriorityFeePerGasToUse,
|
|
|
|
maxPriorityFeePerGasFiat: showFiat ? maxPriorityFeePerGasFiat : '',
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
gasPrice: gasPriceToUse,
|
|
|
|
setGasPrice,
|
|
|
|
gasLimit,
|
|
|
|
setGasLimit,
|
|
|
|
estimateToUse,
|
|
|
|
setEstimateToUse,
|
|
|
|
estimatedMinimumFiat: showFiat ? estimatedMinimumFiat : '',
|
|
|
|
estimatedMaximumFiat: showFiat ? maxFeePerGasFiat : '',
|
|
|
|
estimatedMaximumNative,
|
2021-08-03 19:51:02 +02:00
|
|
|
estimatedMinimumNative,
|
2021-07-09 20:26:54 +02:00
|
|
|
isGasEstimatesLoading,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimatedGasFeeTimeBounds,
|
2021-07-20 21:34:32 +02:00
|
|
|
gasErrors: errorsAndWarnings,
|
|
|
|
hasGasErrors: hasBlockingGasErrors,
|
2021-07-22 18:32:59 +02:00
|
|
|
onManualChange: () => {
|
|
|
|
setEstimateToUse(null);
|
|
|
|
// Restore existing values
|
|
|
|
setGasPrice(gasPriceToUse);
|
|
|
|
setGasLimit(gasLimit);
|
|
|
|
setMaxFeePerGas(maxFeePerGasToUse);
|
|
|
|
setMaxPriorityFeePerGas(maxPriorityFeePerGasToUse);
|
2021-08-03 04:09:21 +02:00
|
|
|
setGasPriceHasBeenManuallySet(true);
|
2021-07-22 18:32:59 +02:00
|
|
|
},
|
2021-07-31 00:46:31 +02:00
|
|
|
balanceError,
|
2021-08-02 23:49:07 +02:00
|
|
|
estimatesUnavailableWarning,
|
2021-07-09 20:26:54 +02:00
|
|
|
};
|
|
|
|
}
|