Skip to content

Conversation

@michalconsensys
Copy link
Contributor

@michalconsensys michalconsensys commented Nov 21, 2025

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

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

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

Before

Screenshot_20251121_104805_MetaMask (1)

After

Simulator Screenshot - iPhone 16e - 2025-11-21 at 11 23 26

Pre-merge author checklist

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

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.

Written by Cursor Bugbot for commit 334fcd6. This will update automatically on new commits. Configure here.

@michalconsensys michalconsensys added the team-perps Perps team label Nov 21, 2025
@github-actions
Copy link
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
Copy link
Contributor

🔍 Smart E2E Test Selection

  • Selected E2E tags: SmokeAccounts, SmokeCore, SmokeConfirmationsRedesigned, SmokeIdentity, SmokeNetworkAbstractions, SmokeNetworkExpansion, SmokeTrade, SmokeWalletPlatform, SmokeWalletUX, SmokeAssets, SmokeSwaps, SmokeStake, SmokeCard, SmokeNotifications, SmokeRewards, SmokePerps, SmokeRamps, SmokeMultiChainPermissions, SmokeAnalytics, SmokeMultiChainAPI, SmokePredictions
  • Risk Level: high
  • AI Confidence: %
click to see 🤖 AI reasoning details

Fallback: AI analysis did not complete successfully. Running all tests.

View GitHub Actions results

@michalconsensys michalconsensys marked this pull request as ready for review November 21, 2025 10:35
@michalconsensys michalconsensys requested a review from a team as a code owner November 21, 2025 10:35
@sonarqubecloud
Copy link

@michalconsensys michalconsensys added this pull request to the merge queue Nov 21, 2025
Merged via the queue into main with commit f4e8f8d Nov 21, 2025
102 of 103 checks passed
@michalconsensys michalconsensys deleted the fix/perps/pnl-percent branch November 21, 2025 11:30
@github-actions github-actions bot locked and limited conversation to collaborators Nov 21, 2025
@metamaskbot metamaskbot added the release-7.61.0 Issue or pull request that will be included in release 7.61.0 label Nov 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

release-7.61.0 Issue or pull request that will be included in release 7.61.0 size-M team-perps Perps team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants