From 6e82ec22f319e11c063594341d6b697497653dfb Mon Sep 17 00:00:00 2001 From: David Walsh Date: Fri, 13 Oct 2023 09:59:06 -0500 Subject: [PATCH] UX: Multichain: Fix useAccountTotalFiatBalance when getWeiHexFromDecimalValue returns "NaN" (#21359) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** While onboarding with `MULTICHAIN` environment variable turned on, I noticed a crash upon initial loading screen. The cause was `getWeiHexFromDecimalValue` returning "NaN" in this block: ``` const totalWeiBalance = getWeiHexFromDecimalValue({ value: totalFiatBalance, fromCurrency: currentCurrency, conversionRate, invertConversionRate: true, }); ``` with params: * `value`: `"0"` * `fromCurrency`: `"usd"` * `conversionRate`: `0` Checking for valid output and returning `0x0` prevents the balance error ## **Manual testing steps** 0. `MULTICHAIN=1 yarn start` _1. Remove your local Metamask _2. Re-install and go through onboarding _3. Get to home wallet screen without crashing ## **Related issues** _Fixes #???_ ## **Pre-merge author checklist** - [ ] I’ve followed [MetaMask Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've clearly explained: - [ ] What problem this PR is solving. - [ ] How this problem was solved. - [ ] How reviewers can test my changes. - [ ] I’ve indicated what issue this PR is linked to: Fixes #??? - [ ] I’ve included tests if applicable. - [ ] I’ve documented any added code. - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). - [ ] I’ve properly set the pull request status: - [ ] In case it's not yet "ready for review", I've set it to "draft". - [ ] In case it's "ready for review", I've changed it from "draft" to "non-draft". ## **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. --- ui/hooks/useAccountTotalFiatBalance.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ui/hooks/useAccountTotalFiatBalance.js b/ui/hooks/useAccountTotalFiatBalance.js index ee0e8173a629..0d2a63d454c1 100644 --- a/ui/hooks/useAccountTotalFiatBalance.js +++ b/ui/hooks/useAccountTotalFiatBalance.js @@ -82,13 +82,19 @@ export const useAccountTotalFiatBalance = ( const formattedFiat = formatCurrency(totalFiatBalance, currentCurrency); // WEI Number which can be used with UserPreferencedCurrencyDisplay component - const totalWeiBalance = getWeiHexFromDecimalValue({ + let totalWeiBalance = getWeiHexFromDecimalValue({ value: totalFiatBalance, fromCurrency: currentCurrency, conversionRate, invertConversionRate: true, }); + // If we have a totalFiatBalance of "0" and conversionRate of "0", + // getWeiHexFromDecimalValue responds with "NaN" + if (totalWeiBalance === 'NaN') { + totalWeiBalance = '0x0'; + } + return { formattedFiat, totalWeiBalance,