Commit 7362ef0
authored
feat(perps): Export perps decimal formatters from UI utils to controller (#28538)
## **Description**
Moves six perps decimal formatters out of the mobile UI layer and into
`@metamask/perps-controller` so the extension (and any future consumer)
can import them without duplicating logic.
**What changed:**
- `app/controllers/perps/utils/perpsFormatters.ts` — new canonical home
for `formatPerpsFiat`, `formatPositionSize`, `formatPnl`,
`formatPercentage`, `formatFundingRate`, `PRICE_RANGES_UNIVERSAL`,
`PRICE_RANGES_MINIMAL_VIEW`, `PRICE_THRESHOLD`,
`formatWithSignificantDigits`, and `FiatRangeConfig`
- `FUNDING_RATE_CONFIG` promoted from UI constants to controller
constants (required by `formatFundingRate`)
- `app/components/UI/Perps/utils/formatUtils.ts` — implementations
replaced with re-exports from `@metamask/perps-controller`; no call-site
changes needed
`Intl.NumberFormat` instances are cached via a module-level `Map` (keyed
by serialized options) instead of the mobile-specific
`getIntlNumberFormatter`. This avoids a transitive
`@metamask/snaps-utils` import that crashes Jest when
`@metamask/assets-controllers` is loaded in test environments.
## **Changelog**
CHANGELOG entry: null
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2870
## **Manual testing steps**
```gherkin
Feature: Perps market data rendering after formatter refactor
Scenario: user views BTC market detail
Given the wallet is unlocked
And the perps feature is enabled
When user navigates to the Perps market list
Then markets are displayed with prices
When user selects BTC
Then the BTC market detail screen loads with a formatted price
```
## **Screenshots/Recordings**
### **Before**
Formatters defined in `app/components/UI/Perps/utils/formatUtils.ts` —
not accessible outside mobile.
### **After**
Formatters exported from `@metamask/perps-controller`. Screenshot
captured by validation recipe showing BTC market detail renders
correctly post-refactor.
## **Pre-merge author checklist**
- [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.
## **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.
## **Validation Recipe**
<details>
<summary>recipe.json — automated validation</summary>
```json
{
"title": "Verify perps formatters exported from controller — markets still render",
"description": "Code refactor: formatPerpsFiat, formatPositionSize, formatPnl, formatPercentage, formatFundingRate, PRICE_RANGES_UNIVERSAL, PRICE_RANGES_MINIMAL_VIEW now exported from @metamask/perps-controller. Verifies the app still renders perps market data (uses these formatters) after the move.",
"validate": {
"workflow": {
"pre_conditions": ["wallet.unlocked", "perps.feature_enabled"],
"entry": "check-markets",
"nodes": {
"check-markets": {
"action": "eval_ref",
"ref": "perps/markets",
"assert": {
"operator": "length_gt",
"value": 0
},
"next": "navigate-to-btc"
},
"navigate-to-btc": {
"action": "call",
"ref": "perps/market-discovery",
"params": { "symbol": "BTC" },
"next": "screenshot-evidence"
},
"screenshot-evidence": {
"action": "screenshot",
"filename": "evidence-market-detail.png",
"next": "done"
},
"done": {
"action": "end",
"status": "pass"
}
}
}
}
}
```
Result: **3/3 nodes PASS** — markets loaded, BTC detail rendered with
formatted prices.
</details>1 parent 68bf844 commit 7362ef0
10 files changed
Lines changed: 970 additions & 586 deletions
File tree
- app
- components/UI/Perps
- Views/PerpsHomeView
- constants
- utils
- controllers/perps
- constants
- utils
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
221 | 221 | | |
222 | 222 | | |
223 | 223 | | |
| 224 | + | |
224 | 225 | | |
225 | 226 | | |
226 | 227 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
| 132 | + | |
144 | 133 | | |
145 | 134 | | |
146 | 135 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
571 | 571 | | |
572 | 572 | | |
573 | 573 | | |
574 | | - | |
| 574 | + | |
575 | 575 | | |
576 | 576 | | |
577 | | - | |
| 577 | + | |
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
581 | 581 | | |
582 | 582 | | |
583 | 583 | | |
584 | | - | |
| 584 | + | |
585 | 585 | | |
586 | 586 | | |
587 | | - | |
| 587 | + | |
588 | 588 | | |
589 | 589 | | |
590 | 590 | | |
| |||
1212 | 1212 | | |
1213 | 1213 | | |
1214 | 1214 | | |
1215 | | - | |
| 1215 | + | |
1216 | 1216 | | |
1217 | 1217 | | |
1218 | 1218 | | |
1219 | 1219 | | |
1220 | 1220 | | |
1221 | 1221 | | |
1222 | 1222 | | |
1223 | | - | |
| 1223 | + | |
1224 | 1224 | | |
1225 | 1225 | | |
1226 | 1226 | | |
1227 | 1227 | | |
1228 | 1228 | | |
1229 | 1229 | | |
1230 | 1230 | | |
1231 | | - | |
| 1231 | + | |
1232 | 1232 | | |
1233 | 1233 | | |
1234 | 1234 | | |
| |||
1237 | 1237 | | |
1238 | 1238 | | |
1239 | 1239 | | |
1240 | | - | |
| 1240 | + | |
1241 | 1241 | | |
1242 | 1242 | | |
1243 | 1243 | | |
1244 | 1244 | | |
1245 | 1245 | | |
1246 | 1246 | | |
1247 | 1247 | | |
1248 | | - | |
| 1248 | + | |
1249 | 1249 | | |
1250 | 1250 | | |
1251 | 1251 | | |
| |||
0 commit comments