Skip to content

feat: update useBalance hook to use asset-specific conversion rates#30869

Merged
meltingice1337 merged 2 commits into
mainfrom
fix/TRAM-3464
Jun 4, 2026
Merged

feat: update useBalance hook to use asset-specific conversion rates#30869
meltingice1337 merged 2 commits into
mainfrom
fix/TRAM-3464

Conversation

@meltingice1337

@meltingice1337 meltingice1337 commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Description

The Sell (off-ramp) flow displayed incorrect fiat balances for the selected asset. For example, a balance of 0.02319 BNB worth roughly $14 was shown as ~54. The wrong value was not a currency-symbol mismatch — the amount itself was off by a large factor regardless of the selected fiat currency.

The root cause is in the useBalance hook used by BuildQuote. It read the native-currency conversion rate (selectConversionRate) and the token exchange rates (selectContractExchangeRates) from the globally selected network rather than from the asset's own chain. In the Sell flow the selected asset can live on a different chain than the active network, so the wrong native rate was applied to the balance — e.g. the Ethereum rate (~2328) applied to a BNB balance yields 0.02319 × 2328 ≈ 54 instead of 0.02319 × 600 ≈ 14.

The fix reads both rates keyed on the asset's own chainId: selectConversionRateByChainId for the native conversion rate and selectContractExchangeRatesByChainId for the token exchange rates. To resolve the asset's chainId (which may be hex, decimal, or CAIP format) to an EVM hex chainId, the logic was extracted into a shared getEvmHexChainId util in the Ramp Aggregator utils, and the existing getHexChainIdFromCryptoCurrency was refactored to delegate to it — removing a near-duplicate implementation and keeping a single source of truth. Non-EVM and unparseable chainIds resolve to undefined, so the previous behavior for those paths is preserved.

Changelog

CHANGELOG entry: Fixed incorrect fiat balances shown in the Sell (off-ramp) flow when the selected asset was on a different network than the globally selected one.

Related issues

Fixes: https://consensyssoftware.atlassian.net/browse/TRAM-3464

Manual testing steps

Feature: Off-ramp fiat balance conversion

  Scenario: user views the balance of an asset on a non-active network in the Sell flow
    Given the globally selected network is Ethereum Mainnet
    And the user holds a balance on a different EVM network (e.g. 0.02319 BNB on BNB Smart Chain)

    When the user opens the Sell flow and selects that asset
    Then the displayed fiat balance is converted using that asset's own native rate (≈ $14)
    And it is no longer computed with the Ethereum rate (which previously showed ≈ 54)

Screenshots/Recordings

Before

qemu-system-x86_64_64RQOmCLgp

After

qemu-system-x86_64_XnK8bpojV4

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
Changes fiat display logic in the Sell flow and rate selector wiring; incorrect chain resolution could still mis-price balances, but scope is limited to Ramp Aggregator with targeted regression tests.

Overview
Fixes Sell (off-ramp) fiat balances when the asset’s chain differs from the globally selected network. useBalance now resolves the asset’s EVM chain id and loads selectConversionRateByChainId and selectContractExchangeRatesByChainId for that chain instead of the global conversion/token rate selectors—so native and ERC-20 fiat amounts use the correct chain’s rates (e.g. BNB @ 600, not ETH @ 2000).

Chain id normalization is centralized in a new getEvmHexChainId helper (hex, decimal, or eip155 CAIP); getHexChainIdFromCryptoCurrency delegates to it. Tests cover multi-chain mock state and TRAM-3464 regressions for native and Polygon ERC-20 fiat math.

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

@meltingice1337 meltingice1337 added the team-money-movement issues related to Money Movement features label Jun 1, 2026
@github-actions

github-actions Bot commented Jun 1, 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 Jun 1, 2026
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

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

E2E Test Selection:
The changes are scoped to the Ramp/Aggregator module:

  1. useBalance.ts (Ramp hook): Bug fix (TRAM-3464) - switches from global network conversion/exchange rates to chain-specific selectors (selectConversionRateByChainId, selectContractExchangeRatesByChainId). This fixes incorrect fiat balance calculations when selling tokens on chains other than the globally selected network. The hook is consumed by BuildQuote.tsx in the Ramp flow.

  2. utils/index.ts: Refactors getHexChainIdFromCryptoCurrency to extract a new getEvmHexChainId helper that handles hex, decimal, and CAIP chain ID formats. This utility is now used by useBalance.ts and is also used elsewhere in the Ramp utils.

  3. Test files: Unit tests added to cover the regression fix and new utility.

Tag selection rationale:

  • SmokeMoney: Directly covers the Ramp buy/sell flows where useBalance is used in BuildQuote.tsx. The bug fix affects fiat balance display in the sell/off-ramp flow, making this the primary tag to validate.
  • No other tags are needed: the changes are isolated to the Ramp/Aggregator module and don't touch confirmations, swaps, staking, or other wallet flows.
  • No performance tests needed: the change is a selector lookup optimization (chain-specific vs global), not a rendering or data loading change that would affect measurable performance metrics.

Performance Test Selection:
The changes are bug fixes to selector usage in the Ramp useBalance hook - switching from global to chain-specific rate selectors. This is a correctness fix, not a performance-impacting change. No UI rendering, list components, or critical flow timing is affected.

View GitHub Actions results

@meltingice1337 meltingice1337 marked this pull request as ready for review June 3, 2026 10:11
@meltingice1337 meltingice1337 requested a review from a team as a code owner June 3, 2026 10:11
@github-actions github-actions Bot added the risk:low AI analysis: low risk label Jun 3, 2026
@meltingice1337 meltingice1337 added this pull request to the merge queue Jun 3, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 3, 2026
@meltingice1337 meltingice1337 added this pull request to the merge queue Jun 4, 2026
Merged via the queue into main with commit e3af1a8 Jun 4, 2026
210 of 236 checks passed
@meltingice1337 meltingice1337 deleted the fix/TRAM-3464 branch June 4, 2026 06:40
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 4, 2026
@metamaskbotv2 metamaskbotv2 Bot added the release-7.81.0 Issue or pull request that will be included in release 7.81.0 label Jun 4, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.81.0 Issue or pull request that will be included in release 7.81.0 risk:low AI analysis: low risk size-M team-money-movement issues related to Money Movement features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants