Commit c594a57
fix: prevent balance empty state flash during wallet import and account switching (MetaMask#23351)
## **Description**
This PR creates a temporary fix for a UX issue where the "Fund your
wallet" empty state briefly flashes when importing a wallet with
existing funds or switching between funded accounts. This happened
because the balance calculation returned $0 while price and balance data
were still loading, causing the empty state to appear before the actual
balance was displayed.
_**"Telling a user they need to fund an account that might have
thousands of dollars in it is confusing at best and mildly
panic-inducing at worst."**_
### Problem
PR MetaMask#21391 introduced a balance empty state that displays when users have
zero mainnet balance. However, due to how balance data loads
asynchronously, users would see:
1. Skeleton loader (correct)
2. **"Fund your wallet" empty state (incorrect - account has funds!)**
3. Actual balance appears (e.g., $49.21)
This flash occurred because:
- Balance calculations depend on multiple data sources (price data,
token balances, native balances)
- These data sources load at different times via polling
- The `AccountTrackerController `always initializes accounts with
`balance: "0x0"`
- We cannot distinguish between "balance is loading" vs "balance is
genuinely $0"
### Solutions Considered
We evaluated several approaches to solve this issue:
#### Option 1: Add Loading State to `AccountTrackerController` ❌
- **Approach**: Modify the core controller to track whether native
balances have been fetched
- **Why rejected**: This is the ideal state but would require changes to
`@metamask/assets-controllers` (shared between mobile and extension),
introducing complexity and breaking type changes across the codebase.
Too large of a scope for this fix.
#### Option 2: Check Controller Initialization State ❌
- **Approach**: Create a selector that validates all price/balance
controllers have initialized
- **Why rejected**: Controllers persist state via redux-persist, so they
always appear "initialized" even with stale data from previous sessions.
Logs confirmed this - all controllers report "state persisted
successfully" immediately on load.
#### Option 3: Balance Change Tracking + Timeout ✅ (Chosen)
- **Approach**: Track when balance values actually change from their
initial value, with a timeout fallback
- **Why chosen**:
- Works with existing architecture without controller modifications
- Relies on observable behavior (balance updates) rather than internal
controller state
- Handles all edge cases: fresh imports, account switching, genuinely
empty wallets
- Simple component-level solution
- **Most importantly**: Prevents showing "fund your wallet" to users
with existing funds
### Implementation
The solution adds balance change tracking to `AccountGroupBalance`
component:
1. **Track account switches** via `groupId` changes - resets all
tracking state
2. **Monitor balance changes** from initial $0 value - marks as
"fetched" when balance updates
3. **Fallback timeout** (3 seconds) - handles genuinely $0 balances or
slow API responses
4. **Dual balance tracking** - watches both `groupBalance` and
`accountGroupBalance` since empty state decision uses the latter
The skeleton loader now displays until either:
- Balance changes from its initial value, OR
- 3-second timeout expires
This ensures users never see the empty state flash for funded accounts.
## **Changelog**
CHANGELOG entry: Fixed an issue where "Fund your wallet" empty state
briefly appeared when importing wallets with existing funds or switching
between funded accounts
## **Related issues**
Fixes: (related to MetaMask#21391 - balance empty state implementation)
## **Manual testing steps**
\`\`\`gherkin
Feature: Balance empty state loading behavior
Scenario: Import wallet with funded accounts
Given the app is freshly installed
When user imports a wallet using SRP with funded accounts
Then user should see skeleton loader
And user should NOT see "Fund your wallet" empty state
And user should see actual balance (e.g., "$49.21")
Scenario: Switch between funded accounts
Given user has multiple accounts with funds
When user switches from Account 1 to Account 2
Then user should see skeleton loader briefly
And user should NOT see "Fund your wallet" empty state
And user should see Account 2's balance
Scenario: Import wallet with zero balance
Given the app is freshly installed
When user imports a wallet with zero mainnet balance
Then user should see skeleton loader
And after 3 seconds, user should see "Fund your wallet" empty state
Scenario: Switch to account with zero balance
Given user has account with zero mainnet balance
When user switches to the zero-balance account
Then user should see skeleton loader
And after 3 seconds, user should see "Fund your wallet" empty state
\`\`\`
## **Screenshots/Recordings**
### **Before**
Empty state incorrectly flashed when importing wallet with funds or
switching accounts.
https://github.com/user-attachments/assets/945567d9-e9b1-458d-aecc-66f05384bcc9
### **After**
Skeleton loader now prevents layout shift displays until balance is
confirmed, preventing false "fund your wallet" messaging.
https://github.com/user-attachments/assets/f1763e5c-f9b4-43c3-98a5-4525a2e83a9c
## **Pre-merge author checklist**
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/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-mobile/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]
> Prevents the “Fund your wallet” flash by tracking balance fetch with a
3s timeout and using Skeleton to hide content until ready; updates
Skeleton behavior and related tests/styles.
>
> - **Account Group Balance**
> - Implement balance fetch tracking with a 3s timeout using
`hasBalanceFetched`, `groupId` change detection, and initial balance
refs in `AccountGroupBalance.tsx`.
> - Gate empty state rendering (`BalanceEmptyState`) until loading
completes; compute `isLoading = !groupBalance || !hasBalanceFetched`.
> - Wrap balance and change components with `Skeleton` using
`hideChildren` to avoid content flash.
> - Adjust styles to column layout and left alignment in
`AccountGroupBalance.styles.ts`.
> - **Skeleton Component**
> - Change render logic to return `children` directly when
`hideChildren` is false; otherwise render animated placeholder and
optional children wrapper.
> - **Tests**
> - Add timer-based tests covering timeout-driven display, immediate
display on balance change, and zero-balance empty state.
> - Update Skeleton tests to reflect new child rendering behavior and
animation conditions.
> - Remove redundant story/use case and align stories with new
`hideChildren` flow.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
4396697. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>1 parent 896144b commit c594a57
6 files changed
Lines changed: 302 additions & 102 deletions
File tree
- app
- component-library/components/Skeleton
- components/UI/Assets/components/Balance
Lines changed: 0 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
56 | 56 | | |
57 | 57 | | |
58 | 58 | | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | 59 | | |
82 | 60 | | |
83 | 61 | | |
| |||
Lines changed: 2 additions & 20 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
75 | | - | |
76 | 60 | | |
77 | 61 | | |
78 | 62 | | |
| |||
92 | 76 | | |
93 | 77 | | |
94 | 78 | | |
95 | | - | |
96 | 79 | | |
97 | 80 | | |
98 | | - | |
| 81 | + | |
99 | 82 | | |
100 | 83 | | |
101 | 84 | | |
102 | 85 | | |
103 | | - | |
104 | | - | |
| 86 | + | |
105 | 87 | | |
106 | 88 | | |
107 | 89 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
71 | | - | |
72 | | - | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
73 | 77 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
83 | 89 | | |
84 | 90 | | |
85 | | - | |
| 91 | + | |
86 | 92 | | |
87 | 93 | | |
88 | 94 | | |
| |||
Lines changed: 1 addition & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | 8 | | |
13 | 9 | | |
| 10 | + | |
14 | 11 | | |
15 | 12 | | |
16 | 13 | | |
| |||
Lines changed: 173 additions & 10 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | | - | |
60 | | - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
61 | 87 | | |
62 | 88 | | |
63 | 89 | | |
64 | | - | |
| 90 | + | |
| 91 | + | |
65 | 92 | | |
66 | 93 | | |
67 | | - | |
| 94 | + | |
68 | 95 | | |
69 | 96 | | |
70 | 97 | | |
| |||
81 | 108 | | |
82 | 109 | | |
83 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
84 | 116 | | |
85 | | - | |
| 117 | + | |
86 | 118 | | |
87 | 119 | | |
88 | | - | |
| 120 | + | |
89 | 121 | | |
90 | 122 | | |
91 | 123 | | |
92 | 124 | | |
93 | 125 | | |
94 | | - | |
| 126 | + | |
95 | 127 | | |
96 | 128 | | |
97 | | - | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
98 | 132 | | |
99 | 133 | | |
100 | 134 | | |
| |||
107 | 141 | | |
108 | 142 | | |
109 | 143 | | |
110 | | - | |
111 | | - | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
112 | 159 | | |
113 | 160 | | |
114 | 161 | | |
115 | 162 | | |
116 | 163 | | |
117 | 164 | | |
118 | 165 | | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
119 | 282 | | |
0 commit comments