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
|
|
|
|
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
|
|
|
|
|
|
|
import { SECONDARY } from '../../helpers/constants/common';
|
|
|
|
import { hexWEIToDecGWEI } from '../../helpers/utils/conversions.util';
|
|
|
|
import {
|
|
|
|
checkNetworkAndAccountSupports1559,
|
|
|
|
getShouldShowFiat,
|
|
|
|
} from '../../selectors';
|
|
|
|
import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
|
2021-12-12 00:26:28 +01:00
|
|
|
import { multiplyCurrencies } from '../../../shared/modules/conversion.utils';
|
2021-10-22 19:14:28 +02:00
|
|
|
|
|
|
|
import { useCurrencyDisplay } from '../useCurrencyDisplay';
|
|
|
|
import { useUserPreferencedCurrency } from '../useUserPreferencedCurrency';
|
|
|
|
import { feeParamsAreCustom, getGasFeeEstimate } from './utils';
|
|
|
|
|
|
|
|
const getMaxPriorityFeePerGasFromTransaction = (transaction) => {
|
|
|
|
const { maxPriorityFeePerGas, maxFeePerGas, gasPrice } =
|
|
|
|
transaction?.txParams || {};
|
|
|
|
return Number(
|
|
|
|
hexWEIToDecGWEI(maxPriorityFeePerGas || maxFeePerGas || gasPrice),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} MaxPriorityFeePerGasInputReturnType
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
export function useMaxPriorityFeePerGasInput({
|
|
|
|
estimateToUse,
|
|
|
|
gasEstimateType,
|
|
|
|
gasFeeEstimates,
|
|
|
|
gasLimit,
|
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);
|
|
|
|
|
|
|
|
const {
|
|
|
|
currency: fiatCurrency,
|
|
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
|
|
} = useUserPreferencedCurrency(SECONDARY);
|
|
|
|
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
|
|
|
|
2021-12-12 00:26:28 +01:00
|
|
|
const initialMaxPriorityFeePerGas = supportsEIP1559
|
2021-10-22 19:14:28 +02:00
|
|
|
? getMaxPriorityFeePerGasFromTransaction(transaction)
|
|
|
|
: 0;
|
|
|
|
|
|
|
|
const [maxPriorityFeePerGas, setMaxPriorityFeePerGas] = useState(() => {
|
2021-12-12 00:26:28 +01:00
|
|
|
if (initialMaxPriorityFeePerGas && feeParamsAreCustom(transaction))
|
|
|
|
return initialMaxPriorityFeePerGas;
|
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 && initialMaxPriorityFeePerGas) {
|
|
|
|
setMaxPriorityFeePerGas(initialMaxPriorityFeePerGas);
|
2021-12-01 01:31:21 +01:00
|
|
|
}
|
2021-12-12 00:26:28 +01:00
|
|
|
}, [initialMaxPriorityFeePerGas, setMaxPriorityFeePerGas, supportsEIP1559V2]);
|
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
|
|
|
);
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
maxPriorityFeePerGas: maxPriorityFeePerGasToUse,
|
|
|
|
maxPriorityFeePerGasFiat: showFiat ? maxPriorityFeePerGasFiat : '',
|
|
|
|
setMaxPriorityFeePerGas,
|
|
|
|
};
|
|
|
|
}
|