Commit 407d4c2
committed
chore(runway): cherry-pick fix(perps): calculate weighted ROE percentage for aggregated account states cp-7.60.0 (#23090)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
This PR fixes the calculation of return on equity (ROE) percentage for
aggregated account states across multiple DEX accounts in the Perps
feature. Previously, the ROE was calculated using a simple formula that
didn't account for the different margin amounts across accounts, which
could lead to inaccurate percentage values.
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: Fixed return on equity percentage calculation for
aggregated Perps positions across multiple DEX accounts
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2086
## **Manual testing steps**
```gherkin
Feature: Perps aggregated account ROE calculation
Scenario: user views aggregated account state with multiple DEX positions
Given user has multiple Perps positions across different DEX accounts with varying margin amounts
And each position has different unrealized PnL and ROE values
When user views the aggregated account state
Then the displayed ROE percentage should be a weighted average based on margin used
And the calculation should correctly handle accounts with zero or negative values
And invalid or NaN values should be skipped in the calculation
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->

### **After**
<!-- [screenshots/recordings] -->
<img width="1170" height="2532" alt="Simulator Screenshot - iPhone 16e -
2025-11-21 at 11 23 26"
src="https://github.com/user-attachments/assets/23a33e94-e8b2-4b63-b57a-031fc5276653"
/>
## **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.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Replaces aggregate ROE with a margin-weighted calculation and adds
comprehensive unit tests.
>
> - **Perps utils**:
> - **Weighted ROE**: Add `calculateWeightedReturnOnEquity` (and
`ReturnOnEquityInput`) in
`app/components/UI/Perps/utils/accountUtils.ts` to compute
margin-weighted ROE across accounts.
> - Integrate weighted ROE in
`HyperLiquidSubscriptionService.aggregateAccountStates()` by using
`calculateWeightedReturnOnEquity` instead of the previous simple ratio.
> - **Tests**:
> - Add extensive unit tests in
`app/components/UI/Perps/utils/accountUtils.test.ts` covering mixed
types, edge cases (zero/negative/NaN), precision, and multi-account
scenarios.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
334fcd6. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 394dcb5 commit 407d4c2
File tree
3 files changed
+443
-8
lines changed- app/components/UI/Perps
- services
- utils
3 files changed
+443
-8
lines changedLines changed: 15 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
476 | 477 | | |
477 | 478 | | |
478 | 479 | | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
479 | 486 | | |
480 | 487 | | |
481 | 488 | | |
| |||
488 | 495 | | |
489 | 496 | | |
490 | 497 | | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
491 | 504 | | |
492 | 505 | | |
493 | 506 | | |
494 | 507 | | |
495 | 508 | | |
496 | 509 | | |
497 | 510 | | |
498 | | - | |
499 | | - | |
500 | | - | |
501 | | - | |
502 | | - | |
503 | | - | |
504 | | - | |
| 511 | + | |
| 512 | + | |
505 | 513 | | |
506 | 514 | | |
507 | 515 | | |
| |||
0 commit comments