Skip to content

fix: Fix useTransactionGasFeeEstimate to calculate gas estimate properly #31469

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

Merged
merged 3 commits into from
Apr 2, 2025
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 @@ -5,21 +5,46 @@ import {
} from '../../../../../../../test/data/confirmations/contract-interaction';
import mockState from '../../../../../../../test/data/mock-state.json';
import { renderHookWithProvider } from '../../../../../../../test/lib/render-helpers';
import { useGasFeeEstimates } from '../../../../../../hooks/useGasFeeEstimates';
import { useTransactionGasFeeEstimate } from './useTransactionGasFeeEstimate';

describe('getGasFeeEstimate', () => {
it('returns the correct estimate when EIP1559 is supported', () => {
const transactionMeta = genUnapprovedContractInteractionConfirmation({
address: CONTRACT_INTERACTION_SENDER_ADDRESS,
}) as TransactionMeta;
const supportsEIP1559 = true;
jest.mock('../../../../../../hooks/useGasFeeEstimates', () => ({
useGasFeeEstimates: jest.fn(),
}));

describe('useTransactionGasFeeEstimate', () => {
const mockUseGasFeeEstimates = jest.mocked(useGasFeeEstimates);

beforeEach(() => {
jest.clearAllMocks();

mockUseGasFeeEstimates.mockReturnValue({
gasFeeEstimates: {
estimatedBaseFee: '0.5', // 0.5 GWEI
},
} as unknown as ReturnType<typeof useGasFeeEstimates>);
});

it('correctly calculates gas estimate using estimatedBaseFee for EIP1559', () => {
const transactionMeta = {
...genUnapprovedContractInteractionConfirmation({
address: CONTRACT_INTERACTION_SENDER_ADDRESS,
}),
txParams: {
gas: '0x5208', // 21000 in hex
maxPriorityFeePerGas: '0x3b9aca00', // 1 GWEI in hex
maxFeePerGas: '0x77359400', // 2 GWEI in hex
},
} as TransactionMeta;

const { result } = renderHookWithProvider(
() => useTransactionGasFeeEstimate(transactionMeta, supportsEIP1559),
() => useTransactionGasFeeEstimate(transactionMeta, true),
mockState,
);

expect(result.current).toMatchInlineSnapshot(`"3be226d2d900"`);
// 21000 * (0.5 + 1) = 21000 * 1.5 = 31500
// 31500 in hex is 1ca62a4f7800
expect(result.current).toBe('1ca62a4f7800');
});

it('returns the correct estimate when EIP1559 is not supported', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TransactionMeta } from '@metamask/transaction-controller';
import { Hex } from '@metamask/utils';
import {
addHexes,
decGWEIToHexWEI,
multiplyHexes,
} from '../../../../../../../shared/modules/conversion.utils';
import { Numeric } from '../../../../../../../shared/modules/Numeric';
Expand Down Expand Up @@ -30,9 +31,11 @@ export function useTransactionGasFeeEstimate(

let gasEstimate: Hex;
if (supportsEIP1559) {
const estimatedBaseFeeWeiHex = decGWEIToHexWEI(estimatedBaseFee);

// Minimum Total Fee = (estimatedBaseFee + maxPriorityFeePerGas) * gasLimit
let minimumFeePerGas = addHexes(
estimatedBaseFee || HEX_ZERO,
estimatedBaseFeeWeiHex || HEX_ZERO,
maxPriorityFeePerGas,
);

Expand Down
Loading