From 8fa45c545460f495cefcc3357dc2d01c545082e0 Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Fri, 3 Feb 2023 03:27:32 -0800 Subject: [PATCH] Fix speed up of 0 max priority fee transactions (#17547) * Fix speed up of 0 max priority fee transactions * Update ui/hooks/gasFeeInput/useTransactionFunction.test.js Co-authored-by: Brad Decker * Use bignumber for comparison to zero --------- Co-authored-by: Brad Decker Co-authored-by: Pedro Figueiredo --- .../useTransactionFunction.test.js | 25 +++++++++++++++++ .../gasFeeInput/useTransactionFunctions.js | 27 ++++++++++++++++--- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/ui/hooks/gasFeeInput/useTransactionFunction.test.js b/ui/hooks/gasFeeInput/useTransactionFunction.test.js index 60f4ba6c4..08e47615b 100644 --- a/ui/hooks/gasFeeInput/useTransactionFunction.test.js +++ b/ui/hooks/gasFeeInput/useTransactionFunction.test.js @@ -90,6 +90,31 @@ describe('useMaxPriorityFeePerGasInput', () => { }); }); + it('invokes action updateTransaction with 10% increased max priority fee and medium fee + 10% when updateTransactionToTenPercentIncreasedGasFee callback is invoked while original priority fee is 0', async () => { + const mockUpdateGasFees = jest + .spyOn(Actions, 'updateTransactionGasFees') + .mockImplementation(() => ({ type: '' })); + + const { result } = renderUseTransactionFunctions({ + transaction: { + userFeeLevel: CUSTOM_GAS_ESTIMATE, + txParams: { maxFeePerGas: '0x5028', maxPriorityFeePerGas: '0x0' }, + }, + }); + await result.current.updateTransactionToTenPercentIncreasedGasFee(); + expect(mockUpdateGasFees).toHaveBeenCalledTimes(1); + expect(mockUpdateGasFees).toHaveBeenCalledWith(undefined, { + estimateSuggested: 'tenPercentIncreased', + estimateUsed: 'custom', + gas: '5208', + gasLimit: '5208', + maxFeePerGas: '0x582c', + maxPriorityFeePerGas: '0x1caf4ad00', + userEditedGasLimit: undefined, + userFeeLevel: 'custom', + }); + }); + it('should invoke action updateTransaction with estimate gas values fee when updateTransactionUsingEstimate callback is invoked', async () => { const mockUpdateGasFees = jest .spyOn(Actions, 'updateTransactionGasFees') diff --git a/ui/hooks/gasFeeInput/useTransactionFunctions.js b/ui/hooks/gasFeeInput/useTransactionFunctions.js index 17d2c60c4..8fd79b3e2 100644 --- a/ui/hooks/gasFeeInput/useTransactionFunctions.js +++ b/ui/hooks/gasFeeInput/useTransactionFunctions.js @@ -1,7 +1,12 @@ import { useCallback } from 'react'; import { useDispatch } from 'react-redux'; -import { EditGasModes, PriorityLevels } from '../../../shared/constants/gas'; +import BigNumber from 'bignumber.js'; +import { + EditGasModes, + PriorityLevels, + CUSTOM_GAS_ESTIMATE, +} from '../../../shared/constants/gas'; import { addTenPercentAndRound, editGasModeIsSpeedUpOrCancel, @@ -164,17 +169,31 @@ export const useTransactionFunctions = ({ maxPriorityFeePerGas, } = transaction.previousGas || transaction.txParams; + const newMaxPriorityFeePerGas = new BigNumber( + maxPriorityFeePerGas, + 16, + ).isZero() + ? decGWEIToHexWEI( + gasFeeEstimates[defaultEstimateToUse].suggestedMaxPriorityFeePerGas, + ) + : maxPriorityFeePerGas; + + const estimateUsed = + maxPriorityFeePerGas === '0x0' + ? CUSTOM_GAS_ESTIMATE + : PriorityLevels.tenPercentIncreased; + updateTransaction({ estimateSuggested: initTransaction ? defaultEstimateToUse : PriorityLevels.tenPercentIncreased, - estimateUsed: PriorityLevels.tenPercentIncreased, + estimateUsed, gasLimit, maxFeePerGas: addTenPercentAndRound(maxFeePerGas), - maxPriorityFeePerGas: addTenPercentAndRound(maxPriorityFeePerGas), + maxPriorityFeePerGas: addTenPercentAndRound(newMaxPriorityFeePerGas), }); }, - [defaultEstimateToUse, transaction, updateTransaction], + [defaultEstimateToUse, gasFeeEstimates, transaction, updateTransaction], ); const updateTransactionUsingEstimate = useCallback(