Skip to content

Conversation

bergarces
Copy link
Contributor

@bergarces bergarces commented Aug 21, 2025

Explanation

The format for EVM addresses for AccountTrackerController in mobile differs from extension and from the other controller states. This was causing it to miss native account balances.

It also uses the correct internal account type in the assets, so that BTC account types can be identified.

Finally, it fixes an issue with the mergeAssets function mutating internal parts of cached objects.

References

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, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@bergarces bergarces changed the title fix: make address keys consistent fix: token selector issues Aug 21, 2025
existingAssets[accountGroupId] = {};
for (const [network, chainAssets] of Object.entries(accountAssets)) {
existingAssets[accountGroupId][network] = [...chainAssets];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was tricky to spot. By reusing the array from the memoized object and later pushing to it, the new test was failing unless it was checked on isolation. This could have caused problems in the clients as well.

)) {
const accountGroupId = accountsMap[accountAddress];
const account = accountsMap[accountAddress.toLowerCase()];
if (!account) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be possible, but better not to take any chances that can crash the app.

const tokenAddress = token.address as Hex;
const accountGroupId = accountsMap[accountAddress];
const account = accountsMap[accountAddress];
if (!account) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be possible, but better not to take any chances that can crash the app.

type MultichainAccountType = Exclude<
InternalAccount['type'],
`eip155:${string}`
>;
Copy link
Contributor Author

@bergarces bergarces Aug 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to use this type from the account instead of the generic evm and multichain. This will allow us to determine the type of bitcoin account the asset belongs to.

This future issue was raised recently on slack.

@bergarces bergarces marked this pull request as ready for review August 21, 2025 17:35
@bergarces bergarces requested review from a team as code owners August 21, 2025 17:35
cursor[bot]

This comment was marked as outdated.

@bergarces
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/account-tree-controller": "0.10.0-preview-b2b44c46",
  "@metamask-previews/accounts-controller": "33.0.0-preview-b2b44c46",
  "@metamask-previews/address-book-controller": "6.1.1-preview-b2b44c46",
  "@metamask-previews/announcement-controller": "7.0.3-preview-b2b44c46",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-b2b44c46",
  "@metamask-previews/approval-controller": "7.1.3-preview-b2b44c46",
  "@metamask-previews/assets-controllers": "74.0.0-preview-b2b44c46",
  "@metamask-previews/base-controller": "8.2.0-preview-b2b44c46",
  "@metamask-previews/bridge-controller": "41.1.0-preview-b2b44c46",
  "@metamask-previews/bridge-status-controller": "40.1.0-preview-b2b44c46",
  "@metamask-previews/build-utils": "3.0.3-preview-b2b44c46",
  "@metamask-previews/chain-agnostic-permission": "1.1.1-preview-b2b44c46",
  "@metamask-previews/composable-controller": "11.0.0-preview-b2b44c46",
  "@metamask-previews/controller-utils": "11.12.0-preview-b2b44c46",
  "@metamask-previews/delegation-controller": "0.7.0-preview-b2b44c46",
  "@metamask-previews/earn-controller": "6.0.0-preview-b2b44c46",
  "@metamask-previews/eip1193-permission-middleware": "1.0.0-preview-b2b44c46",
  "@metamask-previews/ens-controller": "17.0.1-preview-b2b44c46",
  "@metamask-previews/error-reporting-service": "2.0.0-preview-b2b44c46",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-b2b44c46",
  "@metamask-previews/foundryup": "1.0.1-preview-b2b44c46",
  "@metamask-previews/gas-fee-controller": "24.0.0-preview-b2b44c46",
  "@metamask-previews/gator-permissions-controller": "0.0.0-preview-b2b44c46",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-b2b44c46",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-b2b44c46",
  "@metamask-previews/keyring-controller": "23.0.0-preview-b2b44c46",
  "@metamask-previews/logging-controller": "6.0.4-preview-b2b44c46",
  "@metamask-previews/message-manager": "12.0.2-preview-b2b44c46",
  "@metamask-previews/messenger": "0.1.0-preview-b2b44c46",
  "@metamask-previews/multichain-account-service": "0.5.0-preview-b2b44c46",
  "@metamask-previews/multichain-api-middleware": "1.0.0-preview-b2b44c46",
  "@metamask-previews/multichain-network-controller": "0.12.0-preview-b2b44c46",
  "@metamask-previews/multichain-transactions-controller": "5.0.0-preview-b2b44c46",
  "@metamask-previews/name-controller": "8.0.3-preview-b2b44c46",
  "@metamask-previews/network-controller": "24.1.0-preview-b2b44c46",
  "@metamask-previews/network-enablement-controller": "0.3.0-preview-b2b44c46",
  "@metamask-previews/notification-services-controller": "17.0.0-preview-b2b44c46",
  "@metamask-previews/permission-controller": "11.0.6-preview-b2b44c46",
  "@metamask-previews/permission-log-controller": "4.0.0-preview-b2b44c46",
  "@metamask-previews/phishing-controller": "13.1.0-preview-b2b44c46",
  "@metamask-previews/polling-controller": "14.0.0-preview-b2b44c46",
  "@metamask-previews/preferences-controller": "19.0.0-preview-b2b44c46",
  "@metamask-previews/profile-sync-controller": "24.0.0-preview-b2b44c46",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-b2b44c46",
  "@metamask-previews/remote-feature-flag-controller": "1.7.0-preview-b2b44c46",
  "@metamask-previews/sample-controllers": "1.0.0-preview-b2b44c46",
  "@metamask-previews/seedless-onboarding-controller": "3.0.0-preview-b2b44c46",
  "@metamask-previews/selected-network-controller": "23.0.0-preview-b2b44c46",
  "@metamask-previews/shield-controller": "0.0.0-preview-b2b44c46",
  "@metamask-previews/signature-controller": "33.0.0-preview-b2b44c46",
  "@metamask-previews/token-search-discovery-controller": "3.3.0-preview-b2b44c46",
  "@metamask-previews/transaction-controller": "60.0.0-preview-b2b44c46",
  "@metamask-previews/user-operation-controller": "39.0.0-preview-b2b44c46"
}

@bergarces
Copy link
Contributor Author

@metamaskbot publish-preview

Copy link
Contributor

Preview builds have been published. See these instructions for more information about preview builds.

Expand for full list of packages and versions.
{
  "@metamask-previews/account-tree-controller": "0.10.0-preview-ddceb3a8",
  "@metamask-previews/accounts-controller": "33.0.0-preview-ddceb3a8",
  "@metamask-previews/address-book-controller": "6.1.1-preview-ddceb3a8",
  "@metamask-previews/announcement-controller": "7.0.3-preview-ddceb3a8",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-ddceb3a8",
  "@metamask-previews/approval-controller": "7.1.3-preview-ddceb3a8",
  "@metamask-previews/assets-controllers": "74.0.0-preview-ddceb3a8",
  "@metamask-previews/base-controller": "8.2.0-preview-ddceb3a8",
  "@metamask-previews/bridge-controller": "41.1.0-preview-ddceb3a8",
  "@metamask-previews/bridge-status-controller": "40.1.0-preview-ddceb3a8",
  "@metamask-previews/build-utils": "3.0.3-preview-ddceb3a8",
  "@metamask-previews/chain-agnostic-permission": "1.1.1-preview-ddceb3a8",
  "@metamask-previews/composable-controller": "11.0.0-preview-ddceb3a8",
  "@metamask-previews/controller-utils": "11.12.0-preview-ddceb3a8",
  "@metamask-previews/delegation-controller": "0.7.0-preview-ddceb3a8",
  "@metamask-previews/earn-controller": "6.0.0-preview-ddceb3a8",
  "@metamask-previews/eip1193-permission-middleware": "1.0.0-preview-ddceb3a8",
  "@metamask-previews/ens-controller": "17.0.1-preview-ddceb3a8",
  "@metamask-previews/error-reporting-service": "2.0.0-preview-ddceb3a8",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-ddceb3a8",
  "@metamask-previews/foundryup": "1.0.1-preview-ddceb3a8",
  "@metamask-previews/gas-fee-controller": "24.0.0-preview-ddceb3a8",
  "@metamask-previews/gator-permissions-controller": "0.0.0-preview-ddceb3a8",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-ddceb3a8",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-ddceb3a8",
  "@metamask-previews/keyring-controller": "23.0.0-preview-ddceb3a8",
  "@metamask-previews/logging-controller": "6.0.4-preview-ddceb3a8",
  "@metamask-previews/message-manager": "12.0.2-preview-ddceb3a8",
  "@metamask-previews/messenger": "0.1.0-preview-ddceb3a8",
  "@metamask-previews/multichain-account-service": "0.5.0-preview-ddceb3a8",
  "@metamask-previews/multichain-api-middleware": "1.0.0-preview-ddceb3a8",
  "@metamask-previews/multichain-network-controller": "0.12.0-preview-ddceb3a8",
  "@metamask-previews/multichain-transactions-controller": "5.0.0-preview-ddceb3a8",
  "@metamask-previews/name-controller": "8.0.3-preview-ddceb3a8",
  "@metamask-previews/network-controller": "24.1.0-preview-ddceb3a8",
  "@metamask-previews/network-enablement-controller": "0.3.0-preview-ddceb3a8",
  "@metamask-previews/notification-services-controller": "17.0.0-preview-ddceb3a8",
  "@metamask-previews/permission-controller": "11.0.6-preview-ddceb3a8",
  "@metamask-previews/permission-log-controller": "4.0.0-preview-ddceb3a8",
  "@metamask-previews/phishing-controller": "13.1.0-preview-ddceb3a8",
  "@metamask-previews/polling-controller": "14.0.0-preview-ddceb3a8",
  "@metamask-previews/preferences-controller": "19.0.0-preview-ddceb3a8",
  "@metamask-previews/profile-sync-controller": "24.0.0-preview-ddceb3a8",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-ddceb3a8",
  "@metamask-previews/remote-feature-flag-controller": "1.7.0-preview-ddceb3a8",
  "@metamask-previews/sample-controllers": "1.0.0-preview-ddceb3a8",
  "@metamask-previews/seedless-onboarding-controller": "3.0.0-preview-ddceb3a8",
  "@metamask-previews/selected-network-controller": "23.0.0-preview-ddceb3a8",
  "@metamask-previews/shield-controller": "0.0.0-preview-ddceb3a8",
  "@metamask-previews/signature-controller": "33.0.0-preview-ddceb3a8",
  "@metamask-previews/token-search-discovery-controller": "3.3.0-preview-ddceb3a8",
  "@metamask-previews/transaction-controller": "60.0.0-preview-ddceb3a8",
  "@metamask-previews/user-operation-controller": "39.0.0-preview-ddceb3a8"
}

chainAccounts,
)) {
const accountGroupId = accountsMap[accountAddress];
const account = accountsMap[accountAddress.toLowerCase()];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toLowerCase() here because accountsByChainId in mobile uses checksumed addresses, unlike every other controller state.

This is only temporary though, because as soon as native addresses are returned in token balances, all this code can go.

@bergarces bergarces merged commit 529a78e into main Aug 22, 2025
231 checks passed
@bergarces bergarces deleted the consistent-address-keys branch August 22, 2025 09:25
@mcmire mcmire mentioned this pull request Aug 22, 2025
4 tasks
micaelae pushed a commit that referenced this pull request Aug 25, 2025
## Explanation

<!--
Thanks for your contribution! Take a moment to answer these questions so
that reviewers have the information they need to properly understand
your changes:

* What is the current state of things and why does it need to change?
* What is the solution your changes offer and how does it work?
* Are there any changes whose purpose might not obvious to those
unfamiliar with the domain?
* If your primary goal was to update one package but you found you had
to update another one along the way, why did you do so?
* If you had to upgrade a dependency, why did you do so?
-->

The format for EVM addresses for AccountTrackerController in mobile
differs from extension and from the other controller states. This was
causing it to miss native account balances.

It also uses the correct internal account type in the assets, so that
BTC account types can be identified.

Finally, it fixes an issue with the `mergeAssets` function mutating
internal parts of cached objects.

## 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

- [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),
highlighting breaking changes as necessary
- [X] I've prepared draft pull requests for clients and consumer
packages to resolve any breaking changes
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.

2 participants