Skip to content

Commit

Permalink
Merge branch '17191/onboarding-unit-tests' of github.com:MetaMask/met…
Browse files Browse the repository at this point in the history
…amask-extension into 17191/onboarding-unit-tests
  • Loading branch information
tmashuang committed Feb 3, 2023
2 parents f02f30e + e3b9023 commit 08f4a0a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 10 additions & 6 deletions ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { isLegacyTransaction } from '../../helpers/utils/transactions.util';
import { hexWEIToDecGWEI } from '../../../shared/modules/conversion.utils';
import { feeParamsAreCustom, getGasFeeEstimate } from './utils';

const isNullOrUndefined = (value) => value === null || value === undefined;

const getMaxPriorityFeePerGasFromTransaction = (
transaction,
gasFeeEstimates,
Expand All @@ -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;
};

/**
Expand Down Expand Up @@ -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]);
Expand Down
11 changes: 11 additions & 0 deletions ui/hooks/gasFeeInput/useMaxPriorityFeePerGasInput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 08f4a0a

Please sign in to comment.