Commit 11e58e2
authored
fix: perps withdraw token selection and balance validation (#28599)
## **Description**
Two fixes for the Perps Withdraw flow:
- **Zero-balance token selection reverts to mUSD on first try**: When
selecting a zero-balance token from the asset picker,
`TokensController.addTokens` was fire-and-forget (`.catch(noop)`), so
`setPayToken` ran before the controller could resolve the token's
metadata. The payment token was silently cleared and the UI fell back to
mUSD. Now awaits `addTokens` before calling `setPayToken`. Only
reproduced on first selection after fresh install — subsequent attempts
worked because the token was already tracked.
- **Insufficient funds when typing full available balance**: The raw
balance (e.g. `16.069`) was displayed rounded up to `$16.07` via
`formatPerpsFiat`, but validation compared against the raw value — so
typing `16.07` was rejected (`16.07 > 16.069`). Now truncates (floors)
the balance to 2 decimal places for both display and validation, so the
user sees `$16.06` and can withdraw exactly that amount.
## **Changelog**
CHANGELOG entry: Fixed perps withdraw zero-balance token selection
reverting to mUSD and insufficient funds error when typing full balance
## **Related issues**
Fixes: CONF-1160
## **Manual testing steps**
Feature: Perps Withdraw
Scenario: user selects zero-balance token on fresh install
- Given user opens perps withdraw for the first time (or cleared app
data)
- When user selects a token with zero balance from the Receive asset
picker
- Then the selected token persists and does not revert to mUSD
Scenario: user types full available balance
- Given user has a perps balance where the raw value has more than 2
decimal places
- When user looks at the displayed balance (should be truncated down,
e.g. $16.06 not $16.07)
- And user types that displayed balance amount
- Then no insufficient funds error is shown
- When user types one cent more than the displayed balance
- Then insufficient funds error is shown
## **Screenshots/Recordings**
### **Before**
### **After**
## **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
- [ ] 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]
> **Medium Risk**
> Touches withdrawal validation and token-selection flow; risk is
moderate because it changes async ordering around
`TokensController.addTokens` and alters displayed/validated balances,
but scope is limited and covered by unit tests.
>
> **Overview**
> Fixes two Perps withdraw edge cases: **(1)** selecting a zero-balance
receive token now reliably persists by making `pay-with-modal` *await*
`TokensController.addTokens` before calling `setPayToken`, preventing
fallback to the default token.
>
> **(2)** withdraw balance display and validation are aligned by
truncating (not rounding) available balance to 2 decimals via new
`truncateToTwoDecimals` in `formatUtils`, used in both
`PerpsWithdrawView` and `useWithdrawValidation`; adds targeted unit
tests for truncation and call ordering.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
4c6f0ef. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Signed-off-by: dan437 <80175477+dan437@users.noreply.github.com>1 parent 8b1df75 commit 11e58e2
File tree
7 files changed
+104
-15
lines changed- app/components
- UI/Perps
- Views/PerpsWithdrawView
- hooks
- utils
- Views/confirmations/components/modals/pay-with-modal
7 files changed
+104
-15
lines changedLines changed: 7 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
57 | | - | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
58 | 62 | | |
59 | 63 | | |
60 | 64 | | |
| |||
104 | 108 | | |
105 | 109 | | |
106 | 110 | | |
107 | | - | |
| 111 | + | |
108 | 112 | | |
109 | 113 | | |
110 | | - | |
111 | | - | |
| 114 | + | |
112 | 115 | | |
113 | 116 | | |
114 | 117 | | |
| |||
Lines changed: 31 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
107 | 138 | | |
108 | 139 | | |
109 | 140 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
9 | 12 | | |
10 | 13 | | |
11 | 14 | | |
| |||
25 | 28 | | |
26 | 29 | | |
27 | 30 | | |
28 | | - | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 34 | + | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
941 | 942 | | |
942 | 943 | | |
943 | 944 | | |
| 945 | + | |
| 946 | + | |
| 947 | + | |
| 948 | + | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
944 | 970 | | |
945 | 971 | | |
946 | 972 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
26 | 34 | | |
27 | 35 | | |
28 | 36 | | |
| |||
Lines changed: 22 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
438 | 438 | | |
439 | 439 | | |
440 | 440 | | |
441 | | - | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
442 | 458 | | |
443 | 459 | | |
444 | 460 | | |
| |||
461 | 477 | | |
462 | 478 | | |
463 | 479 | | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
464 | 484 | | |
465 | | - | |
| 485 | + | |
466 | 486 | | |
467 | 487 | | |
468 | 488 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
99 | 99 | | |
100 | 100 | | |
101 | 101 | | |
102 | | - | |
| 102 | + | |
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
126 | | - | |
| 126 | + | |
127 | 127 | | |
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
| 134 | + | |
135 | 135 | | |
136 | 136 | | |
137 | 137 | | |
| |||
140 | 140 | | |
141 | 141 | | |
142 | 142 | | |
143 | | - | |
| 143 | + | |
144 | 144 | | |
145 | 145 | | |
146 | 146 | | |
| |||
0 commit comments