From 4b18d48366432908126d3f808a0fa61e13b31ebf Mon Sep 17 00:00:00 2001 From: Dan J Miller Date: Fri, 3 Feb 2023 08:00:49 -0800 Subject: [PATCH] Ensure the advanced gas modal displays a zero priority fee if that is how it is set in the transaction (#17559) Co-authored-by: Pedro Figueiredo --- .../gasFeeInput/useMaxPriorityFeePerGasInput.js | 16 ++++++++++------ .../useMaxPriorityFeePerGasInput.test.js | 11 +++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.js b/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.js index 478039ca1..da29cb676 100644 --- a/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.js +++ b/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.js @@ -7,6 +7,8 @@ import { isLegacyTransaction } from '../../helpers/utils/transactions.util'; import { hexWEIToDecGWEI } from '../../../shared/lib/transactions-controller-utils'; import { feeParamsAreCustom, getGasFeeEstimate } from './utils'; +const isNullOrUndefined = (value) => value === null || value === undefined; + const getMaxPriorityFeePerGasFromTransaction = ( transaction, gasFeeEstimates, @@ -17,9 +19,8 @@ const getMaxPriorityFeePerGasFromTransaction = ( } const { maxPriorityFeePerGas, maxFeePerGas, gasPrice } = transaction?.txParams || {}; - return Number( - hexWEIToDecGWEI(maxPriorityFeePerGas || maxFeePerGas || gasPrice), - ); + const feeInHexWei = maxPriorityFeePerGas || maxFeePerGas || gasPrice; + return feeInHexWei ? Number(hexWEIToDecGWEI(feeInHexWei)) : null; }; /** @@ -50,17 +51,20 @@ export function useMaxPriorityFeePerGasInput({ const initialMaxPriorityFeePerGas = supportsEIP1559 ? getMaxPriorityFeePerGasFromTransaction(transaction, gasFeeEstimates) - : 0; + : null; const [maxPriorityFeePerGas, setMaxPriorityFeePerGas] = useState(() => { - if (initialMaxPriorityFeePerGas && feeParamsAreCustom(transaction)) { + if ( + !isNullOrUndefined(initialMaxPriorityFeePerGas) && + feeParamsAreCustom(transaction) + ) { return initialMaxPriorityFeePerGas; } return null; }); useEffect(() => { - if (supportsEIP1559 && initialMaxPriorityFeePerGas) { + if (supportsEIP1559 && !isNullOrUndefined(initialMaxPriorityFeePerGas)) { setMaxPriorityFeePerGas(initialMaxPriorityFeePerGas); } }, [initialMaxPriorityFeePerGas, setMaxPriorityFeePerGas, supportsEIP1559]); diff --git a/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.test.js b/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.test.js index ac79332ed..ce2afeb79 100644 --- a/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.test.js +++ b/ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.test.js @@ -102,4 +102,15 @@ describe('useMaxPriorityFeePerGasInput', () => { }); expect(result.current.maxPriorityFeePerGas).toBe(100); }); + + it('returns maxPriorityFeePerGas from transaction if it is 0', () => { + const { result } = renderUseMaxPriorityFeePerGasInputHook({ + transaction: { + txParams: { + maxPriorityFeePerGas: '0x0', + }, + }, + }); + expect(result.current.maxPriorityFeePerGas).toBe(0); + }); });