mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
3732c5f71e
ESLint rules have been added to enforce our JSDoc conventions. These rules were introduced by updating `@metamask/eslint-config` to v9. Some of the rules have been disabled because the effort to fix all lint errors was too high. It might be easiest to enable these rules one directory at a time, or one rule at a time. Most of the changes in this PR were a result of running `yarn lint:fix`. There were a handful of manual changes that seemed obvious and simple to make. Anything beyond that and the rule was left disabled.
117 lines
3.6 KiB
JavaScript
117 lines
3.6 KiB
JavaScript
import { useSelector } from 'react-redux';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
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';
|
|
import { multiplyCurrencies } from '../../../shared/modules/conversion.utils';
|
|
|
|
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.
|
|
*/
|
|
|
|
/**
|
|
* @param options
|
|
* @param options.supportsEIP1559V2
|
|
* @param options.estimateToUse
|
|
* @param options.gasEstimateType
|
|
* @param options.gasFeeEstimates
|
|
* @param options.gasLimit
|
|
* @param options.transaction
|
|
* @returns {MaxPriorityFeePerGasInputReturnType}
|
|
*/
|
|
export function useMaxPriorityFeePerGasInput({
|
|
estimateToUse,
|
|
gasEstimateType,
|
|
gasFeeEstimates,
|
|
gasLimit,
|
|
supportsEIP1559V2,
|
|
transaction,
|
|
}) {
|
|
const supportsEIP1559 =
|
|
useSelector(checkNetworkAndAccountSupports1559) &&
|
|
!isLegacyTransaction(transaction?.txParams);
|
|
|
|
const {
|
|
currency: fiatCurrency,
|
|
numberOfDecimals: fiatNumberOfDecimals,
|
|
} = useUserPreferencedCurrency(SECONDARY);
|
|
|
|
const showFiat = useSelector(getShouldShowFiat);
|
|
|
|
const initialMaxPriorityFeePerGas = supportsEIP1559
|
|
? getMaxPriorityFeePerGasFromTransaction(transaction)
|
|
: 0;
|
|
|
|
const [maxPriorityFeePerGas, setMaxPriorityFeePerGas] = useState(() => {
|
|
if (initialMaxPriorityFeePerGas && feeParamsAreCustom(transaction)) {
|
|
return initialMaxPriorityFeePerGas;
|
|
}
|
|
return null;
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (supportsEIP1559V2 && initialMaxPriorityFeePerGas) {
|
|
setMaxPriorityFeePerGas(initialMaxPriorityFeePerGas);
|
|
}
|
|
}, [initialMaxPriorityFeePerGas, setMaxPriorityFeePerGas, supportsEIP1559V2]);
|
|
|
|
const maxPriorityFeePerGasToUse =
|
|
maxPriorityFeePerGas ??
|
|
getGasFeeEstimate(
|
|
'suggestedMaxPriorityFeePerGas',
|
|
gasFeeEstimates,
|
|
gasEstimateType,
|
|
estimateToUse,
|
|
initialMaxPriorityFeePerGas || 0,
|
|
);
|
|
|
|
// 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,
|
|
};
|
|
}
|