Skip to content

fix: MUSD-282 Add useTokensBuyability hook#25539

Merged
Matt561 merged 14 commits into
mainfrom
fix/musd-282-musd-asset-list-cta-is-spamming-ramps-tokens-api-endpoint
Mar 3, 2026
Merged

fix: MUSD-282 Add useTokensBuyability hook#25539
Matt561 merged 14 commits into
mainfrom
fix/musd-282-musd-asset-list-cta-is-spamming-ramps-tokens-api-endpoint

Conversation

@Matt561

@Matt561 Matt561 commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

Description

This PR introduces useTokensBuyability(tokens) to evaluate buyability for multiple tokens in a single hook call, and keeps useTokenBuyability(token) as a backward-compatible convenience wrapper over the batch hook.

useTokensBuyability returns buyability as buyabilityByTokenKey (keyed by normalized chain + token address).

Motivation: reduce redundant legacy ramp token fetches when checking multiple tokens in the same render path. Previously, calling useTokenBuyability N times could trigger repeated legacy useRampTokens mount-driven fetch behavior.

This change also preserves V2 efficiency by passing fetchOnMount: !isV2Enabled, so the legacy fetch is suppressed when unified buy V2 is enabled and controller-backed tokens are used.

Changelog

CHANGELOG entry: refactored Ramp buyability to add batched useTokensBuyability with keyed results and keep useTokenBuyability as a backward-compatible wrapper, reducing redundant legacy token-cache fetches for multi-token checks.

Related issues

Fixes: MUSD-282: mUSD Asset List CTA is spamming the Ramps Tokens API endpoint

Manual testing steps

Feature: Batched Ramp token buyability checks

  Scenario: user views mUSD eligibility
    Given user is on a screen that evaluates multiple token buyability states (e.g. Token List)
    Then the app checks buyability of multiple tokens in one hook call

Screenshots/Recordings

Before

N/A

After

When MM_RAMPS_UNIFIED_BUY_V2_ENABLED="true" useMusdRampAvailability leverages ramp token cache.

Pre-merge author checklist

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
Changes Ramp buyability evaluation and when the legacy token-cache API is fetched; regressions could hide/show buy CTAs incorrectly or delay buyability data if the new fetch gating is misused.

Overview
Introduces useTokensBuyability(tokens) and supporting helpers (e.g., getTokenBuyabilityKey) to compute Ramp buyability for multiple tokens in one hook call, while keeping useTokenBuyability(token) as a backward-compatible wrapper.

Adds a fetchOnMount option to useRampTokens and uses it from buyability logic to skip legacy token-cache fetching when Unified Ramps V2 is enabled, reducing repeated API calls.

Refactors useMusdRampAvailability to derive per-chain mUSD buyability via useTokensBuyability (using mUSD token addresses per chain), and updates/expands tests across Ramp and Earn hooks to cover the new batching and fetch gating behavior.

Written by Cursor Bugbot for commit 437bc20. This will update automatically on new commits. Configure here.

@Matt561 Matt561 added No QA Needed Apply this label when your PR does not need any QA effort. team-earn labels Feb 2, 2026
@github-actions

github-actions Bot commented Feb 2, 2026

Copy link
Copy Markdown
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@github-actions github-actions Bot added the size-M label Feb 2, 2026
@Matt561 Matt561 marked this pull request as ready for review February 2, 2026 19:43
@Matt561 Matt561 requested review from a team as code owners February 2, 2026 19:43
Comment thread app/components/UI/Earn/hooks/useMusdRampAvailability.ts Outdated
Comment thread app/components/UI/Earn/hooks/useMusdRampAvailability.ts Outdated
*/
export function useRampTokens(): UseRampTokensResult {
export function useRampTokens(
{ fetchOnMount }: UseRampTokensOptions = { fetchOnMount: true },

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fetchOnMount: true by default for backwards compatibility.

nickewansmith
nickewansmith previously approved these changes Feb 4, 2026
@Matt561 Matt561 changed the title fix: MUSD-282 updated useMusdRampAvailability to leverage unified buy v2 caching [ON HOLD] fix: MUSD-282 updated useMusdRampAvailability to leverage unified buy v2 caching Feb 5, 2026
@Matt561 Matt561 changed the title [ON HOLD] fix: MUSD-282 updated useMusdRampAvailability to leverage unified buy v2 caching fix: MUSD-282 updated useMusdRampAvailability to leverage unified buy v2 caching Mar 2, 2026
@github-actions github-actions Bot added size-L and removed size-M labels Mar 2, 2026
Comment thread app/components/UI/Ramp/hooks/useTokenBuyability.ts

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

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

export function getTokenBuyabilityKey(token: TokenI): string {
const caipChainId = getTokenCaipChainId(token);
return `${caipChainId}:${token.address.toLowerCase()}`;
}

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.

Buyability keys mismatch across token formats

Low Severity

getTokenBuyabilityKey lowercases and concatenates token.address even when it is already a CAIP asset id, so equivalent tokens represented as CAIP vs hex+chain generate different keys. This can make buyabilityByTokenKey lookups return false negatives when callers mix token representations across useTokensBuyability inputs and reads.

Additional Locations (1)

Fix in Cursor Fix in Web

@Matt561 Matt561 changed the title fix: MUSD-282 updated useMusdRampAvailability to leverage unified buy v2 caching fix: MUSD-282 Add useTokensBuyability hook Mar 3, 2026
@amitabh94

Copy link
Copy Markdown
Contributor

Is it possible to add a small loom video to show how it is working?

@Matt561

Matt561 commented Mar 3, 2026

Copy link
Copy Markdown
Contributor Author

Is it possible to add a small loom video to show how it is working?

Sure thing @amitabh94 - here it is. https://www.loom.com/share/ef9929d2e19743ffba33b2b7a226b0d2

I'm mainly trying to avoid duplicate network requests since the legacy useRampTokens hook currently fetches on mount regardless of if Unified Buy v2 is enabled.

@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeRamps
  • Selected Performance tags: None (no tests recommended)
  • Risk Level: medium
  • AI Confidence: 82%
click to see 🤖 AI reasoning details

E2E Test Selection:
The changes are focused on Ramp-related hooks that determine token buyability:

  1. useTokenBuyability.ts: Refactored to add a new batch hook useTokensBuyability and added getTokenBuyabilityKey helper. The original useTokenBuyability hook is now a wrapper around the batch version. This hook is used in AssetOverviewContent.tsx to determine if tokens can be bought via ramp services.

  2. useRampTokens.ts: Added a fetchOnMount option to control when tokens are fetched. This is a minor enhancement that allows conditional fetching.

  3. useMusdRampAvailability.ts: Refactored to use the new useTokensBuyability hook instead of directly using useRampTokens. This affects mUSD (MetaMask USD) ramp availability checking in Earn flows.

  4. Test files: All test files were updated to reflect the new implementation with comprehensive test coverage.

The changes are internal refactoring that maintain backwards compatibility. The external API of useTokenBuyability remains unchanged. The primary impact is on:

  • Ramp flows (buy/sell crypto) - determining which tokens are buyable
  • Earn/mUSD flows - checking mUSD availability for conversion

SmokeRamps is the appropriate tag as it covers fiat on-ramp (buy crypto) and off-ramp (sell crypto) features, which directly use these hooks. The changes don't affect wallet platform features, confirmations, or other areas significantly enough to warrant additional tags.

Performance Test Selection:
The changes are internal refactoring of hooks that determine token buyability. While there's a new batch hook (useTokensBuyability), it's primarily used for checking mUSD availability across a small fixed set of chains (MUSD_BUYABLE_CHAIN_IDS). The changes don't affect UI rendering performance, list rendering, or critical user flows in a way that would impact performance metrics. The fetchOnMount optimization actually reduces unnecessary API calls when V2 is enabled. No performance tests are needed for these hook refactoring changes.

View GitHub Actions results

@github-actions

github-actions Bot commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

⚠️ E2E Fixture Validation — Structural changes detected

Category Count
New keys 60
Missing keys 0
Type mismatches 0
Value mismatches 7 (informational)

The committed fixture schema is out of date. To update, comment:

@metamaskbot update-mobile-fixture

View full details | Download diff report

@sonarqubecloud

sonarqubecloud Bot commented Mar 3, 2026

Copy link
Copy Markdown

@Matt561 Matt561 requested a review from nickewansmith March 3, 2026 19:42
@Matt561 Matt561 enabled auto-merge March 3, 2026 20:07
@Matt561 Matt561 added this pull request to the merge queue Mar 3, 2026
Merged via the queue into main with commit 9c138df Mar 3, 2026
158 of 161 checks passed
@Matt561 Matt561 deleted the fix/musd-282-musd-asset-list-cta-is-spamming-ramps-tokens-api-endpoint branch March 3, 2026 20:42
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 3, 2026
@metamaskbot metamaskbot added the release-7.69.0 Issue or pull request that will be included in release 7.69.0 label Mar 3, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

No QA Needed Apply this label when your PR does not need any QA effort. release-7.69.0 Issue or pull request that will be included in release 7.69.0 size-L team-earn

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants