Skip to content

fix: resolve balance aggregation issue for large token decimals - #9653

Merged
Prithpal-Sooriya merged 4 commits into
mainfrom
fix/resolve-balance-aggregation-issue
Jul 29, 2026
Merged

fix: resolve balance aggregation issue for large token decimals#9653
Prithpal-Sooriya merged 4 commits into
mainfrom
fix/resolve-balance-aggregation-issue

Conversation

@Prithpal-Sooriya

@Prithpal-Sooriya Prithpal-Sooriya commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

Explanation

Removes old behaviour that was kept prior from WS issues around data in incorrect format, but now has been fixed.
Also did some test spec cleaning...

References

MetaMask/metamask-extension#44786

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
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Example Extension Test:

Screenshot 2026-07-29 at 12 37 58

Note

Medium Risk
Changes core balance aggregation math used for wallet/group fiat totals; the fix aligns with the invariant that state amounts are human-readable, but any edge case that still stored raw base units would now be mispriced.

Overview
Fixes incorrect portfolio totals when token balances are large human-readable amounts (e.g. billions of tokens with 9 decimals) by removing the legacy scaleToHumanIfRaw path in balance.ts aggregation.

assetsBalance amounts are always human-readable, but the old heuristic treated values ≥ 10^decimals as raw base units and divided by 10^decimals, which under-counted those holdings in getAggregatedBalanceForAccount, getAggregatedBalanceForAccountIds, calculateBalanceForAllWallets, and calculateBalanceChangeForAccountGroup (metamask-extension#44786).

Tests add a TangYuan regression case, shared arrangeAssetsControllerState helpers, and coverage for group aggregation when only accountTreeState is passed (no accountsById). Changelog documents the fix.

Reviewed by Cursor Bugbot for commit 7056a0b. Bugbot is set up for automated code reviews on this repo. Configure here.

removes old behaviour that was kept prior to WS issues around data in incorrect format, but now has been fixed
@Prithpal-Sooriya

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
Contributor

Preview builds have been published. Learn how to use preview builds in other projects.

Expand for full list of packages and versions.
@metamask-previews/account-tree-controller@7.5.5-preview-a3ea429
@metamask-previews/accounts-controller@39.0.5-preview-a3ea429
@metamask-previews/address-book-controller@7.1.2-preview-a3ea429
@metamask-previews/ai-controllers@0.8.0-preview-a3ea429
@metamask-previews/analytics-controller@1.2.1-preview-a3ea429
@metamask-previews/analytics-data-regulation-controller@0.0.0-preview-a3ea429
@metamask-previews/announcement-controller@8.1.0-preview-a3ea429
@metamask-previews/app-metadata-controller@2.0.1-preview-a3ea429
@metamask-previews/approval-controller@9.0.2-preview-a3ea429
@metamask-previews/assets-controller@11.2.1-preview-a3ea429
@metamask-previews/assets-controllers@110.0.0-preview-a3ea429
@metamask-previews/authenticated-user-storage@3.0.1-preview-a3ea429
@metamask-previews/base-controller@9.1.0-preview-a3ea429
@metamask-previews/base-data-service@0.1.3-preview-a3ea429
@metamask-previews/bitcoin-regtest-up@1.0.0-preview-a3ea429
@metamask-previews/bridge-controller@78.0.0-preview-a3ea429
@metamask-previews/bridge-status-controller@74.5.0-preview-a3ea429
@metamask-previews/build-utils@3.0.4-preview-a3ea429
@metamask-previews/chain-agnostic-permission@1.7.0-preview-a3ea429
@metamask-previews/chomp-api-service@4.0.0-preview-a3ea429
@metamask-previews/claims-controller@0.5.3-preview-a3ea429
@metamask-previews/client-controller@1.0.1-preview-a3ea429
@metamask-previews/client-utils@1.2.1-preview-a3ea429
@metamask-previews/compliance-controller@2.1.0-preview-a3ea429
@metamask-previews/composable-controller@12.0.1-preview-a3ea429
@metamask-previews/config-registry-controller@0.4.1-preview-a3ea429
@metamask-previews/connectivity-controller@0.3.0-preview-a3ea429
@metamask-previews/controller-utils@12.3.0-preview-a3ea429
@metamask-previews/core-backend@7.0.0-preview-a3ea429
@metamask-previews/delegation-controller@3.0.2-preview-a3ea429
@metamask-previews/earn-controller@12.2.2-preview-a3ea429
@metamask-previews/eip-5792-middleware@3.0.5-preview-a3ea429
@metamask-previews/eip-7702-internal-rpc-middleware@0.1.1-preview-a3ea429
@metamask-previews/eip1193-permission-middleware@2.0.1-preview-a3ea429
@metamask-previews/ens-controller@19.1.5-preview-a3ea429
@metamask-previews/eth-block-tracker@15.0.1-preview-a3ea429
@metamask-previews/eth-json-rpc-middleware@23.1.3-preview-a3ea429
@metamask-previews/eth-json-rpc-provider@6.0.1-preview-a3ea429
@metamask-previews/foundryup@1.0.1-preview-a3ea429
@metamask-previews/gas-fee-controller@26.3.0-preview-a3ea429
@metamask-previews/gator-permissions-controller@4.2.3-preview-a3ea429
@metamask-previews/geolocation-controller@0.1.3-preview-a3ea429
@metamask-previews/java-tron-up@1.0.0-preview-a3ea429
@metamask-previews/json-rpc-engine@10.5.0-preview-a3ea429
@metamask-previews/json-rpc-middleware-stream@8.0.8-preview-a3ea429
@metamask-previews/keyring-controller@27.1.0-preview-a3ea429
@metamask-previews/local-node-utils@1.0.0-preview-a3ea429
@metamask-previews/logging-controller@8.0.2-preview-a3ea429
@metamask-previews/message-manager@14.1.2-preview-a3ea429
@metamask-previews/messenger@2.0.0-preview-a3ea429
@metamask-previews/messenger-cli@0.2.0-preview-a3ea429
@metamask-previews/money-account-api-data-service@0.3.0-preview-a3ea429
@metamask-previews/money-account-balance-service@2.3.0-preview-a3ea429
@metamask-previews/money-account-controller@0.3.3-preview-a3ea429
@metamask-previews/money-account-upgrade-controller@3.0.0-preview-a3ea429
@metamask-previews/multichain-account-service@13.0.0-preview-a3ea429
@metamask-previews/multichain-api-middleware@4.0.1-preview-a3ea429
@metamask-previews/multichain-network-controller@3.2.1-preview-a3ea429
@metamask-previews/multichain-transactions-controller@7.1.1-preview-a3ea429
@metamask-previews/name-controller@9.1.2-preview-a3ea429
@metamask-previews/network-connection-banner-controller@0.1.0-preview-a3ea429
@metamask-previews/network-controller@34.0.0-preview-a3ea429
@metamask-previews/network-enablement-controller@5.6.0-preview-a3ea429
@metamask-previews/notification-services-controller@26.0.0-preview-a3ea429
@metamask-previews/passkey-controller@3.0.0-preview-a3ea429
@metamask-previews/permission-controller@13.1.1-preview-a3ea429
@metamask-previews/permission-log-controller@5.1.0-preview-a3ea429
@metamask-previews/perps-controller@10.0.0-preview-a3ea429
@metamask-previews/phishing-controller@17.3.0-preview-a3ea429
@metamask-previews/platform-api-docs@0.0.0-preview-a3ea429
@metamask-previews/polling-controller@16.0.8-preview-a3ea429
@metamask-previews/preferences-controller@23.1.0-preview-a3ea429
@metamask-previews/profile-metrics-controller@4.0.2-preview-a3ea429
@metamask-previews/profile-sync-controller@28.3.0-preview-a3ea429
@metamask-previews/ramps-controller@17.1.0-preview-a3ea429
@metamask-previews/rate-limit-controller@7.0.1-preview-a3ea429
@metamask-previews/react-data-query@0.2.2-preview-a3ea429
@metamask-previews/remote-feature-flag-controller@4.2.2-preview-a3ea429
@metamask-previews/sample-controllers@5.0.3-preview-a3ea429
@metamask-previews/seedless-onboarding-controller@10.1.0-preview-a3ea429
@metamask-previews/selected-network-controller@26.1.5-preview-a3ea429
@metamask-previews/sentinel-api-service@1.0.0-preview-a3ea429
@metamask-previews/shield-controller@5.1.3-preview-a3ea429
@metamask-previews/signature-controller@39.2.7-preview-a3ea429
@metamask-previews/smart-transactions-controller@25.0.1-preview-a3ea429
@metamask-previews/snap-account-service@2.0.0-preview-a3ea429
@metamask-previews/social-controllers@2.6.0-preview-a3ea429
@metamask-previews/solana-test-validator-up@1.0.0-preview-a3ea429
@metamask-previews/stellar-quickstart-up@0.0.0-preview-a3ea429
@metamask-previews/storage-service@1.0.2-preview-a3ea429
@metamask-previews/subscription-controller@6.2.1-preview-a3ea429
@metamask-previews/transaction-controller@69.2.1-preview-a3ea429
@metamask-previews/transaction-pay-controller@26.0.0-preview-a3ea429
@metamask-previews/user-operation-controller@41.2.7-preview-a3ea429
@metamask-previews/wallet@8.1.0-preview-a3ea429
@metamask-previews/wallet-cli@0.0.0-preview-a3ea429

@Prithpal-Sooriya

Copy link
Copy Markdown
Contributor Author

@cursor get latest and fix merge conflicts ty

@cursor

cursor Bot commented Jul 29, 2026

Copy link
Copy Markdown

Merged latest main into fix/resolve-balance-aggregation-issue and pushed the resolution.

Conflict resolved

  • packages/assets-controller/CHANGELOG.md — combined both sides under [Unreleased] in Keep a Changelog order (Added → Changed → Fixed), keeping your balance aggregation fix alongside the new main entries.

Post-merge fix

  • packages/assets-controller/src/selectors/balance.test.ts — the Sentry tracing test from main still called buildState(), which this branch had renamed to arrangeAssetsControllerState. Updated that call so tests pass (38/38).

Branch is pushed and PR #9653 is updated: #9653

Open in Web Open in Cursor 

Resolve CHANGELOG conflict by keeping both unreleased entries.
Fix tracing test to use arrangeAssetsControllerState helper.

Co-authored-by: Prithpal Sooriya <prithpal.sooriya@gmail.com>
@Prithpal-Sooriya
Prithpal-Sooriya added this pull request to the merge queue Jul 29, 2026
Merged via the queue into main with commit 39c3632 Jul 29, 2026
58 checks passed
@Prithpal-Sooriya
Prithpal-Sooriya deleted the fix/resolve-balance-aggregation-issue branch July 29, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants