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-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 { areDappSuggestedAndTxParamGasFeesTheSame } from '../../helpers/utils/confirm-tx.util';
|
|
|
|
import {
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
getAdvancedInlineGasShown,
|
|
|
|
} 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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
) {
|
2021-11-12 20:09:59 +01:00
|
|
|
const supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
|
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,
|
|
|
|
} = useGasFeeEstimates();
|
|
|
|
|
|
|
|
const userPrefersAdvancedGas = useSelector(getAdvancedInlineGasShown);
|
|
|
|
|
|
|
|
const [estimateToUse, setInternalEstimateToUse] = useState(() => {
|
|
|
|
if (
|
|
|
|
userPrefersAdvancedGas &&
|
|
|
|
transaction?.txParams?.maxPriorityFeePerGas &&
|
|
|
|
transaction?.txParams?.maxFeePerGas
|
|
|
|
)
|
|
|
|
return null;
|
|
|
|
if (transaction) return transaction?.userFeeLevel || null;
|
|
|
|
return defaultEstimateToUse;
|
|
|
|
});
|
|
|
|
|
2021-11-18 18:54:58 +01:00
|
|
|
const [estimateUsed, setEstimateUsed] = useState(() => {
|
|
|
|
if (areDappSuggestedAndTxParamGasFeesTheSame(transaction)) {
|
|
|
|
return 'dappSuggested';
|
|
|
|
}
|
|
|
|
return estimateToUse;
|
|
|
|
});
|
2021-11-12 20:09:59 +01:00
|
|
|
|
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(() => {
|
|
|
|
if (areDappSuggestedAndTxParamGasFeesTheSame(transaction)) {
|
|
|
|
setEstimateUsed('dappSuggested');
|
|
|
|
} else if (transaction?.userFeeLevel) {
|
|
|
|
setEstimateUsed(transaction?.userFeeLevel);
|
|
|
|
}
|
|
|
|
}, [setEstimateUsed, transaction]);
|
|
|
|
|
2021-11-18 18:54:58 +01:00
|
|
|
const [gasLimit, setGasLimit] = useState(() =>
|
2021-10-22 19:14:28 +02:00
|
|
|
Number(hexToDecimal(transaction?.txParams?.gas ?? '0x0')),
|
|
|
|
);
|
|
|
|
|
|
|
|
const {
|
|
|
|
gasPrice,
|
|
|
|
setGasPrice,
|
|
|
|
setGasPriceHasBeenManuallySet,
|
|
|
|
} = useGasPriceInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
maxFeePerGas,
|
|
|
|
maxFeePerGasFiat,
|
|
|
|
setMaxFeePerGas,
|
|
|
|
} = useMaxFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
maxPriorityFeePerGasFiat,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
} = useMaxPriorityFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
|
|
|
transaction,
|
|
|
|
});
|
|
|
|
|
|
|
|
const {
|
|
|
|
estimatedBaseFee,
|
|
|
|
estimatedMaximumFiat,
|
|
|
|
estimatedMinimumFiat,
|
|
|
|
estimatedMaximumNative,
|
|
|
|
estimatedMinimumNative,
|
|
|
|
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-11-18 20:08:29 +01:00
|
|
|
const { updateTransactionUsingGasFeeEstimates } = useTransactionFunctions({
|
2021-11-18 18:54:58 +01:00
|
|
|
defaultEstimateToUse,
|
|
|
|
gasLimit,
|
|
|
|
gasPrice,
|
|
|
|
maxFeePerGas,
|
|
|
|
maxPriorityFeePerGas,
|
|
|
|
gasFeeEstimates,
|
|
|
|
supportsEIP1559,
|
|
|
|
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);
|
|
|
|
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,
|
|
|
|
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,
|
|
|
|
estimateToUse,
|
|
|
|
setEstimateToUse,
|
|
|
|
estimatedMinimumFiat,
|
|
|
|
estimatedMaximumFiat,
|
|
|
|
estimatedMaximumNative,
|
|
|
|
estimatedMinimumNative,
|
|
|
|
isGasEstimatesLoading,
|
2021-11-18 18:54:58 +01:00
|
|
|
estimateUsed,
|
2021-10-22 19:14:28 +02:00
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimatedGasFeeTimeBounds,
|
|
|
|
onManualChange,
|
|
|
|
estimatedBaseFee,
|
|
|
|
// error and warnings
|
|
|
|
balanceError,
|
|
|
|
estimatesUnavailableWarning,
|
|
|
|
gasErrors,
|
|
|
|
gasWarnings,
|
|
|
|
hasGasErrors,
|
2021-11-24 18:02:53 +01:00
|
|
|
hasSimulationError,
|
2021-11-12 20:09:59 +01:00
|
|
|
supportsEIP1559,
|
2021-11-18 20:08:29 +01:00
|
|
|
updateTransactionUsingGasFeeEstimates,
|
2021-10-22 19:14:28 +02:00
|
|
|
};
|
|
|
|
}
|