Commit e3dd7bd
authored
fix(predict): hide unrealized pnl if the user has 0 positions (MetaMask#22678)
## **Description**
This PR fixes an issue where unrealized P&L was being displayed even
when users had no active positions. The fix ensures that unrealized P&L
is only shown when the user has at least one position, preventing
confusing or misleading information from being displayed.
**What is the reason for the change?**
Users were seeing unrealized P&L data displayed even when they had no
positions, which could be confusing since P&L is meaningless without any
active positions.
In addition, Polymarket uPnL API endpoint seems to give us bogus
information when we have 0 positions.
**What is the improvement/solution?**
The `useUnrealizedPnL` hook now fetches both unrealized P&L data and the
user's positions in parallel. The unrealized P&L is only displayed if
`positions.length > 0`, ensuring users only see relevant P&L information
when they have active positions.
## **Changelog**
CHANGELOG entry: null
## **Related issues**
Fixes:
## **Manual testing steps**
```gherkin
Feature: Unrealized P&L visibility based on positions
Scenario: user has no positions
Given user is logged in with an account
And user has no active positions
When user navigates to the Predict screen
Then unrealized P&L section is not displayed
Scenario: user has active positions
Given user is logged in with an account
And user has at least one active position
When user navigates to the Predict screen
Then unrealized P&L section is displayed with current P&L data
```
## **Screenshots/Recordings**
### **Before**
<!-- Unrealized P&L displayed even with 0 positions -->
### **After**
https://github.com/user-attachments/assets/cba5278e-09fe-4f75-9eb3-a17771d3174d
## **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]
> Only show unrealized PnL when the user has at least one position,
fetching PnL and positions concurrently.
>
> - **Predict Hook
(`app/components/UI/Predict/hooks/useUnrealizedPnL.tsx`)**
> - Fetches `getUnrealizedPnL` and `getPositions` in parallel via
`Promise.all`.
> - Sets `unrealizedPnL` to `null` when `positions.length === 0`;
otherwise uses fetched P&L.
> - Calls `getPositions` with `{ providerId, limit: 1, offset: 0,
claimable: false }`.
> - **Tests
(`app/components/UI/Predict/hooks/useUnrealizedPnL.test.tsx`)**
> - Add mocks and coverage for positions-based visibility (no positions
=> `null`, with positions => show P&L).
> - Verify `getPositions` is called with the correct params and
provider-specific calls.
> - Assert parallel invocation of `getUnrealizedPnL` and `getPositions`.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
8b77e7f. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent a7e247c commit e3dd7bd
2 files changed
Lines changed: 123 additions & 5 deletions
Lines changed: 110 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| 55 | + | |
53 | 56 | | |
54 | 57 | | |
55 | 58 | | |
| |||
68 | 71 | | |
69 | 72 | | |
70 | 73 | | |
| 74 | + | |
| 75 | + | |
71 | 76 | | |
72 | 77 | | |
73 | 78 | | |
| 79 | + | |
74 | 80 | | |
75 | 81 | | |
76 | 82 | | |
| |||
260 | 266 | | |
261 | 267 | | |
262 | 268 | | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
263 | 373 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
75 | 75 | | |
76 | 76 | | |
77 | 77 | | |
78 | | - | |
79 | | - | |
| 78 | + | |
| 79 | + | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
83 | | - | |
84 | | - | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
85 | 93 | | |
86 | 94 | | |
87 | 95 | | |
| |||
0 commit comments