Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Gas changes for low Max base fee and Priority fee #28037

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ export const useEIP1559TxFees = (
maxFeePerGas: string;
maxPriorityFeePerGas: string;
} => {
const hexMaxFeePerGas = transactionMeta?.txParams?.maxFeePerGas;
const hexMaxFeePerGas =
transactionMeta.dappSuggestedGasFees?.maxFeePerGas ||
transactionMeta?.txParams?.maxFeePerGas;
const hexMaxPriorityFeePerGas =
transactionMeta.dappSuggestedGasFees?.maxPriorityFeePerGas ||
transactionMeta?.txParams?.maxPriorityFeePerGas;

return useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getValueFromWeiHex,
multiplyHexes,
} from '../../../../../../../shared/modules/conversion.utils';
import { Numeric } from '../../../../../../../shared/modules/Numeric';
import { getConversionRate } from '../../../../../../ducks/metamask/metamask';
import { useFiatFormatter } from '../../../../../../hooks/useFiatFormatter';
import { useGasFeeEstimates } from '../../../../../../hooks/useGasFeeEstimates';
Expand Down Expand Up @@ -114,11 +115,21 @@ export function useFeeCalculations(transactionMeta: TransactionMeta) {
}

// Logic for any network without L1 and L2 fee components
const minimumFeePerGas = addHexes(
let minimumFeePerGas = addHexes(
decGWEIToHexWEI(estimatedBaseFee) || HEX_ZERO,
decimalToHex(maxPriorityFeePerGas),
);

// `minimumFeePerGas` should never be higher than the `maxFeePerGas`
if (
new Numeric(minimumFeePerGas, 16).greaterThan(
decimalToHex(maxFeePerGas),
16,
)
) {
minimumFeePerGas = decimalToHex(maxFeePerGas);
}

const estimatedFee = multiplyHexes(
supportsEIP1559 ? (minimumFeePerGas as Hex) : (gasPrice as Hex),
gasLimit as Hex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
addHexes,
multiplyHexes,
} from '../../../../../../../shared/modules/conversion.utils';
import { Numeric } from '../../../../../../../shared/modules/Numeric';
import { useGasFeeEstimates } from '../../../../../../hooks/useGasFeeEstimates';
import { HEX_ZERO } from '../shared/constants';

Expand All @@ -28,15 +29,24 @@ export function useTransactionGasFeeEstimate(
transactionMeta.dappSuggestedGasFees?.maxPriorityFeePerGas ||
transactionMeta.txParams?.maxPriorityFeePerGas ||
HEX_ZERO;
const maxFeePerGas =
transactionMeta.dappSuggestedGasFees?.maxFeePerGas ||
transactionMeta.txParams?.maxFeePerGas ||
HEX_ZERO;

let gasEstimate: Hex;
if (supportsEIP1559) {
// Minimum Total Fee = (estimatedBaseFee + maxPriorityFeePerGas) * gasLimit
const minimumFeePerGas = addHexes(
let minimumFeePerGas = addHexes(
estimatedBaseFee || HEX_ZERO,
maxPriorityFeePerGas,
);

// `minimumFeePerGas` should never be higher than the `maxFeePerGas`
if (new Numeric(minimumFeePerGas, 16).greaterThan(maxFeePerGas, 16)) {
minimumFeePerGas = maxFeePerGas;
}

gasEstimate = multiplyHexes(minimumFeePerGas as Hex, gasLimit as Hex);
} else {
gasEstimate = multiplyHexes(gasPrice as Hex, gasLimit as Hex);
Expand Down