2021-12-01 01:31:21 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
|
|
|
|
|
|
|
import { GAS_ESTIMATE_TYPES } from '../../../shared/constants/gas';
|
|
|
|
import { SECONDARY } from '../../helpers/constants/common';
|
|
|
|
import { getMaximumGasTotalInHexWei } from '../../../shared/modules/gas.utils';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { decGWEIToHexWEI } from '../../helpers/utils/conversions.util';
|
2021-10-22 19:14:28 +02:00
|
|
|
import {
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
getShouldShowFiat,
|
|
|
|
} from '../../selectors';
|
|
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
|
|
|
|
|
|
|
import { useCurrencyDisplay } from '../useCurrencyDisplay';
|
|
|
|
import { useUserPreferencedCurrency } from '../useUserPreferencedCurrency';
|
2022-09-16 21:05:21 +02:00
|
|
|
import {
|
|
|
|
decimalToHex,
|
|
|
|
hexWEIToDecGWEI,
|
|
|
|
} from '../../../shared/lib/transactions-controller-utils';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { feeParamsAreCustom, getGasFeeEstimate } from './utils';
|
|
|
|
|
2022-03-03 01:22:46 +01:00
|
|
|
const getMaxFeePerGasFromTransaction = (transaction, gasFeeEstimates) => {
|
|
|
|
if (gasFeeEstimates?.[transaction?.userFeeLevel]) {
|
|
|
|
return gasFeeEstimates[transaction.userFeeLevel].suggestedMaxFeePerGas;
|
|
|
|
}
|
2021-10-22 19:14:28 +02:00
|
|
|
const { maxFeePerGas, gasPrice } = transaction?.txParams || {};
|
|
|
|
return Number(hexWEIToDecGWEI(maxFeePerGas || gasPrice));
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} MaxFeePerGasInputReturnType
|
2021-10-22 19:14:28 +02:00
|
|
|
* @property {(DecGweiString) => void} setMaxFeePerGas - state setter method to
|
|
|
|
* update the maxFeePerGas.
|
|
|
|
* @property {string} [maxFeePerGasFiat] - the maxFeePerGas converted to the
|
|
|
|
* user's preferred currency.
|
2022-01-07 16:57:33 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param options
|
|
|
|
* @param options.supportsEIP1559V2
|
|
|
|
* @param options.estimateToUse
|
|
|
|
* @param options.gasEstimateType
|
|
|
|
* @param options.gasFeeEstimates
|
|
|
|
* @param options.gasLimit
|
|
|
|
* @param options.gasPrice
|
|
|
|
* @param options.transaction
|
|
|
|
* @returns {MaxFeePerGasInputReturnType}
|
2021-10-22 19:14:28 +02:00
|
|
|
*/
|
|
|
|
export function 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 supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
|
2022-07-31 20:26:40 +02:00
|
|
|
const { currency: fiatCurrency, numberOfDecimals: fiatNumberOfDecimals } =
|
|
|
|
useUserPreferencedCurrency(SECONDARY);
|
2021-10-22 19:14:28 +02:00
|
|
|
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
const initialMaxFeePerGas = supportsEIP1559
|
2022-03-03 01:22:46 +01:00
|
|
|
? getMaxFeePerGasFromTransaction(transaction, gasFeeEstimates)
|
2021-10-22 19:14:28 +02:00
|
|
|
: 0;
|
|
|
|
|
|
|
|
// 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(() => {
|
2022-01-06 23:56:51 +01:00
|
|
|
if (initialMaxFeePerGas && feeParamsAreCustom(transaction)) {
|
2021-12-12 00:26:28 +01:00
|
|
|
return initialMaxFeePerGas;
|
2022-01-06 23:56:51 +01:00
|
|
|
}
|
2021-10-22 19:14:28 +02:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
2021-12-01 01:31:21 +01:00
|
|
|
useEffect(() => {
|
2021-12-12 00:26:28 +01:00
|
|
|
if (supportsEIP1559V2 && initialMaxFeePerGas) {
|
|
|
|
setMaxFeePerGas(initialMaxFeePerGas);
|
2021-12-01 01:31:21 +01:00
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
}, [initialMaxFeePerGas, setMaxFeePerGas, supportsEIP1559V2]);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
let gasSettings = {
|
|
|
|
gasLimit: decimalToHex(gasLimit),
|
|
|
|
};
|
|
|
|
if (supportsEIP1559) {
|
|
|
|
gasSettings = {
|
|
|
|
...gasSettings,
|
|
|
|
maxFeePerGas: decGWEIToHexWEI(maxFeePerGas || gasPrice || '0'),
|
|
|
|
};
|
|
|
|
} else if (gasEstimateType === GAS_ESTIMATE_TYPES.NONE) {
|
|
|
|
gasSettings = {
|
|
|
|
...gasSettings,
|
|
|
|
gasPrice: '0x0',
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
gasSettings = {
|
|
|
|
...gasSettings,
|
|
|
|
gasPrice: decGWEIToHexWEI(gasPrice),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const maximumCostInHexWei = getMaximumGasTotalInHexWei(gasSettings);
|
|
|
|
|
|
|
|
// 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 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,
|
2021-12-12 00:26:28 +01:00
|
|
|
initialMaxFeePerGas || 0,
|
2021-10-22 19:14:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
maxFeePerGas: maxFeePerGasToUse,
|
|
|
|
maxFeePerGasFiat: showFiat ? maxFeePerGasFiat : '',
|
|
|
|
setMaxFeePerGas,
|
|
|
|
};
|
|
|
|
}
|