Commit dbad6ed
chore(runway): cherry-pick fix(perps): cp-7.59.0 exclude P&L from total margin calculation in close all positions (#22451)
- fix(perps): exclude P&L from total margin calculation in close all
positions (#22391)
<!--
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 a calculation error in the "Close All Positions" feature
for Perps. Previously, the total margin calculation incorrectly included
unrealized P&L (profit and loss), which led to incorrect amount
calculations when closing all positions.
**What is the reason for the change?**
The total margin should represent only the actual margin used in
positions, not the combined value of margin + P&L. Including P&L in the
margin calculation resulted in incorrect receive amounts being displayed
to users.
## **Changelog**
CHANGELOG entry: Fixed margin calculation in Close All Positions to
exclude P&L
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2030
## **Manual testing steps**
```gherkin
Feature: Close All Positions margin calculation
Scenario: user closes all positions with positive P&L
Given user has multiple open positions with positive unrealized P&L
And user navigates to Close All Positions view
When user views the close summary
Then the total margin displayed should only include marginUsed (excluding P&L)
And the receive amount should equal total margin minus fees
And P&L should be displayed separately
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<img width="1170" height="2532" alt="Simulator Screenshot - iPhone 16e -
2025-11-10 at 12 16 24"
src="https://github.com/user-attachments/assets/0f566135-2acc-46fa-89ec-06db62139f31"
/>
### **After**
<img width="1170" height="2532" alt="Simulator Screenshot - iPhone 16e -
2025-11-10 at 12 11 04"
src="https://github.com/user-attachments/assets/0f1762ac-0da3-45b7-80a8-d0d18b0c450b"
/>
## **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]
> Exclude P&L from total margin in close-all calculations, compute
receive amount as margin minus fees, and update tests accordingly.
>
> - **Perps close-all calculations (`usePerpsCloseAllCalculations`)**:
> - `totalMargin` now excludes `unrealizedPnl`; `totalPnl` calculated
separately.
> - `receiveAmount` = `totalMargin` − `totalFees` (no P&L mixing).
> - JSDoc updated to reflect margin definition; use `Number.parseFloat`
for parsing.
> - **Tests (`usePerpsCloseAllCalculations.test.ts`)**:
> - Update expectations to exclude P&L from `totalMargin` and verify
`totalPnl` separately.
> - Add/adjust cases for positive/negative/zero P&L, multiple positions,
and fee scenarios (e.g., fees equal margin → receive amount 0).
> - Verify fee aggregation, discounts, averages, and points remain
consistent with new margin logic.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
1504819. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Nicholas Smith <nick.smith@consensys.net>
[ecf1712](ecf1712)
Co-authored-by: Michal Szorad <michal.szorad@consensys.net>
Co-authored-by: Nicholas Smith <nick.smith@consensys.net>
Co-authored-by: João Loureiro <175489935+joaoloureirop@users.noreply.github.com>1 parent 791afc4 commit dbad6ed
2 files changed
Lines changed: 101 additions & 14 deletions
File tree
- app/components/UI/Perps/hooks
Lines changed: 96 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
148 | | - | |
| 148 | + | |
149 | 149 | | |
150 | 150 | | |
151 | 151 | | |
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
165 | 165 | | |
166 | 166 | | |
167 | 167 | | |
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
| 192 | + | |
193 | 193 | | |
194 | 194 | | |
195 | | - | |
| 195 | + | |
196 | 196 | | |
197 | 197 | | |
198 | 198 | | |
| |||
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
211 | | - | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
212 | 252 | | |
213 | 253 | | |
214 | 254 | | |
| |||
778 | 818 | | |
779 | 819 | | |
780 | 820 | | |
781 | | - | |
| 821 | + | |
| 822 | + | |
| 823 | + | |
782 | 824 | | |
783 | 825 | | |
784 | | - | |
| 826 | + | |
785 | 827 | | |
786 | 828 | | |
787 | 829 | | |
| |||
807 | 849 | | |
808 | 850 | | |
809 | 851 | | |
810 | | - | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
| 855 | + | |
| 856 | + | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
| 863 | + | |
| 864 | + | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
| 876 | + | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
811 | 899 | | |
812 | 900 | | |
813 | 901 | | |
| |||
Lines changed: 5 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
123 | | - | |
| 123 | + | |
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
128 | | - | |
129 | | - | |
| 127 | + | |
| 128 | + | |
130 | 129 | | |
131 | 130 | | |
132 | 131 | | |
| |||
135 | 134 | | |
136 | 135 | | |
137 | 136 | | |
138 | | - | |
| 137 | + | |
139 | 138 | | |
140 | 139 | | |
141 | 140 | | |
| |||
0 commit comments