Commit dcad69f
authored
fix(perps): replace 90% with Max button on Perps Withdraw (#29257)
## **Description**
Wires up the Max button on the Perps Withdraw confirmation (replacing
the `90%` button) and fixes three bugs uncovered while doing so:
- `!isNativePayToken` in `CustomAmountInfo` was inverted for post-quote
withdraws (pay token is the destination, not the source) — picking a
native destination (BNB/ETH) silently fell back to `90%`. Now gated on
`isTransactionPayWithdraw`, which is also synchronous so there's no `90%
→ Max` flicker on mount.
- `useTransactionCustomAmount` was overriding the input fiat with
`totals.targetAmount.usd` while `isMaxAmount=true`. For post-quote
withdraws that's the destination-chain received value (e.g. BNB after
bridge fees, ~$6 for a $50 withdraw), not the withdraw amount — now
skipped for withdraw flows.
- TPC's `calculatePostQuoteSourceAmounts` substitutes `token.balanceRaw`
(the **Arbitrum USDC wallet** balance, not the HyperLiquid balance) when
`isMaxAmount=true`, which stranded most of the HL balance on Max. Don't
set `isMaxAmount=true` for perps withdraw; route the typed amount
through instead.
Also added a shared `formatPerpsBalance` helper that truncates down to 2
decimals before formatting, and used it in both `PerpsWithdrawBalance`
and `PerpsMarketBalanceActions` (Perps home) so the two surfaces never
disagree by a cent.
## **Changelog**
CHANGELOG entry: Fixed Perps Withdraw Max so users actually withdraw
their full HyperLiquid balance; replaced 90% with a Max button; Perps
home and Withdraw now show the same balance.
## **Related issues**
Fixes:
[CONF-1161](https://consensyssoftware.atlassian.net/browse/CONF-1161)
## **Manual testing steps**
~~~gherkin
Feature: Perps Withdraw Max
Scenario: Max button replaces 90%
Given user is on the Perps Withdraw confirmation
Then the percentage row shows 10% / 25% / 50% / Max
And Max is visible on first render (no 90% flash)
Scenario: Max withdraws the full HyperLiquid balance
Given user has $40.40 available on HyperLiquid
When user taps Max
Then the input field shows $40.40 (not a BNB-denominated fiat value)
And "Available balance: $40.40" is shown below the input
When user taps Withdraw and submits
Then the HyperLiquid balance after the withdraw is 0
And no Arbitrum USDC wallet amount is used
Scenario: Max with a native destination token
Given user is on the Perps Withdraw confirmation
And user selects BNB (or ETH) as the Receive token
Then the percentage row still shows Max (not 90%)
Scenario: Perps home matches Withdraw page balance
Given a HyperLiquid balance of $40.489
Then Perps home shows "$40.48" (total) and "$40.48 available"
And the Withdraw page shows "Available balance: $40.48"
~~~
## **Screenshots/Recordings**
### **Before**
### **After**
Max button stable, full balance withdrawn, home and Withdraw match.
## **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.
[CONF-1161]:
https://consensyssoftware.atlassian.net/browse/CONF-1161?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Touches confirmation amount entry and Transaction Pay max-handling for
withdraw flows, which can affect how much users withdraw/send. Changes
are scoped and covered by new unit tests, but regressions could impact
displayed/entered amounts and max behavior.
>
> **Overview**
> Enables a true **Max** path for Perps Withdraw confirmations by
passing `hasMax` through `PerpsWithdrawInfo`/`CustomAmountInfo` and
allowing Max to render even when the (post-quote) selected token is
native.
>
> Fixes withdraw amount correctness in `useTransactionCustomAmount` by
skipping the `totals.targetAmount.usd` override for *withdraw* flows and
by **not setting `isMaxAmount`** for `perpsWithdraw` when 100% is
selected (to avoid Transaction Pay using the wrong source balance).
Perps withdraw Max is also truncated down to 2 decimals.
>
> Adds `formatPerpsBalance` (truncate-then-format) and switches Perps
home (`PerpsMarketBalanceActions`) and withdraw balance display to use
it, ensuring displayed balances never round above what can actually be
withdrawn. Also hardens a Trending hook test by waiting for the state
update to avoid flakiness.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
18e60cf. 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 70f8823 commit dcad69f
13 files changed
Lines changed: 324 additions & 26 deletions
File tree
- app/components
- UI
- Perps
- components/PerpsMarketBalanceActions
- utils
- Trending/hooks/useTrendingRequest
- Views/confirmations
- components
- info
- custom-amount-info
- perps-withdraw-info
- perps-confirmations/perps-withdraw-balance
- hooks/transactions
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
200 | 200 | | |
201 | 201 | | |
202 | 202 | | |
| 203 | + | |
203 | 204 | | |
204 | 205 | | |
205 | 206 | | |
| |||
Lines changed: 3 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 25 | + | |
29 | 26 | | |
30 | 27 | | |
31 | 28 | | |
| |||
255 | 252 | | |
256 | 253 | | |
257 | 254 | | |
258 | | - | |
| 255 | + | |
259 | 256 | | |
260 | 257 | | |
261 | 258 | | |
| |||
271 | 268 | | |
272 | 269 | | |
273 | 270 | | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
| 271 | + | |
278 | 272 | | |
279 | 273 | | |
280 | 274 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
967 | 968 | | |
968 | 969 | | |
969 | 970 | | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
| 992 | + | |
| 993 | + | |
| 994 | + | |
| 995 | + | |
| 996 | + | |
| 997 | + | |
| 998 | + | |
| 999 | + | |
| 1000 | + | |
| 1001 | + | |
970 | 1002 | | |
971 | 1003 | | |
972 | 1004 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
334 | 334 | | |
335 | 335 | | |
336 | 336 | | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
337 | 358 | | |
338 | 359 | | |
339 | 360 | | |
| |||
Lines changed: 9 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
239 | 239 | | |
240 | 240 | | |
241 | 241 | | |
242 | | - | |
243 | | - | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
244 | 251 | | |
245 | 252 | | |
246 | 253 | | |
| |||
Lines changed: 88 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
533 | 533 | | |
534 | 534 | | |
535 | 535 | | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
536 | 624 | | |
537 | 625 | | |
538 | 626 | | |
| |||
Lines changed: 6 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
50 | 53 | | |
51 | 54 | | |
52 | 55 | | |
| |||
125 | 128 | | |
126 | 129 | | |
127 | 130 | | |
| 131 | + | |
128 | 132 | | |
129 | 133 | | |
130 | 134 | | |
| |||
289 | 293 | | |
290 | 294 | | |
291 | 295 | | |
292 | | - | |
| 296 | + | |
293 | 297 | | |
294 | 298 | | |
295 | 299 | | |
| |||
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
74 | 83 | | |
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
| |||
Lines changed: 16 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
49 | 65 | | |
50 | 66 | | |
51 | 67 | | |
| |||
0 commit comments