Commit e734f45
chore(runway): cherry-pick fix: cp-7.59.0 hotfix-7.58.2 update close position calculation with funding fees and live data (#22333)
- fix: cp-7.59.0 hotfix-7.58.2 update close position calculation with
funding fees and live data (#22229)
<!--
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**
Fixes bug where close position view showed incorrect receive amounts,
sometimes displaying negative values when position value had dropped.
Root causes:
1. Used stale position data from route params instead of live WebSocket
updates
2. HyperLiquid's marginUsed already includes PnL, but code was
double-counting
3. Recalculated PnL from prices, which missed accumulated funding fees
4. Timing mismatch between price updates and position updates
Changes:
- Add usePerpsLivePositions to subscribe to real-time position data
- Use livePosition.marginUsed which already includes accumulated PnL and
funding fees
- Use livePosition.positionValue for fee calculations to keep margin and
fees synchronized
- Remove effectivePnL recalculation that missed funding fees
- Round margin and fees separately before subtraction for transparent
calculation
- Update tests to reflect that marginUsed includes PnL from HyperLiquid
Result:
- Position card and close position view now show matching margin values
- Funding fees correctly included in all calculations
- No more incorrect negative receive amounts
- Calculation transparency: displayed margin - displayed fees =
displayed receive amount
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->
## **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: null
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-1956
## **Manual testing steps**
```gherkin
Feature: Close Position Calculation with Funding Fees
Scenario: Close position with accumulated funding fees shows correct receive amount
Given I have an open position with the following details:
| Coin | ETH |
| Margin Used | $100 |
| Unrealized PnL| -$5 |
| Entry Price | $2000 |
| Current Price | $2000 |
When I navigate to the close position view
And I view the close position summary
Then the margin displayed should be "$100"
And the receive amount should match "margin - fees"
And the receive amount should be positive
And the receive amount should not be negative or zero
Scenario: Receive amount calculation matches visual breakdown
Given I have an open position
When I view the close position summary
Then the displayed margin should be rounded to 2 decimals
And the displayed fees should be rounded to 2 decimals
And the receive amount should equal "rounded margin minus rounded fees"
And the calculation should be transparent to the user
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
https://github.com/user-attachments/assets/29289663-7b77-45f7-8ff6-c48917e1e21a
<!-- [screenshots/recordings] -->
## **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]
> Close Position view now uses live position data and computes receive
amount as rounded marginUsed minus fees, syncing PnL/margin (incl.
funding) and updating limit/fee logic and tests.
>
> - **Perps Close Position (UI/logic)**:
> - Subscribe to `usePerpsLivePositions` and derive `livePosition` for
real-time `marginUsed` and `unrealizedPnl` (includes funding fees).
> - Rework calculations:
> - `receiveAmount` = rounded `(closePercentage * marginUsed)` − rounded
fees.
> - `effectivePnL`: use live `pnl` for market orders; recompute only for
limit price.
> - `positionValue`/`closingValue` use current or limit price with
correct deps.
> - Back-calc `unrealizedPnlPercent` from `marginUsed - pnl`.
> - Pass `livePosition` into `handleClosePosition`; include updated
`receivedAmount`, `realizedPnl`, and price string.
> - Keep USD input in sync without jumps; auto-open limit price sheet on
limit; minor deps/rounding adjustments.
> - **Tests**:
> - Mock `usePerpsLivePositions`; update assertions to reflect
`marginUsed` includes PnL and new receive formula.
> - Add price update synchronization test and adjust PnL/receive
expectations for market/limit scenarios.
> - Update event/confirm handler expectations and various calculation
paths (fees, partial closes, short/long, limit price variations).
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
e008a6a. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: abretonc7s
<107169956+abretonc7s@users.noreply.github.com>
[9186674](9186674)
Co-authored-by: Nicholas Smith <nick.smith@consensys.net>
Co-authored-by: abretonc7s <107169956+abretonc7s@users.noreply.github.com>1 parent 16f0321 commit e734f45
2 files changed
Lines changed: 171 additions & 58 deletions
File tree
- app/components/UI/Perps/Views/PerpsClosePositionView
Lines changed: 119 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
119 | 120 | | |
120 | 121 | | |
121 | 122 | | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
122 | 126 | | |
123 | 127 | | |
124 | 128 | | |
| |||
166 | 170 | | |
167 | 171 | | |
168 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
169 | 177 | | |
170 | 178 | | |
171 | 179 | | |
| |||
446 | 454 | | |
447 | 455 | | |
448 | 456 | | |
449 | | - | |
| 457 | + | |
450 | 458 | | |
451 | 459 | | |
452 | 460 | | |
453 | 461 | | |
454 | 462 | | |
455 | | - | |
456 | | - | |
| 463 | + | |
| 464 | + | |
457 | 465 | | |
458 | 466 | | |
459 | 467 | | |
| |||
462 | 470 | | |
463 | 471 | | |
464 | 472 | | |
465 | | - | |
| 473 | + | |
466 | 474 | | |
467 | 475 | | |
468 | 476 | | |
| |||
471 | 479 | | |
472 | 480 | | |
473 | 481 | | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
474 | 486 | | |
475 | 487 | | |
476 | 488 | | |
| |||
481 | 493 | | |
482 | 494 | | |
483 | 495 | | |
484 | | - | |
485 | | - | |
486 | | - | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
487 | 499 | | |
488 | 500 | | |
489 | 501 | | |
490 | 502 | | |
491 | | - | |
492 | 503 | | |
493 | | - | |
| 504 | + | |
494 | 505 | | |
495 | 506 | | |
496 | 507 | | |
497 | 508 | | |
498 | 509 | | |
499 | 510 | | |
500 | 511 | | |
501 | | - | |
502 | | - | |
| 512 | + | |
| 513 | + | |
503 | 514 | | |
504 | 515 | | |
505 | 516 | | |
| |||
517 | 528 | | |
518 | 529 | | |
519 | 530 | | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
520 | 535 | | |
521 | 536 | | |
522 | 537 | | |
| |||
527 | 542 | | |
528 | 543 | | |
529 | 544 | | |
530 | | - | |
531 | | - | |
532 | | - | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
533 | 548 | | |
534 | 549 | | |
535 | 550 | | |
536 | 551 | | |
537 | | - | |
538 | | - | |
| 552 | + | |
| 553 | + | |
539 | 554 | | |
540 | 555 | | |
541 | 556 | | |
| |||
632 | 647 | | |
633 | 648 | | |
634 | 649 | | |
| 650 | + | |
635 | 651 | | |
636 | | - | |
| 652 | + | |
637 | 653 | | |
638 | 654 | | |
639 | 655 | | |
| |||
644 | 660 | | |
645 | 661 | | |
646 | 662 | | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
647 | 667 | | |
648 | 668 | | |
649 | 669 | | |
| |||
654 | 674 | | |
655 | 675 | | |
656 | 676 | | |
657 | | - | |
658 | | - | |
659 | | - | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
660 | 680 | | |
661 | 681 | | |
662 | 682 | | |
| |||
2049 | 2069 | | |
2050 | 2070 | | |
2051 | 2071 | | |
2052 | | - | |
2053 | | - | |
2054 | | - | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
2055 | 2075 | | |
2056 | 2076 | | |
2057 | 2077 | | |
| |||
2060 | 2080 | | |
2061 | 2081 | | |
2062 | 2082 | | |
2063 | | - | |
| 2083 | + | |
2064 | 2084 | | |
2065 | 2085 | | |
2066 | 2086 | | |
| |||
2369 | 2389 | | |
2370 | 2390 | | |
2371 | 2391 | | |
| 2392 | + | |
| 2393 | + | |
| 2394 | + | |
| 2395 | + | |
| 2396 | + | |
| 2397 | + | |
| 2398 | + | |
| 2399 | + | |
| 2400 | + | |
| 2401 | + | |
| 2402 | + | |
| 2403 | + | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
| 2409 | + | |
| 2410 | + | |
| 2411 | + | |
| 2412 | + | |
| 2413 | + | |
| 2414 | + | |
| 2415 | + | |
| 2416 | + | |
| 2417 | + | |
| 2418 | + | |
| 2419 | + | |
| 2420 | + | |
| 2421 | + | |
| 2422 | + | |
| 2423 | + | |
| 2424 | + | |
| 2425 | + | |
| 2426 | + | |
| 2427 | + | |
| 2428 | + | |
| 2429 | + | |
| 2430 | + | |
| 2431 | + | |
| 2432 | + | |
| 2433 | + | |
| 2434 | + | |
| 2435 | + | |
| 2436 | + | |
| 2437 | + | |
| 2438 | + | |
| 2439 | + | |
| 2440 | + | |
| 2441 | + | |
| 2442 | + | |
| 2443 | + | |
| 2444 | + | |
| 2445 | + | |
| 2446 | + | |
| 2447 | + | |
| 2448 | + | |
| 2449 | + | |
| 2450 | + | |
| 2451 | + | |
| 2452 | + | |
| 2453 | + | |
| 2454 | + | |
| 2455 | + | |
| 2456 | + | |
| 2457 | + | |
| 2458 | + | |
| 2459 | + | |
| 2460 | + | |
| 2461 | + | |
| 2462 | + | |
| 2463 | + | |
| 2464 | + | |
| 2465 | + | |
| 2466 | + | |
2372 | 2467 | | |
2373 | 2468 | | |
2374 | 2469 | | |
| |||
0 commit comments