Commit 30c8a38
authored
fix: should not update balances when app is locked (#7492)
## Explanation
### Current state and why it needs to change
The `AccountTrackerController` previously had inconsistent lock/unlock
handling compared to `TokenBalancesController`:
1. **No lock state tracking**: The controller had no way to track
whether the keyring was locked, meaning it could attempt balance fetches
even when the wallet was locked.
2. **Refresh on unlock**: When `KeyringController:unlock` was fired, it
would immediately trigger a full balance refresh for all networks. This
behavior was unnecessary since polling continues regardless of lock
state.
3. **Refresh all networks on network add**: When a new network was added
via `NetworkController:networkAdded`, the controller would refresh
balances for **all** networks instead of just the newly added one.
### Solution
This PR aligns `AccountTrackerController` with the pattern already
established in `TokenBalancesController`:
1. **Added `#isLocked` property**: Tracks whether the keyring is locked,
initialized from `KeyringController:getState` in the constructor.
2. **Added `isActive` getter**: Returns `true` when the keyring is
unlocked AND the user is onboarded. Balance fetching is skipped when
`isActive` is `false`.
3. **Subscribe to `KeyringController:lock`**: Sets `#isLocked = true`
when the keyring is locked.
4. **Updated `KeyringController:unlock` subscription**: Now only sets
`#isLocked = false` without triggering an immediate refresh. Polling
continues as normal and will fetch balances on the next interval.
5. **Optimized `NetworkController:networkAdded` handler**: Now extracts
the `networkClientId` from the added network configuration and only
refreshes balances for that specific network.
### Breaking changes
- `AccountTrackerController` messenger must now allow
`KeyringController:getState` action
- `AccountTrackerController` messenger must now allow
`KeyringController:lock` event
## References
- Similar pattern exists in `TokenBalancesController` (`#isUnlocked`
property and `isActive` getter)
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/contributing.md#updating-changelogs)
- [x] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> AccountTrackerController now tracks keyring lock state to gate balance
updates, adds isActive, refreshes only the newly added network on
networkAdded, and requires KeyringController:getState and :lock.
>
> - **AccountTrackerController**
> - **Lock state & activity gating**:
> - Add `#isLocked` (initialized via `KeyringController:getState`) and
`isActive` getter; skip balance updates when inactive (locked or not
onboarded).
> - Subscribe to `KeyringController:lock`/`unlock` to update lock state;
unlock no longer triggers immediate refresh.
> - **Network addition behavior**:
> - On `NetworkController:networkAdded`, refresh balances only for the
newly added network’s `networkClientId`.
> - **Messenger contract (BREAKING)**:
> - Requires `KeyringController:getState` action and
`KeyringController:lock` event in allowed actions/events.
> - **Tests & docs**:
> - Update tests to publish full `NetworkConfiguration` and assert
`isActive` transitions.
> - Update `CHANGELOG.md` to document breaking changes and behavior
tweaks.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
6c2de6a. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 023d8c0 commit 30c8a38
File tree
3 files changed
+78
-32
lines changed- packages/assets-controllers
- src
3 files changed
+78
-32
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
14 | 24 | | |
15 | 25 | | |
16 | 26 | | |
| |||
Lines changed: 24 additions & 19 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
395 | 399 | | |
396 | 400 | | |
397 | 401 | | |
| |||
402 | 406 | | |
403 | 407 | | |
404 | 408 | | |
405 | | - | |
| 409 | + | |
406 | 410 | | |
407 | 411 | | |
408 | 412 | | |
409 | 413 | | |
410 | 414 | | |
411 | 415 | | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
| 416 | + | |
| 417 | + | |
420 | 418 | | |
421 | | - | |
422 | | - | |
423 | | - | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
424 | 422 | | |
425 | | - | |
426 | | - | |
427 | | - | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
428 | 426 | | |
429 | 427 | | |
430 | 428 | | |
| |||
2033 | 2031 | | |
2034 | 2032 | | |
2035 | 2033 | | |
| 2034 | + | |
| 2035 | + | |
| 2036 | + | |
| 2037 | + | |
| 2038 | + | |
2036 | 2039 | | |
2037 | 2040 | | |
2038 | 2041 | | |
| |||
2050 | 2053 | | |
2051 | 2054 | | |
2052 | 2055 | | |
| 2056 | + | |
2053 | 2057 | | |
2054 | 2058 | | |
2055 | 2059 | | |
2056 | 2060 | | |
2057 | 2061 | | |
2058 | 2062 | | |
| 2063 | + | |
2059 | 2064 | | |
2060 | 2065 | | |
2061 | 2066 | | |
| |||
Lines changed: 44 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
192 | 196 | | |
193 | 197 | | |
194 | 198 | | |
195 | | - | |
| 199 | + | |
| 200 | + | |
196 | 201 | | |
197 | 202 | | |
198 | 203 | | |
| |||
217 | 222 | | |
218 | 223 | | |
219 | 224 | | |
| 225 | + | |
220 | 226 | | |
221 | 227 | | |
222 | 228 | | |
| |||
256 | 262 | | |
257 | 263 | | |
258 | 264 | | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
259 | 268 | | |
260 | 269 | | |
261 | 270 | | |
| |||
331 | 340 | | |
332 | 341 | | |
333 | 342 | | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
334 | 346 | | |
335 | 347 | | |
336 | 348 | | |
| |||
345 | 357 | | |
346 | 358 | | |
347 | 359 | | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
353 | 372 | | |
354 | 373 | | |
355 | | - | |
356 | | - | |
357 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
358 | 379 | | |
359 | 380 | | |
360 | 381 | | |
| |||
392 | 413 | | |
393 | 414 | | |
394 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
395 | 426 | | |
396 | 427 | | |
397 | 428 | | |
| |||
645 | 676 | | |
646 | 677 | | |
647 | 678 | | |
648 | | - | |
| 679 | + | |
649 | 680 | | |
650 | 681 | | |
651 | 682 | | |
| |||
815 | 846 | | |
816 | 847 | | |
817 | 848 | | |
818 | | - | |
819 | | - | |
| 849 | + | |
| 850 | + | |
820 | 851 | | |
821 | 852 | | |
822 | 853 | | |
| |||
0 commit comments