2021-10-22 19:14:28 +02:00
|
|
|
import { useSelector } from 'react-redux';
|
2021-12-01 01:31:21 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-10-22 19:14:28 +02:00
|
|
|
|
2022-12-08 19:37:06 +01:00
|
|
|
import { checkNetworkAndAccountSupports1559 } from '../../selectors';
|
2021-10-22 19:14:28 +02:00
|
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
|
|
|
|
2022-09-16 21:05:21 +02:00
|
|
|
import { 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 getMaxPriorityFeePerGasFromTransaction = (
|
|
|
|
transaction,
|
|
|
|
gasFeeEstimates,
|
|
|
|
) => {
|
|
|
|
if (gasFeeEstimates?.[transaction?.userFeeLevel]) {
|
|
|
|
return gasFeeEstimates[transaction.userFeeLevel]
|
|
|
|
.suggestedMaxPriorityFeePerGas;
|
|
|
|
}
|
2021-10-22 19:14:28 +02:00
|
|
|
const { maxPriorityFeePerGas, maxFeePerGas, gasPrice } =
|
|
|
|
transaction?.txParams || {};
|
|
|
|
return Number(
|
|
|
|
hexWEIToDecGWEI(maxPriorityFeePerGas || maxFeePerGas || gasPrice),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} MaxPriorityFeePerGasInputReturnType
|
2021-10-22 19:14:28 +02:00
|
|
|
* @property {DecGweiString} [maxPriorityFeePerGas] - the maxPriorityFeePerGas
|
|
|
|
* input value.
|
|
|
|
* @property {(DecGweiString) => void} setMaxPriorityFeePerGas - state setter
|
|
|
|
* method to update the maxPriorityFeePerGas.
|
|
|
|
*/
|
2022-01-07 16:57:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param options
|
|
|
|
* @param options.estimateToUse
|
|
|
|
* @param options.gasEstimateType
|
|
|
|
* @param options.gasFeeEstimates
|
|
|
|
* @param options.transaction
|
|
|
|
* @returns {MaxPriorityFeePerGasInputReturnType}
|
|
|
|
*/
|
2021-10-22 19:14:28 +02:00
|
|
|
export function useMaxPriorityFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
transaction,
|
|
|
|
}) {
|
|
|
|
const supportsEIP1559 =
|
|
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
const initialMaxPriorityFeePerGas = supportsEIP1559
|
2022-03-03 01:22:46 +01:00
|
|
|
? getMaxPriorityFeePerGasFromTransaction(transaction, gasFeeEstimates)
|
2021-10-22 19:14:28 +02:00
|
|
|
: 0;
|
|
|
|
|
|
|
|
const [maxPriorityFeePerGas, setMaxPriorityFeePerGas] = useState(() => {
|
2022-01-06 23:56:51 +01:00
|
|
|
if (initialMaxPriorityFeePerGas && feeParamsAreCustom(transaction)) {
|
2021-12-12 00:26:28 +01:00
|
|
|
return initialMaxPriorityFeePerGas;
|
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(() => {
|
2022-12-08 19:37:06 +01:00
|
|
|
if (supportsEIP1559 && initialMaxPriorityFeePerGas) {
|
2021-12-12 00:26:28 +01:00
|
|
|
setMaxPriorityFeePerGas(initialMaxPriorityFeePerGas);
|
2021-12-01 01:31:21 +01:00
|
|
|
}
|
2022-12-08 19:37:06 +01:00
|
|
|
}, [initialMaxPriorityFeePerGas, setMaxPriorityFeePerGas, supportsEIP1559]);
|
2021-12-01 01:31:21 +01:00
|
|
|
|
2021-10-22 19:14:28 +02:00
|
|
|
const maxPriorityFeePerGasToUse =
|
|
|
|
maxPriorityFeePerGas ??
|
|
|
|
getGasFeeEstimate(
|
|
|
|
'suggestedMaxPriorityFeePerGas',
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
|
|
|
estimateToUse,
|
2021-12-12 00:26:28 +01:00
|
|
|
initialMaxPriorityFeePerGas || 0,
|
2021-10-22 19:14:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
maxPriorityFeePerGas: maxPriorityFeePerGasToUse,
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
};
|
|
|
|
}
|