You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: improve balance fetch and not reset to 0 (#6743)
## Explanation
### Current State and Problem
Previously, the `TokenBalancesController` would update token balances,
native balances, and staked balances regardless of whether the values
had actually changed. This led to several inefficiencies:
1. **Unnecessary state updates**: The controller would trigger state
changes even when balance values remained the same
2. **Performance overhead**: Unchanged balances would still go through
the entire update pipeline, causing unnecessary re-renders and
processing
3. **Reset behavior**: Balances could be reset to zero and then
immediately updated with the same value, creating unnecessary
intermediate states
### Solution
This PR implements value comparison logic to skip updates when balances
haven't actually changed:
1. **Token Balance Updates**: Added comparison between `currentBalance`
and `newBalance` before updating the state. Only updates are applied
when values differ, preventing unnecessary state mutations.
2. **Native Balance Updates**: Enhanced the filter logic to compare
existing balances from `AccountTrackerController` state with new balance
values. Only balances that have actually changed are included in the
update batch sent to `AccountTrackerController:updateNativeBalances`.
3. **Staked Balance Updates**: Similar optimization for staked balances,
comparing current staked balance values with new ones before including
them in the update batch sent to
`AccountTrackerController:updateStakedBalances`.
### Key Improvements
- **Reduced State Mutations**: Prevents unnecessary state updates when
balance values are identical
- **Better Performance**: Eliminates redundant processing and re-renders
caused by unchanged balance updates
- **Cleaner State Management**: Avoids intermediate states where
balances are reset to zero before being set to the same value
- **Batch Optimization**: Only sends actual changes to the
AccountTracker, reducing the payload size and processing overhead
### Technical Details
The implementation uses `isEqual` comparison for the main state update
check and direct value comparison (`currentBalance !== newBalance`) for
individual balance updates. This ensures that both the overall state
update and individual balance updates are optimized to only occur when
necessary.
## References
<!--
Are there any issues that this pull request is tied to?
Are there other links that reviewers should consult to understand these
changes better?
Are there client or consumer pull requests to adopt any breaking
changes?
For example:
* Fixes #12345
* Related to #67890
-->
## Checklist
- [ ] I've updated the test suite for new or updated code as appropriate
- [ ] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [ ] 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),
highlighting breaking changes as necessary
- [ ] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Avoids unnecessary token/native/staked balance updates by comparing
existing values and only updating when changed, reducing state mutations
and calls.
>
> - **Fixed: Balance update churn**
> - `TokenBalancesController`
> - Initialize missing entries without overwriting existing balances;
only set balances when values change.
> - Filter `updateNativeBalances`/`updateStakedBalances` calls by
diffing against `AccountTrackerController:getState` and only send
changed entries.
> - Add `AccountTrackerController:getState` to allowed actions.
> - `AccountTrackerController`
> - Optimize `updateNativeBalances` and `updateStakedBalances` to clone
state, compare current values, and call `update` only if changes exist.
> - **Tests**: Permit and stub `AccountTrackerController:getState`;
update messenger setups accordingly.
> - **Changelog**: Note fix for unnecessary balance updates to improve
performance.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
460183d. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Copy file name to clipboardExpand all lines: packages/assets-controllers/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
11
11
12
12
- Bump `@metamask/utils` from `^11.8.0` to `^11.8.1` ([#6708](https://github.com/MetaMask/core/pull/6708))
13
13
14
+
### Fixed
15
+
16
+
- Fix unnecessary balance updates in `TokenBalancesController` by skipping updates when values haven't changed ([#6743](https://github.com/MetaMask/core/pull/6743))
17
+
- Prevents unnecessary state mutations for token balances when values are identical
18
+
- Improves performance by reducing redundant processing and re-renders
0 commit comments