Skip to content

feat: pre-fetch swap popular tokens#29587

Merged
micaelae merged 35 commits into
mainfrom
swaps4403-prefetch-popular-tokens2
May 11, 2026
Merged

feat: pre-fetch swap popular tokens#29587
micaelae merged 35 commits into
mainfrom
swaps4403-prefetch-popular-tokens2

Conversation

@micaelae

@micaelae micaelae commented Apr 30, 2026

Copy link
Copy Markdown
Member

Description

Changes

  • Fall back to the user's owned assets while usePopularTokens and useSearchTokens fetch the token list, or if the fetch request fails
  • Extract includedAsset filtering from usePopularTokens to useInitialBridgeTokens so that the same logic can be used to pre-fetch the popular tokens during navigation
  • Move types from usePopularTokens to UI/Bridge/types.ts
  • Move cache utils from usePopularTokens to utils/cacheUtils.ts

Changelog

CHANGELOG entry: feat: pre-fetch swap popular tokens

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/SWAPS-4403

Manual testing steps

Feature: token pre-fetching

  Scenario: user opens the wallet for the first time
    Given they have balances

    When user navigates to Swap and expands the asset picker
    Then they instantly see tokens

  Scenario: user searches for a token
    Given they have balances

    When user navigates to Swap and searches for a token in the asset picker
    Then they see matching tokens with balances while the search is loading

Screenshots/Recordings

Before

Screenshot 2026-04-28 at 12 04 28 PM

After

Screenshot 2026-04-28 at 12 36 21 PM Screenshot 2026-04-28 at 12 43 37 PM

Pre-merge author checklist

Performance checks (if applicable)

  • I've tested on Android
    • Ideally on a mid-range device; emulator is acceptable
  • I've tested with a power user scenario
    • Use these power-user SRPs to import wallets with many accounts and tokens
  • I've instrumented key operations with Sentry traces for production performance metrics

For performance guidelines and tooling, see the Performance Guide.

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.

Note

Medium Risk
Adds new token prefetch/caching and changes the swap/bridge token selector data flow, which could affect API load, abort/race behavior, and what tokens users see during loading/failure states.

Overview
Prefetches popular swap/bridge tokens during navigation via useSwapBridgeNavigation, gated by basicFunctionalityEnabled, to reduce empty-state time when opening the token selector.

Refactors token fetching responsibilities by introducing useInitialBridgeTokens (builds includeAssets from owned balances, provides fetchPopularTokens, and centralizes 15-min caching in utils/cacheUtils.ts) and simplifying usePopularTokens to be a thin fetch/abort wrapper.

Improves token selector loading UX by showing owned tokens (and partial results) as placeholders while popular/search requests are pending or fail, and by adjusting search/includeAssets handling to avoid stale results and race conditions.

Type and utility cleanup: moves SecurityData*, PopularToken, and IncludeAsset into UI/Bridge/types.ts, updates call sites/tests accordingly, and enhances getTokenImageSource to derive an icon URL from address/chainId when no image URL is provided.

Reviewed by Cursor Bugbot for commit d7d2fbb. Bugbot is set up for automated code reviews on this repo. Configure here.

@metamaskbotv2 metamaskbotv2 Bot added the team-swaps-and-bridge Swaps and Bridge team label Apr 30, 2026
@github-actions github-actions Bot added size-XL and removed size-L labels May 1, 2026
@micaelae micaelae force-pushed the swaps4403-prefetch-popular-tokens2 branch from 94b916d to 304a891 Compare May 5, 2026 00:23
@micaelae micaelae marked this pull request as ready for review May 6, 2026 15:22
davibroc
davibroc previously approved these changes May 7, 2026
Comment thread app/components/UI/Bridge/hooks/useSearchTokens.ts
Comment thread app/components/UI/Bridge/hooks/useInitialBridgeTokens.ts Outdated
Comment thread tests/framework/fixtures/FixtureHelper.ts
@micaelae micaelae added this pull request to the merge queue May 7, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 7, 2026
@micaelae micaelae added this pull request to the merge queue May 8, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks May 8, 2026

@cursor cursor Bot 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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 258018b. Configure here.

Comment thread app/components/UI/Bridge/hooks/useSearchTokens.ts
@github-actions

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeSwap, SmokeConfirmations, SmokeWalletPlatform
  • Selected Performance tags: @PerformanceSwaps
  • Risk Level: medium
  • AI Confidence: 85%
click to see 🤖 AI reasoning details

E2E Test Selection:

The PR introduces significant refactoring of the Bridge/Swap token selector system and test infrastructure changes:

  1. SmokeSwap (primary): The core swap/bridge token selector logic has been substantially refactored:

    • New useInitialBridgeTokens hook handles token fetching with caching (extracted from usePopularTokens)
    • usePopularTokens now accepts a fetchTokens callback instead of managing fetching itself
    • useSearchTokens updated to use typed IncludeAsset[] instead of stringified JSON
    • BridgeTokenSelector now shows tokens with balance while loading (UX improvement)
    • useSwapBridgeNavigation now prefetches popular tokens when navigating to swap
    • Two swap E2E test specs (swap-trending-tokens.spec.ts, unified-ui-wallet-actions.spec.ts) were directly modified to add mockSwapPopularTokens
    • These tests are tagged SmokeSwap
  2. SmokeConfirmations (dependent): Per SmokeSwap tag description, swap flows require confirmations. The swap/bridge flow changes could affect transaction confirmation flows.

  3. SmokeWalletPlatform: The useSwapBridgeNavigation hook (used from wallet actions) now prefetches popular tokens. The walletActions.ts and marketInsightsView.ts test presets were updated with tokenSortConfig. The Wallet.view.test.tsx and index.test.tsx were updated with tokenSortConfig and AuthenticationController.getBearerToken mock. The Trending tab and wallet actions entry points are affected.

  4. Test infrastructure impact: FixtureHelper.ts now auto-mocks popular tokens for ALL tests using withFixtures (with shouldPrefetchSwapTokens=true default). This is a global change that could affect any test navigating to swap/bridge. However, since this is additive (adding a mock that was previously missing), it should fix rather than break tests. The most directly affected tests are SmokeSwap tests.

Not selected:

  • SmokeNetworkAbstractions, SmokeNetworkExpansion, SmokeMultiChainAPI: No changes to network/chain permission logic
  • SmokeAccounts, SmokeIdentity: No account management changes
  • SmokeStake, SmokeMoney, SmokePerps, SmokePredictions: No changes to these flows
  • SmokeBrowser, SmokeSnaps: No browser or snap changes
  • SmokeSeedlessOnboarding: No onboarding changes

Performance Test Selection:
The Bridge/Swap token selector has been significantly refactored with new caching logic (cacheUtils.ts), prefetching behavior in useSwapBridgeNavigation, and changes to how tokens are loaded and displayed during loading states. The new useInitialBridgeTokens hook introduces a fetch-on-navigate pattern that could impact swap flow performance. The @PerformanceSwaps tag covers quote fetching, token selection, and swap execution times, which are directly affected by these token loading changes.

View GitHub Actions results

@sonarqubecloud

Copy link
Copy Markdown

@micaelae micaelae added this pull request to the merge queue May 11, 2026
Merged via the queue into main with commit aab2c60 May 11, 2026
107 of 108 checks passed
@micaelae micaelae deleted the swaps4403-prefetch-popular-tokens2 branch May 11, 2026 21:07
@github-actions github-actions Bot locked and limited conversation to collaborators May 11, 2026
@metamaskbotv2 metamaskbotv2 Bot added the release-7.78.0 Issue or pull request that will be included in release 7.78.0 label May 11, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.78.0 Issue or pull request that will be included in release 7.78.0 size-XL team-swaps-and-bridge Swaps and Bridge team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants