Skip to content

Commit 8fb01e1

Browse files
authored
fix: standardize fallback value for zero hex in balance checks (#38649)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** This PR fixes a [issue raised by cursor](#38306 (comment)) to 0x0 is a numeric literal (the number zero) rather than the hex string '0x0' <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/38649?quickstart=1) ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null ## **Related issues** Fixes: MetaMask/MetaMask-planning#6408 ## **Manual testing steps** 1. Send flow with insufficient balance 2. should display the insufficient alert ## **Screenshots/Recordings** [alert.webm](https://github.com/user-attachments/assets/5cf66e88-8256-4995-9b2d-4cb30f2f5198) <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > Standardizes zero-hex fallback via a shared `ZERO_HEX_FALLBACK` in `useHasInsufficientBalance` for tx value, batch values, and balance lookups. > > - **Hooks** > - `ui/pages/confirmations/hooks/useHasInsufficientBalance.ts`: > - Introduces `ZERO_HEX_FALLBACK = '0x0'`. > - Replaces inline zero-hex defaults with `ZERO_HEX_FALLBACK` for `txParams.value`, nested transaction values, and balance fallback. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 3d24dbb. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 69fcd37 commit 8fb01e1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ui/pages/confirmations/hooks/useHasInsufficientBalance.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { useConfirmContext } from '../context/confirm';
1212
import { isBalanceSufficient } from '../send-legacy/send.utils';
1313

14+
const ZERO_HEX_FALLBACK = '0x0';
15+
1416
export function useHasInsufficientBalance(): {
1517
hasInsufficientBalance: boolean;
1618
nativeCurrency?: string;
@@ -19,12 +21,12 @@ export function useHasInsufficientBalance(): {
1921
const {
2022
id: transactionId,
2123
chainId,
22-
txParams: { value = '0x0', from: fromAddress = '' } = {},
24+
txParams: { value = ZERO_HEX_FALLBACK, from: fromAddress = '' } = {},
2325
} = currentConfirmation ?? {};
2426

2527
const batchTransactionValues =
2628
currentConfirmation?.nestedTransactions?.map(
27-
(trxn) => (trxn.value as Hex) ?? 0x0,
29+
(trxn) => (trxn.value as Hex) ?? ZERO_HEX_FALLBACK,
2830
) ?? [];
2931

3032
const chainBalances = useSelector((state) =>
@@ -34,7 +36,7 @@ export function useHasInsufficientBalance(): {
3436
),
3537
) as Record<Hex, Hex>;
3638

37-
const balance = chainBalances?.[chainId as Hex] ?? '0x0';
39+
const balance = chainBalances?.[chainId as Hex] ?? ZERO_HEX_FALLBACK;
3840

3941
const totalValue = sumHexes(value, ...batchTransactionValues);
4042

0 commit comments

Comments
 (0)