chore: update assets controllers for tokensList#29743
Conversation
## Explanation This PR decouples the `TokenDetectionController` and `TokensController` from the `TokenListController` by introducing a new shared `TokenListService`. This architectural improvement provides: - **Reduced coupling**: Controllers no longer depend on `TokenListController` state/messaging - **Enhanced caching**: Token lists are cached in-memory per chain for 4 hours using TanStack Query - **Better performance**: Optimized token list fetching with proper caching and deduplication - **Simplified data flow**: Token enrichment moves from reactive events to one-time initialization ### Key Changes #### New `TokenListService` - Introduces a TanStack Query-backed service for fetching and caching token lists - Provides 4-hour in-memory caching per chain ID - Exports `TokenListService` class and `buildTokenListMap` utility function - Adds `@tanstack/query-core` as a new dependency #### Breaking Changes to Controllers - **`TokenDetectionController`**: Now requires `tokenListService` in constructor options - **`TokensController`**: Now requires `tokenListService` in constructor options - Both controllers are decoupled from `TokenListController` messaging system - `TokenDetectionController` no longer automatically restarts detection on `TokenListController:stateChange` #### Token Detection Improvements - Token list metadata fetched per detection pass with address normalization - Enhanced error handling with failure-safe early returns - Improved mUSD deduplication and websocket/polling guards #### Token Enrichment Changes - `TokensController` switches from reactive events to one-time async enrichment at initialization - Multi-chain enrichment using `Promise.allSettled` for resilience - Token `name` and `rwaData` enriched once during initialization vs. on every state change ### Testing Updates - Updated tests for new architecture - Added comprehensive `TokenListService` unit tests - Enhanced token detection test coverage **Note**: This introduces breaking changes requiring constructor updates for both controllers. Consumers will need to provide the new `tokenListService` dependency. <!-- 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? --> ## References Mobile: MetaMask/metamask-mobile#29743 <!-- 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/processes/updating-changelogs.md) - [ ] I've introduced [breaking changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md) in this PR and have prepared draft pull requests for clients and consumer packages to resolve them <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Introduces a new shared caching layer and changes `TokenDetectionController`/`TokensController` construction and runtime behavior, which can impact token discovery/enrichment if consumers don’t wire `tokenListService` correctly or if cache/normalization assumptions differ across chains. > > **Overview** > Adds a new `TokenListService` (TanStack Query-backed) to fetch + cache per-chain token lists in-memory for 4 hours, exporting both `TokenListService` and `buildTokenListMap`, and adds `@tanstack/query-core` as a direct dependency. > > **BREAKING:** `TokenDetectionController` and `TokensController` now require a `tokenListService` constructor option and no longer depend on `TokenListController` actions/events; token detection now pulls fresh token-list snapshots per chain from the service (with lowercase-key normalization and graceful early-return on fetch failures) and no longer restarts on `TokenListController:stateChange`. > > Changes token metadata enrichment in `TokensController` from reactive updates on token-list state changes to a one-time async initialization pass (multi-chain, `Promise.allSettled`) that fills `name`/`rwaData`, and updates tests/changelog accordingly (including new `TokenListService` unit tests and expanded mUSD/detection guard coverage). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit c67b9ef. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #29743 +/- ##
==========================================
+ Coverage 81.86% 81.98% +0.12%
==========================================
Files 5255 5301 +46
Lines 138980 140622 +1642
Branches 31518 32005 +487
==========================================
+ Hits 113774 115290 +1516
- Misses 17465 17496 +31
- Partials 7741 7836 +95 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit a20f9b0. Configure here.
|
No dependency changes detected. Learn more about Socket for GitHub. 👍 No dependency changes detected in pull request |
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection: Performance Test Selection: |
|




Description
Update assets controllers for tokensList
Changelog
CHANGELOG entry: update assets controllers for tokensList
Related issues
Fixes: https://consensyssoftware.atlassian.net/browse/ASSETS-3171
Manual testing steps
Screenshots/Recordings
Before
After
Pre-merge author checklist
Performance checks (if applicable)
trace()for usage andaddTokenfor an exampleFor performance guidelines and tooling, see the Performance Guide.
Pre-merge reviewer checklist
Note
Medium Risk
Moderate risk because it changes initialization and lifecycle of token-related controllers and introduces a shared in-memory/cached token list service, which could affect token detection/list fetching behavior or teardown semantics.
Overview
Introduces a shared
TokenListServiceinstance inEngineand wires it into bothTokensControllerandTokenDetectionControllerinitialization so they share a single token-list cache and avoid redundant requests.Updates controller init typings/tests to require
tokenListService, removesTokenListControllermessenger dependencies from the token controllers’ messengers, and ensures the service is explicitlydestroy()ed when tearing down the engine.Bumps
@metamask/assets-controllersto^106.0.0to pick up the new service API.Reviewed by Cursor Bugbot for commit f88fd7e. Bugbot is set up for automated code reviews on this repo. Configure here.