Commit e2a7b97
authored
feat: improve token detection (#7408)
## Explanation
Extension UI: MetaMask/metamask-extension#38708
Mobile UI: MetaMask/metamask-mobile#23864
Acceptance Criteria
• Replace all Account API v2 calls with Account API v4 for token
auto-detection.
• Continue using RPC-based detection for chains not supported by Account
API v4.
• If Account API v4 requests fail, fallback to RPC-based token
detection.
• If Account API v4 returns any unprocessed networks, fallback to RPC
detection for those networks as well.
<!--
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
<!--
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)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Replaces token auto-detection via Accounts API v2 with v4, delegating
supported chains to TokenBalancesController, adding RPC fallbacks/new
actions, and improving polling, lock handling, and tests.
>
> - **Breaking/Behavioral Changes**
> - Replace Accounts API v2 with v4 for token auto-detection; RPC-based
detection remains for unsupported chains.
> - `TokenDetectionController.detectTokens()` adds `forceRpc` and is
exposed via `TokenDetectionController:detectTokens`.
> - **TokenDetectionController**
> - Skips Accounts API–supported chains (handled by balances), performs
RPC detection otherwise.
> - Adds `addDetectedTokensViaPolling` and enhances
`addDetectedTokensViaWs`; respects prefs/external services; metrics
tracking.
> - Subscribes to tx confirmed events; refactors polling restart and
state helpers; minor type/utility cleanups.
> - **TokenBalancesController**
> - On Accounts API failure or unprocessed chains, calls
`TokenDetectionController:detectTokens` with `forceRpc: true`.
> - Imports untracked tokens via
`TokenDetectionController:addDetectedTokensViaPolling`.
> - Adds keyring lock/unlock gating (`isActive`), transaction events
handling, per-chain polling grouping/updates, token address filtering,
and robust state normalization.
> - **Accounts API Fetcher**
> - Supports v4 response shapes: handles CAIP `unprocessedNetworks`,
maps to hex; maintains timeout/batching; staked balances handling.
> - **Exports & Docs**
> - Update `index.ts` exports for new actions; changelog documents
breaking changes and new flows.
> - **Tests**
> - Extensive new/updated tests covering RPC fallback, polling behavior,
event handling, lock state, and edge cases.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
10463da. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 1175c01 commit e2a7b97
9 files changed
Lines changed: 3035 additions & 1950 deletions
File tree
- packages/assets-controllers
- src
- multi-chain-accounts-service
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
386 | 386 | | |
387 | 387 | | |
388 | 388 | | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | 389 | | |
446 | 390 | | |
447 | 391 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
19 | 29 | | |
20 | 30 | | |
21 | 31 | | |
| |||
0 commit comments