feat: update useBalance hook to use asset-specific conversion rates#30869
Conversation
…nd add regression tests
|
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. |
🔍 Smart E2E Test Selection
click to see 🤖 AI reasoning detailsE2E Test Selection:
Tag selection rationale:
Performance Test Selection: |
Description
The Sell (off-ramp) flow displayed incorrect fiat balances for the selected asset. For example, a balance of
0.02319BNB worth roughly$14was 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
useBalancehook used byBuildQuote. 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 yields0.02319 × 2328 ≈ 54instead of0.02319 × 600 ≈ 14.The fix reads both rates keyed on the asset's own chainId:
selectConversionRateByChainIdfor the native conversion rate andselectContractExchangeRatesByChainIdfor 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 sharedgetEvmHexChainIdutil in the Ramp Aggregatorutils, and the existinggetHexChainIdFromCryptoCurrencywas refactored to delegate to it — removing a near-duplicate implementation and keeping a single source of truth. Non-EVM and unparseable chainIds resolve toundefined, 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
Screenshots/Recordings
Before
After
Pre-merge author checklist
Performance checks (if applicable)
trace()for usage andaddTokenfor an exampleFor performance guidelines and tooling, see the Performance Guide.
Pre-merge reviewer checklist
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.
useBalancenow resolves the asset’s EVM chain id and loadsselectConversionRateByChainIdandselectContractExchangeRatesByChainIdfor 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
getEvmHexChainIdhelper (hex, decimal, oreip155CAIP);getHexChainIdFromCryptoCurrencydelegates 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.