Commit 2ef73f3
authored
fix: cp-8.3.0 convert pay-flow token amounts to USD instead of relabeling (#33321)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until this PR meets the canonical
Definition of Ready For Review in `docs/readme/ready-for-review.md`.
-->
## **Description**
<!-- mms-check: type=text required=true -->
Follow-up to #32631.
That PR fixed the *label* on pay-flow token amounts. It didn't fix the
*value*: `asset.fiat.balance` comes from the assets-controller in the
user's preferred currency (e.g. EUR), and `Intl.NumberFormat({ currency:
'USD' })` only changes the symbol — it doesn't convert. Result:
EUR-preferred users saw their EUR magnitude next to a `$` symbol.
This PR converts the value instead of relabeling it, and encapsulates
the pay-currency policy in a new hook.
**Changes**
- New `useAssetFiatFormatter` hook in
[`hooks/pay/`](/app/components/Views/confirmations/hooks/pay/). Owns:
reading pay currency, reading currency rates, re-scaling by
`(native→USD) / (native→preferred)`, Intl formatting. Returns
`undefined` when the target currency can't be safely computed (missing
rate, missing chain config).
- `useAccountTokens` becomes currency-agnostic. Drops all pay-flow
imports; calls `useAssetFiatFormatter` and forwards its output to
`balanceInSelectedCurrency`. Public API and all 9 consumers unchanged.
- Zero balances short-circuit the conversion (0 is currency-invariant)
so the enrichment `$0` placeholder always renders.
## **Changelog**
<!-- mms-check: type=changelog required=true blocking=true -->
CHANGELOG entry: Fixed a bug that caused pay-flow token amounts (Perps,
Predict, Money Account, mUSD) to be shown with a USD symbol but the
user's preferred-currency numeric value, when the user's preferred
currency was not USD.
## **Related issues**
<!-- mms-check: type=issue-link required=true -->
Fixes: N/A (follow-up to #32631)
Refs: #32631
## **Manual testing steps**
<!-- mms-check: type=manual-testing required=true -->
```gherkin
Feature: Pay-flow confirmations convert token amounts to USD, not just relabel
Background:
Given the user has set their preferred fiat currency to EUR
Scenario: Pay With modal shows USD-converted numeric values
Given the user opens a Perps / Predict / Money / mUSD conversion confirmation
When the user opens the "Pay with" modal
Then every row shows a "$" prefix
And the number is the USD equivalent (not the EUR magnitude)
Scenario: musdClaim keeps the user's preferred currency
Given the user opens an mUSD claim confirmation
Then token amounts render in EUR
Scenario: Regular Send flow is unaffected
Given the user opens the regular Send flow
Then token amounts render in EUR
Scenario: Preferred currency already USD
Given the user has set their preferred currency to USD
When the user opens any pay-flow confirmation
Then amounts render exactly as before this PR
Scenario: Missing USD rate for a chain
Given a token on a chain whose usdConversionRate has not loaded
When the user opens a pay-flow confirmation
Then that row shows no fiat value (rather than a wrong-currency value)
```
## **Screenshots/Recordings**
<!-- mms-check: type=screenshot required=true -->
### **Before**
N/A — visible symptom is the `$` prefix on a preferred-currency numeric
value. Device recording will be attached with QA build.
### **After**
https://github.com/user-attachments/assets/7913f4e7-5e91-4e07-ba78-965a26836482
## **Pre-merge author checklist**
<!-- mms-check: type=checklist required=true -->
- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I've included tests if applicable
- [x] I've documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I've applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
#### Performance checks (if applicable)
- [ ] I've tested on Android
- [ ] I've tested with a power user scenario
- [ ] I've instrumented key operations with Sentry traces for production
performance metrics
## **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.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Changes how fiat amounts are computed and sorted in send/pay token
lists; wrong rates or eligibility bugs could show incorrect USD values
or hide balances, though missing-rate cases are explicitly guarded.
>
> **Overview**
> Fixes pay-flow token rows showing a **USD symbol** while still using
the **preferred-currency magnitude** from `asset.fiat.balance` by
**computing EVM fiat as `balance × rate`** (via `useTokenFiatRates` with
the active display currency) instead of formatting the assets-controller
fiat field directly.
>
> Adds **`useAssetFiatFormatter`**, which picks **pay-flow USD** vs
**user preferred currency** (through `useTransactionPayCurrency`) and
handles **Intl currency formatting** only; rate selection stays in
`useTokenFiatRates`.
>
> **`useAccountTokens`** delegates display formatting to that hook,
builds rate requests only for **EVM hex-address** assets (non-EVM still
uses `fiat.balance`), sorts by the **same derived fiat** shown in the
UI, and keeps **$0** visible for zero balances when rates are missing
while **hiding** non-zero rows without a rate.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
1ad7b83. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent dc305bf commit 2ef73f3
4 files changed
Lines changed: 588 additions & 172 deletions
File tree
- app/components/Views/confirmations/hooks
- pay
- send
Lines changed: 145 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
Lines changed: 60 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
0 commit comments