Skip to content

fix: remove usage current network from tokens controller#5659

Merged
salimtb merged 5 commits into
mainfrom
fix/remove-current-network-tokens-controller
Apr 25, 2025
Merged

fix: remove usage current network from tokens controller#5659
salimtb merged 5 commits into
mainfrom
fix/remove-current-network-tokens-controller

Conversation

@salimtb

@salimtb salimtb commented Apr 16, 2025

Copy link
Copy Markdown
Contributor

Explanation

The current implementation of TokensController relies heavily on a single selected network (chainId) as an instance property, which makes multi-chain token handling brittle and hard to scale. Additionally, some token operations unintentionally assume a persistent network context, introducing potential bugs when switching networks or performing actions concurrently across different chains.

This PR refactors the TokensController to eliminate reliance on an internal #chainId property. Instead, all methods that require a network context now accept a networkClientId as an explicit parameter. This improves the controller’s ability to operate safely and predictably in a multi-chain environment.

The #onNetworkDidChange logic and associated subscription were removed since network changes are now handled at the method level with explicit networkClientId parameters. All relevant internal state mutations and token metadata fetches are scoped to the appropriate chain via the networkClientId -> chainId mapping.

This refactor is part of a broader initiative to support simultaneous multi-chain state handling across the codebase (see recent changes to TokenRatesController and AccountTrackerController).

integration with UI:
extension: MetaMask/metamask-extension#32274
mobile: MetaMask/metamask-mobile#14913

References

Changelog

@metamask/assets-controllers

  • UPDATE: Refactor TokensController to support handling multiple chains in parallel. All network-dependent methods now require an explicit networkClientId parameter, enabling token operations to be fully scoped to a specific chain. This improves reliability and predictability when managing tokens across different networks.
  • BREAKING: Remove internal reliance on a single chainId instance property in TokensController. All chain-specific logic has been externalized. Consumers must now explicitly pass a networkClientId to methods that interact with the token state. This change may require updates on the client side to ensure correct chain context is provided during token actions.

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

@salimtb salimtb force-pushed the fix/remove-current-network-tokens-controller branch from f457cdf to 530f9e5 Compare April 24, 2025 22:57
@salimtb salimtb marked this pull request as ready for review April 24, 2025 23:00
@salimtb salimtb requested review from a team as code owners April 24, 2025 23:00
@salimtb

salimtb commented Apr 24, 2025

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
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/accounts-controller": "27.0.0-preview-530f9e5",
  "@metamask-previews/address-book-controller": "6.0.3-preview-530f9e5",
  "@metamask-previews/announcement-controller": "7.0.3-preview-530f9e5",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-530f9e5",
  "@metamask-previews/approval-controller": "7.1.3-preview-530f9e5",
  "@metamask-previews/assets-controllers": "58.0.0-preview-530f9e5",
  "@metamask-previews/base-controller": "8.0.0-preview-530f9e5",
  "@metamask-previews/bridge-controller": "17.0.0-preview-530f9e5",
  "@metamask-previews/bridge-status-controller": "14.0.0-preview-530f9e5",
  "@metamask-previews/build-utils": "3.0.3-preview-530f9e5",
  "@metamask-previews/chain-agnostic-permission": "0.4.0-preview-530f9e5",
  "@metamask-previews/composable-controller": "11.0.0-preview-530f9e5",
  "@metamask-previews/controller-utils": "11.7.0-preview-530f9e5",
  "@metamask-previews/delegation-controller": "0.1.0-preview-530f9e5",
  "@metamask-previews/earn-controller": "0.12.0-preview-530f9e5",
  "@metamask-previews/eip1193-permission-middleware": "0.1.0-preview-530f9e5",
  "@metamask-previews/ens-controller": "16.0.0-preview-530f9e5",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-530f9e5",
  "@metamask-previews/gas-fee-controller": "23.0.0-preview-530f9e5",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-530f9e5",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-530f9e5",
  "@metamask-previews/keyring-controller": "21.0.3-preview-530f9e5",
  "@metamask-previews/logging-controller": "6.0.4-preview-530f9e5",
  "@metamask-previews/message-manager": "12.0.1-preview-530f9e5",
  "@metamask-previews/multichain": "4.0.0-preview-530f9e5",
  "@metamask-previews/multichain-api-middleware": "0.2.0-preview-530f9e5",
  "@metamask-previews/multichain-network-controller": "0.5.1-preview-530f9e5",
  "@metamask-previews/multichain-transactions-controller": "0.9.0-preview-530f9e5",
  "@metamask-previews/name-controller": "8.0.3-preview-530f9e5",
  "@metamask-previews/network-controller": "23.2.0-preview-530f9e5",
  "@metamask-previews/notification-services-controller": "6.0.0-preview-530f9e5",
  "@metamask-previews/permission-controller": "11.0.6-preview-530f9e5",
  "@metamask-previews/permission-log-controller": "3.0.3-preview-530f9e5",
  "@metamask-previews/phishing-controller": "12.5.0-preview-530f9e5",
  "@metamask-previews/polling-controller": "13.0.0-preview-530f9e5",
  "@metamask-previews/preferences-controller": "17.0.0-preview-530f9e5",
  "@metamask-previews/profile-sync-controller": "12.0.0-preview-530f9e5",
  "@metamask-previews/queued-request-controller": "10.0.0-preview-530f9e5",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-530f9e5",
  "@metamask-previews/remote-feature-flag-controller": "1.6.0-preview-530f9e5",
  "@metamask-previews/sample-controllers": "0.1.0-preview-530f9e5",
  "@metamask-previews/selected-network-controller": "22.0.0-preview-530f9e5",
  "@metamask-previews/signature-controller": "27.1.0-preview-530f9e5",
  "@metamask-previews/token-search-discovery-controller": "3.1.0-preview-530f9e5",
  "@metamask-previews/transaction-controller": "54.2.0-preview-530f9e5",
  "@metamask-previews/user-operation-controller": "33.0.0-preview-530f9e5"
}

Comment thread packages/assets-controllers/src/TokensController.ts Outdated

@cryptodev-2s cryptodev-2s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

Comment thread packages/assets-controllers/CHANGELOG.md Outdated
Comment thread packages/assets-controllers/CHANGELOG.md Outdated
@salimtb salimtb force-pushed the fix/remove-current-network-tokens-controller branch from f8813f3 to 83a6970 Compare April 25, 2025 16:47
@salimtb salimtb requested a review from cryptodev-2s April 25, 2025 16:48

@cryptodev-2s cryptodev-2s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@salimtb

salimtb commented Apr 25, 2025

Copy link
Copy Markdown
Contributor Author

@metamaskbot publish-preview

@github-actions

Copy link
Copy Markdown
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/accounts-controller": "27.0.0-preview-83a6970d",
  "@metamask-previews/address-book-controller": "6.0.3-preview-83a6970d",
  "@metamask-previews/announcement-controller": "7.0.3-preview-83a6970d",
  "@metamask-previews/app-metadata-controller": "1.0.0-preview-83a6970d",
  "@metamask-previews/approval-controller": "7.1.3-preview-83a6970d",
  "@metamask-previews/assets-controllers": "59.0.0-preview-83a6970d",
  "@metamask-previews/base-controller": "8.0.0-preview-83a6970d",
  "@metamask-previews/bridge-controller": "18.0.0-preview-83a6970d",
  "@metamask-previews/bridge-status-controller": "15.0.0-preview-83a6970d",
  "@metamask-previews/build-utils": "3.0.3-preview-83a6970d",
  "@metamask-previews/chain-agnostic-permission": "0.4.0-preview-83a6970d",
  "@metamask-previews/composable-controller": "11.0.0-preview-83a6970d",
  "@metamask-previews/controller-utils": "11.7.0-preview-83a6970d",
  "@metamask-previews/delegation-controller": "0.1.0-preview-83a6970d",
  "@metamask-previews/earn-controller": "0.12.0-preview-83a6970d",
  "@metamask-previews/eip1193-permission-middleware": "0.1.0-preview-83a6970d",
  "@metamask-previews/ens-controller": "16.0.0-preview-83a6970d",
  "@metamask-previews/eth-json-rpc-provider": "4.1.8-preview-83a6970d",
  "@metamask-previews/gas-fee-controller": "23.0.0-preview-83a6970d",
  "@metamask-previews/json-rpc-engine": "10.0.3-preview-83a6970d",
  "@metamask-previews/json-rpc-middleware-stream": "8.0.7-preview-83a6970d",
  "@metamask-previews/keyring-controller": "21.0.4-preview-83a6970d",
  "@metamask-previews/logging-controller": "6.0.4-preview-83a6970d",
  "@metamask-previews/message-manager": "12.0.1-preview-83a6970d",
  "@metamask-previews/multichain": "4.0.0-preview-83a6970d",
  "@metamask-previews/multichain-api-middleware": "0.2.0-preview-83a6970d",
  "@metamask-previews/multichain-network-controller": "0.5.1-preview-83a6970d",
  "@metamask-previews/multichain-transactions-controller": "0.9.0-preview-83a6970d",
  "@metamask-previews/name-controller": "8.0.3-preview-83a6970d",
  "@metamask-previews/network-controller": "23.2.0-preview-83a6970d",
  "@metamask-previews/notification-services-controller": "6.0.0-preview-83a6970d",
  "@metamask-previews/permission-controller": "11.0.6-preview-83a6970d",
  "@metamask-previews/permission-log-controller": "3.0.3-preview-83a6970d",
  "@metamask-previews/phishing-controller": "12.5.0-preview-83a6970d",
  "@metamask-previews/polling-controller": "13.0.0-preview-83a6970d",
  "@metamask-previews/preferences-controller": "17.0.0-preview-83a6970d",
  "@metamask-previews/profile-sync-controller": "12.0.0-preview-83a6970d",
  "@metamask-previews/queued-request-controller": "10.0.0-preview-83a6970d",
  "@metamask-previews/rate-limit-controller": "6.0.3-preview-83a6970d",
  "@metamask-previews/remote-feature-flag-controller": "1.6.0-preview-83a6970d",
  "@metamask-previews/sample-controllers": "0.1.0-preview-83a6970d",
  "@metamask-previews/selected-network-controller": "22.0.0-preview-83a6970d",
  "@metamask-previews/signature-controller": "27.1.0-preview-83a6970d",
  "@metamask-previews/token-search-discovery-controller": "3.1.0-preview-83a6970d",
  "@metamask-previews/transaction-controller": "54.2.0-preview-83a6970d",
  "@metamask-previews/user-operation-controller": "33.0.0-preview-83a6970d"
}

@salimtb salimtb enabled auto-merge (squash) April 25, 2025 20:08
@salimtb salimtb disabled auto-merge April 25, 2025 20:08
@salimtb salimtb merged commit 48e81ce into main Apr 25, 2025
@salimtb salimtb deleted the fix/remove-current-network-tokens-controller branch April 25, 2025 21:58
github-merge-queue Bot pushed a commit to MetaMask/metamask-extension that referenced this pull request May 1, 2025
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

This update upgrades the asset controllers to version 60, which
introduces important changes to the `TokensController`:

- Refactored `TokensController` to eliminate reliance on a single
selected network ([#5659](MetaMask/core#5659)).
- `TokensController` methods now require `networkClientId` to be
explicitly passed as a parameter.
- Token management logic is now fully parameterized by `chainId`,
enabling multi-chain token handling and improving reliability across
network changes.
- Internal state updates and token metadata fetching are now scoped to
the appropriate `chainId`.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/32321?quickstart=1)

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to MM
2. Test add tokens , hide tokens, and other flows

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/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.

---------

Co-authored-by: sahar-fehri <sahar.fehri@consensys.net>
github-merge-queue Bot pushed a commit to MetaMask/metamask-mobile that referenced this pull request May 6, 2025
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

# Upgrade Asset Controllers to v60

This update upgrades the asset controllers to version 59, which
introduces important changes to the `TokensController`:

- Refactored `TokensController` to eliminate reliance on a single
selected network ([#5659](MetaMask/core#5659)).
- `TokensController` methods now require `networkClientId` to be
explicitly passed as a parameter.
- Token management logic is now fully parameterized by `chainId`,
enabling multi-chain token handling and improving reliability across
network changes.
- Internal state updates and token metadata fetching are now scoped to
the appropriate `chainId`.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to MM wallet
2. Test the assets part ( import token, hide tokens, add tokens ...etc )

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] 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).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] 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.

---------

Co-authored-by: sahar-fehri <sahar.fehri@consensys.net>
theceo1 pushed a commit to theceo1/metamask-mobile that referenced this pull request Jun 24, 2025
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->

## **Description**

# Upgrade Asset Controllers to v60

This update upgrades the asset controllers to version 59, which
introduces important changes to the `TokensController`:

- Refactored `TokensController` to eliminate reliance on a single
selected network ([MetaMask#5659](MetaMask/core#5659)).
- `TokensController` methods now require `networkClientId` to be
explicitly passed as a parameter.
- Token management logic is now fully parameterized by `chainId`,
enabling multi-chain token handling and improving reliability across
network changes.
- Internal state updates and token metadata fetching are now scoped to
the appropriate `chainId`.

<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->

## **Related issues**

Fixes:

## **Manual testing steps**

1. Go to MM wallet
2. Test the assets part ( import token, hide tokens, add tokens ...etc )

## **Screenshots/Recordings**

<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->

### **Before**

<!-- [screenshots/recordings] -->

### **After**

<!-- [screenshots/recordings] -->

## **Pre-merge author checklist**

- [ ] 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).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] 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.

---------

Co-authored-by: sahar-fehri <sahar.fehri@consensys.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GNS] Remove usage of #chainId and current selected network from TokensController

3 participants