Skip to content

Conversation

@AxelGes
Copy link
Contributor

@AxelGes AxelGes commented Dec 5, 2025

Description

The useBalance hook in the Ramp Aggregator was using selectContractBalances, which returns token balances only for the currently selected network. This caused incorrect behavior.

Solution: Changed the hook to use selectContractBalancesPerChainId and access the correct chain's balances using the asset.chainId parameter that is already passed to the hook.

Changelog

CHANGELOG entry: Fixed incorrect token balance check in Ramp when selecting tokens from non-active networks

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TRAM-2875?atlOrigin=eyJpIjoiNDE5YjQ4YjQ3NGRkNGUzMGFkODUxNjI0M2QxMzlkNjUiLCJwIjoiaiJ9

Fixes #23722

Manual testing steps

Feature: Ramp token balance check

  Scenario: user sells USDC on Arbitrum while connected to different network
    Given user has 40 USDC on Arbitrum
    And user's wallet is connected to Mainnet

    When user navigates to Sell flow in Ramp
    And user selects USDC on Arbitrum as the asset to sell
    And user enters an amount less than their balance (e.g. 22)
    Then the "this amount is higher than your balance" error should NOT appear
    And the balance should correctly reflect the Arbitrum USDC balance

Screenshots/Recordings

Before

balanceBN was null for tokens on non-active networks, causing false insufficient balance errors.
(quick amount selector isn't shown because amount is not being detected)
image

After

balanceBN correctly reflects the token balance on the asset's chain regardless of the currently active network.

image

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

Compute token balances from the asset’s chain using per-chain balances, fixing incorrect balances when the active network differs.

  • Ramp Aggregator useBalance (app/components/UI/Ramp/Aggregator/hooks/useBalance.ts):
    • Switch to selectContractBalancesPerChainId and index by the asset’s chainId (via toHex) to retrieve correct per-chain ERC-20 balances.
    • Update balance computations (balance, balanceBN, balanceFiat) to use chain-scoped balances and existing exchange rates.
    • Ensure native balance lookup uses accountsByChainId[hexChainId] guard for the asset’s chain.

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

@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

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.

@metamaskbot metamaskbot added the team-ramp issues related to Ramp features label Dec 5, 2025
@github-actions github-actions bot added the size-S label Dec 5, 2025
@AxelGes AxelGes marked this pull request as ready for review December 5, 2025 14:33
@AxelGes AxelGes requested a review from a team as a code owner December 5, 2025 14:33
@AxelGes
Copy link
Contributor Author

AxelGes commented Dec 5, 2025

bugbot run

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no bugs!


@AxelGes AxelGes changed the title fix(ramps): use asset chainId to fetch correct token balance in useBalance fix(ramps): use asset chainId to fetch correct token balance in useBalance cp-7.61.0 Dec 5, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 5, 2025

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeRamps
  • Risk Level: medium
  • AI Confidence: 90%
click to see 🤖 AI reasoning details

The change modifies the useBalance hook in app/components/UI/Ramp/Aggregator/hooks/useBalance.ts. The modification switches from using selectContractBalances (which returns balances only for the current chain) to selectContractBalancesPerChainId (which returns all balances organized by chain ID). The hook then uses the asset.chainId parameter to look up the correct chain's balances.

This is a targeted improvement that makes the balance lookup chain-aware in the Ramp/Aggregator feature. The hook is used in:

  1. BuildQuote.tsx - The main view for building buy/sell quotes in the Ramp feature
  2. ConfirmAddAsset.tsx - For confirming asset additions

The change is specific to the on/off ramp (buy/sell) functionality and should be tested with SmokeRamps to verify that:

  • Balance display works correctly when building quotes
  • Multi-chain balance lookups work as expected
  • The ramp feature functions correctly with this balance selector change

The risk is medium because while the change is well-contained, it affects an important financial feature (displaying user balances during buy/sell operations).

View GitHub Actions results

@sonarqubecloud
Copy link

sonarqubecloud bot commented Dec 5, 2025

@AxelGes AxelGes added this pull request to the merge queue Dec 5, 2025
Merged via the queue into main with commit cb0a747 Dec 5, 2025
89 checks passed
@AxelGes AxelGes deleted the fix/ramp-usebalance-wrong-chain-token-balance branch December 5, 2025 16:58
@github-actions github-actions bot locked and limited conversation to collaborators Dec 5, 2025
@metamaskbot metamaskbot added the release-7.62.0 Issue or pull request that will be included in release 7.62.0 label Dec 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.62.0 Issue or pull request that will be included in release 7.62.0 size-S team-ramp issues related to Ramp features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Ramp Sell flow shows incorrect token balance for non-active networks

4 participants