diff --git a/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.test.ts b/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.test.ts index 088ebc37412e..220224d144b2 100644 --- a/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.test.ts +++ b/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.test.ts @@ -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); + }); + + 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', () => { diff --git a/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.ts b/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.ts index 2a4ddb7d162f..eac66ff43d18 100644 --- a/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.ts +++ b/ui/pages/confirmations/components/confirm/info/hooks/useTransactionGasFeeEstimate.ts @@ -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'; @@ -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, );