2021-11-18 20:08:29 +01:00
|
|
|
import { useCallback, useEffect, useState } from 'react';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
2021-11-06 01:59:23 +01:00
|
|
|
import {
|
|
|
|
CUSTOM_GAS_ESTIMATE,
|
2021-11-12 04:22:54 +01:00
|
|
|
GAS_RECOMMENDATIONS,
|
|
|
|
EDIT_GAS_MODES,
|
2021-11-29 18:40:48 +01:00
|
|
|
PRIORITY_LEVELS,
|
2021-11-06 01:59:23 +01:00
|
|
|
} from '../../../shared/constants/gas';
|
2021-11-10 15:24:17 +01:00
|
|
|
import { GAS_FORM_ERRORS } from '../../helpers/constants/gas';
|
2021-11-12 20:09:59 +01:00
|
|
|
import {
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
getAdvancedInlineGasShown,
|
2022-01-11 20:17:56 +01:00
|
|
|
getEIP1559V2Enabled,
|
2021-11-12 20:09:59 +01:00
|
|
|
} from '../../selectors';
|
|
|
|
import { hexToDecimal } from '../../helpers/utils/conversions.util';
|
|
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useGasFeeEstimates } from '../useGasFeeEstimates';
|
|
|
|
|
|
|
|
import { useGasFeeErrors } from './useGasFeeErrors';
|
|
|
|
import { useGasPriceInput } from './useGasPriceInput';
|
|
|
|
import { useMaxFeePerGasInput } from './useMaxFeePerGasInput';
|
|
|
|
import { useMaxPriorityFeePerGasInput } from './useMaxPriorityFeePerGasInput';
|
|
|
|
import { useGasEstimates } from './useGasEstimates';
|
2021-11-18 18:54:58 +01:00
|
|
|
import { useTransactionFunctions } from './useTransactionFunctions';
|
2021-10-22 19:14:28 +02:00
|
|
|
|
2021-12-06 19:47:26 +01:00
|
|
|
/**
|
|
|
|
* In EIP_1559_V2 implementation as used by useGasfeeInputContext() the use of this hook is evolved.
|
|
|
|
* It is no longer used to keep transient state of advance gas fee inputs.
|
|
|
|
* Transient state of inputs is maintained locally in /ui/components/app/advance-gas-fee-popover component.
|
|
|
|
*
|
|
|
|
* This hook is used now as source of shared data about transaction, it shares details of gas fee in transaction,
|
|
|
|
* estimate used, is EIP-1559 supported and other details. It also have methods to update transaction.
|
|
|
|
*
|
|
|
|
* Transaction is used as single source of truth and as transaction is updated the fields shared by hook are
|
|
|
|
* also updated using useEffect hook.
|
|
|
|
*
|
|
|
|
* It will be useful to plan a task to create a new hook of this shared information from this hook.
|
|
|
|
* Methods like setEstimateToUse, onManualChange are deprecated in context of EIP_1559_V2 implementation.
|
|
|
|
*/
|
|
|
|
|
2021-10-22 19:14:28 +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.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
|
|
|
* @param {EstimateLevel} [defaultEstimateToUse] - which estimate
|
2021-10-22 19:14:28 +02:00
|
|
|
* level to default the 'estimateToUse' state variable to.
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {object} [transaction]
|
|
|
|
* @param {string} [minimumGasLimit]
|
|
|
|
* @param {EDIT_GAS_MODES[keyof EDIT_GAS_MODES]} editGasMode
|
2021-10-22 19:14:28 +02:00
|
|
|
* @returns {GasFeeInputReturnType & import(
|
|
|
|
* './useGasFeeEstimates'
|
2022-01-07 16:57:33 +01:00
|
|
|
* ).GasEstimates} gas fee input state and the GasFeeEstimates object
|
2021-10-22 19:14:28 +02:00
|
|
|
*/
|
|
|
|
export function useGasFeeInputs(
|
2021-11-06 01:59:23 +01:00
|
|
|
defaultEstimateToUse = GAS_RECOMMENDATIONS.MEDIUM,
|
2021-10-22 19:14:28 +02:00
|
|
|
transaction,
|
|
|
|
minimumGasLimit = '0x5208',
|
2021-11-10 03:07:10 +01:00
|
|
|
editGasMode = EDIT_GAS_MODES.MODIFY_IN_PLACE,
|
2021-10-22 19:14:28 +02:00
|
|
|
) {
|
2022-01-11 20:17:56 +01:00
|
|
|
const eip1559V2Enabled = useSelector(getEIP1559V2Enabled);
|
2021-11-29 17:08:24 +01:00
|
|
|
|
2021-11-12 20:09:59 +01:00
|
|
|
const supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
|
2022-01-11 20:17:56 +01:00
|
|
|
const supportsEIP1559V2 = supportsEIP1559 && eip1559V2Enabled;
|
2021-12-03 16:59:48 +01:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
// 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,
|
2022-01-07 20:18:02 +01:00
|
|
|
isNetworkBusy,
|
2021-10-22 19:14:28 +02:00
|
|
|
} = useGasFeeEstimates();
|
|
|
|
|
|
|
|
const userPrefersAdvancedGas = useSelector(getAdvancedInlineGasShown);
|
|
|
|
|
|
|
|
const [estimateToUse, setInternalEstimateToUse] = useState(() => {
|
|
|
|
if (
|
|
|
|
userPrefersAdvancedGas &&
|
|
|
|
transaction?.txParams?.maxPriorityFeePerGas &&
|
|
|
|
transaction?.txParams?.maxFeePerGas
|
2022-01-06 23:56:51 +01:00
|
|
|
) {
|
2021-10-22 19:14:28 +02:00
|
|
|
return null;
|
2022-01-06 23:56:51 +01:00
|
|
|
}
|
|
|
|
if (transaction) {
|
|
|
|
return transaction?.userFeeLevel || null;
|
|
|
|
}
|
2021-10-22 19:14:28 +02:00
|
|
|
return defaultEstimateToUse;
|
|
|
|
});
|
|
|
|
|
2021-11-18 18:54:58 +01:00
|
|
|
const [estimateUsed, setEstimateUsed] = useState(() => {
|
2021-11-29 18:40:48 +01:00
|
|
|
if (estimateToUse) {
|
|
|
|
return estimateToUse;
|
2021-11-18 18:54:58 +01:00
|
|
|
}
|
2021-11-29 18:40:48 +01:00
|
|
|
return PRIORITY_LEVELS.CUSTOM;
|
2021-11-18 18:54:58 +01:00
|
|
|
});
|
2021-11-12 20:09:59 +01:00
|
|
|
|
2021-12-03 16:59:48 +01:00
|
|
|
const [gasLimit, setGasLimit] = useState(() =>
|
|
|
|
Number(hexToDecimal(transaction?.txParams?.gas ?? '0x0')),
|
|
|
|
);
|
|
|
|
|
2022-01-21 16:58:59 +01:00
|
|
|
const properGasLimit = Number(hexToDecimal(transaction?.originalGasEstimate));
|
|
|
|
|
|
|
|
const [userEditedGasLimit, setUserEditedGasLimit] = useState(() =>
|
|
|
|
Boolean(transaction?.userEditedGasLimit),
|
|
|
|
);
|
|
|
|
|
2021-11-18 20:08:29 +01:00
|
|
|
/**
|
|
|
|
* In EIP-1559 V2 designs change to gas estimate is always updated to transaction
|
|
|
|
* Thus callback setEstimateToUse can be deprecate in favour of this useEffect
|
|
|
|
* so that transaction is source of truth whenever possible.
|
|
|
|
*/
|
|
|
|
useEffect(() => {
|
2021-12-03 16:59:48 +01:00
|
|
|
if (supportsEIP1559V2) {
|
|
|
|
if (transaction?.userFeeLevel) {
|
|
|
|
setEstimateUsed(transaction?.userFeeLevel);
|
|
|
|
setInternalEstimateToUse(transaction?.userFeeLevel);
|
|
|
|
}
|
|
|
|
setGasLimit(Number(hexToDecimal(transaction?.txParams?.gas ?? '0x0')));
|
2021-11-18 20:08:29 +01:00
|
|
|
}
|
2021-12-01 01:31:21 +01:00
|
|
|
}, [
|
|
|
|
setEstimateUsed,
|
2021-12-03 16:59:48 +01:00
|
|
|
setGasLimit,
|
2021-12-01 01:31:21 +01:00
|
|
|
setInternalEstimateToUse,
|
2021-12-03 16:59:48 +01:00
|
|
|
supportsEIP1559V2,
|
2021-12-01 01:31:21 +01:00
|
|
|
transaction,
|
|
|
|
]);
|
2021-11-18 20:08:29 +01:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
const {
|
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
setGasPriceHasBeenManuallySet,
|
|
|
|
} = useGasPriceInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
maxFeePerGas,
|
|
|
|
maxFeePerGasFiat,
|
|
|
|
setMaxFeePerGas,
|
|
|
|
} = useMaxFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
2021-12-12 00:26:28 +01:00
|
|
|
supportsEIP1559V2,
|
2021-10-22 19:14:28 +02:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
maxPriorityFeePerGasFiat,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
} = useMaxPriorityFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
2021-12-12 00:26:28 +01:00
|
|
|
supportsEIP1559V2,
|
2021-10-22 19:14:28 +02:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
estimatedBaseFee,
|
|
|
|
estimatedMaximumFiat,
|
|
|
|
estimatedMinimumFiat,
|
|
|
|
estimatedMaximumNative,
|
|
|
|
estimatedMinimumNative,
|
2022-01-06 03:47:26 +01:00
|
|
|
maximumCostInHexWei,
|
2021-10-22 19:14:28 +02:00
|
|
|
minimumCostInHexWei,
|
|
|
|
} = useGasEstimates({
|
|
|
|
editGasMode,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
minimumGasLimit,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
balanceError,
|
|
|
|
estimatesUnavailableWarning,
|
|
|
|
gasErrors,
|
|
|
|
gasWarnings,
|
|
|
|
hasGasErrors,
|
2021-11-24 18:02:53 +01:00
|
|
|
hasSimulationError,
|
2021-10-22 19:14:28 +02:00
|
|
|
} = useGasFeeErrors({
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
isGasEstimatesLoading,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
maxFeePerGas,
|
|
|
|
minimumCostInHexWei,
|
|
|
|
minimumGasLimit,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const handleGasLimitOutOfBoundError = useCallback(() => {
|
|
|
|
if (gasErrors.gasLimit === GAS_FORM_ERRORS.GAS_LIMIT_OUT_OF_BOUNDS) {
|
|
|
|
const transactionGasLimitDec = hexToDecimal(transaction?.txParams?.gas);
|
|
|
|
const minimumGasLimitDec = hexToDecimal(minimumGasLimit);
|
|
|
|
setGasLimit(
|
|
|
|
transactionGasLimitDec > minimumGasLimitDec
|
|
|
|
? transactionGasLimitDec
|
|
|
|
: minimumGasLimitDec,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [minimumGasLimit, gasErrors.gasLimit, transaction]);
|
|
|
|
|
2021-12-01 01:31:21 +01:00
|
|
|
const {
|
2022-01-06 03:47:26 +01:00
|
|
|
cancelTransaction,
|
|
|
|
speedUpTransaction,
|
2021-12-01 01:31:21 +01:00
|
|
|
updateTransaction,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingDAPPSuggestedValues,
|
|
|
|
updateTransactionUsingEstimate,
|
2021-12-01 01:31:21 +01:00
|
|
|
} = useTransactionFunctions({
|
2021-11-18 18:54:58 +01:00
|
|
|
defaultEstimateToUse,
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode,
|
2021-12-01 01:31:21 +01:00
|
|
|
gasFeeEstimates,
|
2021-11-18 18:54:58 +01:00
|
|
|
gasLimit,
|
2022-01-06 03:47:26 +01:00
|
|
|
maxPriorityFeePerGas,
|
|
|
|
minimumGasLimit,
|
2021-11-18 18:54:58 +01:00
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
2021-10-22 19:14:28 +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);
|
|
|
|
handleGasLimitOutOfBoundError();
|
|
|
|
setMaxFeePerGas(null);
|
|
|
|
setMaxPriorityFeePerGas(null);
|
|
|
|
setGasPrice(null);
|
|
|
|
setGasPriceHasBeenManuallySet(false);
|
2021-11-18 18:54:58 +01:00
|
|
|
setEstimateUsed(estimateLevel);
|
2021-10-22 19:14:28 +02:00
|
|
|
},
|
|
|
|
[
|
|
|
|
setInternalEstimateToUse,
|
|
|
|
handleGasLimitOutOfBoundError,
|
|
|
|
setMaxFeePerGas,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
setGasPrice,
|
|
|
|
setGasPriceHasBeenManuallySet,
|
2021-11-18 18:54:58 +01:00
|
|
|
setEstimateUsed,
|
2021-10-22 19:14:28 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
const onManualChange = useCallback(() => {
|
2021-11-06 01:59:23 +01:00
|
|
|
setInternalEstimateToUse(CUSTOM_GAS_ESTIMATE);
|
2021-10-22 19:14:28 +02:00
|
|
|
handleGasLimitOutOfBoundError();
|
|
|
|
// Restore existing values
|
|
|
|
setGasPrice(gasPrice);
|
|
|
|
setGasLimit(gasLimit);
|
2022-01-21 16:58:59 +01:00
|
|
|
setUserEditedGasLimit(true);
|
2021-10-22 19:14:28 +02:00
|
|
|
setMaxFeePerGas(maxFeePerGas);
|
|
|
|
setMaxPriorityFeePerGas(maxPriorityFeePerGas);
|
|
|
|
setGasPriceHasBeenManuallySet(true);
|
2021-11-18 18:54:58 +01:00
|
|
|
setEstimateUsed('custom');
|
2021-10-22 19:14:28 +02:00
|
|
|
}, [
|
|
|
|
setInternalEstimateToUse,
|
|
|
|
handleGasLimitOutOfBoundError,
|
|
|
|
setGasPrice,
|
|
|
|
gasPrice,
|
|
|
|
setGasLimit,
|
|
|
|
gasLimit,
|
2022-01-21 16:58:59 +01:00
|
|
|
setUserEditedGasLimit,
|
2021-10-22 19:14:28 +02:00
|
|
|
setMaxFeePerGas,
|
|
|
|
maxFeePerGas,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
setGasPriceHasBeenManuallySet,
|
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
2021-11-12 20:09:59 +01:00
|
|
|
transaction,
|
2021-10-22 19:14:28 +02:00
|
|
|
maxFeePerGas,
|
|
|
|
maxFeePerGasFiat,
|
|
|
|
setMaxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
maxPriorityFeePerGasFiat,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
gasLimit,
|
|
|
|
setGasLimit,
|
2022-01-21 16:58:59 +01:00
|
|
|
properGasLimit,
|
|
|
|
userEditedGasLimit,
|
2021-12-12 00:26:28 +01:00
|
|
|
editGasMode,
|
2021-10-22 19:14:28 +02:00
|
|
|
estimateToUse,
|
|
|
|
setEstimateToUse,
|
|
|
|
estimatedMinimumFiat,
|
|
|
|
estimatedMaximumFiat,
|
|
|
|
estimatedMaximumNative,
|
|
|
|
estimatedMinimumNative,
|
|
|
|
isGasEstimatesLoading,
|
2022-01-06 03:47:26 +01:00
|
|
|
maximumCostInHexWei,
|
|
|
|
minimumCostInHexWei,
|
2021-11-18 18:54:58 +01:00
|
|
|
estimateUsed,
|
2021-10-22 19:14:28 +02:00
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimatedGasFeeTimeBounds,
|
2022-01-07 20:18:02 +01:00
|
|
|
isNetworkBusy,
|
2021-10-22 19:14:28 +02:00
|
|
|
onManualChange,
|
|
|
|
estimatedBaseFee,
|
|
|
|
// error and warnings
|
|
|
|
balanceError,
|
|
|
|
estimatesUnavailableWarning,
|
|
|
|
gasErrors,
|
|
|
|
gasWarnings,
|
|
|
|
hasGasErrors,
|
2021-11-24 18:02:53 +01:00
|
|
|
hasSimulationError,
|
2021-12-06 19:47:26 +01:00
|
|
|
minimumGasLimitDec: hexToDecimal(minimumGasLimit),
|
2021-11-12 20:09:59 +01:00
|
|
|
supportsEIP1559,
|
2021-12-03 16:59:48 +01:00
|
|
|
supportsEIP1559V2,
|
2022-01-06 03:47:26 +01:00
|
|
|
cancelTransaction,
|
|
|
|
speedUpTransaction,
|
2021-12-01 01:31:21 +01:00
|
|
|
updateTransaction,
|
2022-01-06 23:40:31 +01:00
|
|
|
updateTransactionToTenPercentIncreasedGasFee,
|
2022-01-06 03:47:26 +01:00
|
|
|
updateTransactionUsingDAPPSuggestedValues,
|
|
|
|
updateTransactionUsingEstimate,
|
2021-10-22 19:14:28 +02:00
|
|
|
};
|
|
|
|
}
|