Skip to content

Conversation

@vinnyhoward
Copy link
Contributor

@vinnyhoward vinnyhoward commented Jan 8, 2026

Explanation

Current State

The TokenDetectionController#detectTokensUsingRpc method processes blockchain networks sequentially using a for...await loop. When multiple networks are enabled (e.g., 20 networks in MetaMask Mobile), each chain must complete token detection before the next chain can start. This creates a severe performance bottleneck:

  • 20 healthy networks (2s each): 40 seconds total
  • 20 networks with 1 slow RPC (30s timeout): 68 seconds total
  • 20 networks with multiple slow RPCs: 100+ seconds

This makes the app feel frozen and causes users to think it has crashed.

Solution

This PR refactors #detectTokensUsingRpc to execute all chains in parallel:

  1. Replace sequential loop: Changed from for...await loop to Promise.allSettled() with parallel execution
  2. Improve error isolation: Use allSettled instead of all so one failing chain doesn't block successful ones
  3. Maintain per-chain logic: Token detection within each chain remains unchanged (already parallelized)

Performance Impact

  • 20× faster with multiple networks (100s → 5s)
  • Tokens from fast networks appear immediately
  • Slow/failed chains don't block the entire detection process
  • Better user experience: progressive token display instead of long freeze

Implementation Details

The change follows the same pattern already used successfully in:

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

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