Commit 0862685
authored
fix(predict): cp-7.61.0 improve bet amount validation with fee-adjusted calculations (MetaMask#23543)
<!--
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**
Add fee percentage tracking and fix max bet amount calculations to
properly account for fees in the prediction betting system.
Changes:
- Add totalFeePercentage field to PredictFees interface
- Implement calculateMaxBetAmount utility to account for fees
- Update calculateFees to return totalFeePercentage (0 or
FEE_PERCENTAGE)
- Fix minimum bet validation to include fees (minimumBetWithFees)
- Improve error messaging for insufficient funds vs below minimum
- Add conditional error message when balance is below minimum bet
- Export MINIMUM_BET constant for reusability
Test coverage:
- Add comprehensive tests for calculateMaxBetAmount utility
- Update all existing tests to include totalFeePercentage in mock fees
- Add assertions for totalFeePercentage in fee calculation tests
This ensures users can accurately see their maximum bet amount after
fees are deducted, preventing order failures due to insufficient funds
when fees are applied.
<!--
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/PRED-342?atlOrigin=eyJpIjoiMzU5NTJmY2I2NjM5NGQ5ZGIyYmFhMzJjM2VhNTY1ZWQiLCJwIjoiaiJ9
## **Manual testing steps**
```gherkin
Feature: my feature name
Scenario: user [verb for user action]
Given [describe expected initial app state]
When user [verb for user action]
Then [describe expected outcome]
```
## **Screenshots/Recordings**
https://www.loom.com/share/05f87ae8888c4e739819a2b5bdc6c4a5
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
## **Pre-merge author checklist**
- [ ] 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).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] 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]
> Track fee percentage in previews, compute fee-adjusted min/max bet
amounts, update UI/error messages, and expand tests accordingly.
>
> - **Predict UI**:
> - Add `MINIMUM_BET` constant and compute `minimumBetWithFees` using
`fees.totalFeePercentage`.
> - Use new `calculateMaxBetAmount` to determine available amount;
refine insufficient-funds vs below-minimum messaging.
> - Update `PredictBuyPreview` logic and tests for fee-adjusted
validation and display.
> - **Utils**:
> - New `orders.ts`: `generateOrderId`, `calculateMaxBetAmount` (+
comprehensive tests).
> - `polymarket/utils.ts`: `calculateFees` now returns
`totalFeePercentage`; waive fees sets it to `0`.
> - **Types & Providers**:
> - Extend `PredictFees` with `totalFeePercentage` and propagate through
previews/tests.
> - **Localization**:
> - Add `predict.order.no_funds_enough` message.
> - **Tests**:
> - Update/expand unit tests across hooks, provider, utils, and view to
include fee percentage and new validation paths.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
92a617e. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 3c902cc commit 0862685
10 files changed
Lines changed: 1141 additions & 23 deletions
File tree
- app/components/UI/Predict
- hooks
- providers
- polymarket
- utils
- views/PredictBuyPreview
- locales/languages
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: 37 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
740 | 740 | | |
741 | 741 | | |
742 | 742 | | |
| 743 | + | |
743 | 744 | | |
744 | 745 | | |
745 | 746 | | |
| |||
1358 | 1359 | | |
1359 | 1360 | | |
1360 | 1361 | | |
1361 | | - | |
| 1362 | + | |
| 1363 | + | |
| 1364 | + | |
| 1365 | + | |
| 1366 | + | |
| 1367 | + | |
1362 | 1368 | | |
1363 | 1369 | | |
1364 | 1370 | | |
| |||
1374 | 1380 | | |
1375 | 1381 | | |
1376 | 1382 | | |
1377 | | - | |
| 1383 | + | |
| 1384 | + | |
| 1385 | + | |
| 1386 | + | |
| 1387 | + | |
| 1388 | + | |
1378 | 1389 | | |
1379 | 1390 | | |
1380 | 1391 | | |
| |||
1395 | 1406 | | |
1396 | 1407 | | |
1397 | 1408 | | |
1398 | | - | |
| 1409 | + | |
| 1410 | + | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
1399 | 1415 | | |
1400 | 1416 | | |
1401 | 1417 | | |
| |||
1416 | 1432 | | |
1417 | 1433 | | |
1418 | 1434 | | |
1419 | | - | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
| 1440 | + | |
1420 | 1441 | | |
1421 | 1442 | | |
1422 | 1443 | | |
| |||
1447 | 1468 | | |
1448 | 1469 | | |
1449 | 1470 | | |
1450 | | - | |
| 1471 | + | |
| 1472 | + | |
| 1473 | + | |
| 1474 | + | |
| 1475 | + | |
| 1476 | + | |
1451 | 1477 | | |
1452 | 1478 | | |
1453 | 1479 | | |
| |||
1472 | 1498 | | |
1473 | 1499 | | |
1474 | 1500 | | |
1475 | | - | |
| 1501 | + | |
| 1502 | + | |
| 1503 | + | |
| 1504 | + | |
| 1505 | + | |
| 1506 | + | |
1476 | 1507 | | |
1477 | 1508 | | |
1478 | 1509 | | |
| |||
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1832 | 1832 | | |
1833 | 1833 | | |
1834 | 1834 | | |
| 1835 | + | |
1835 | 1836 | | |
1836 | 1837 | | |
1837 | 1838 | | |
| |||
1845 | 1846 | | |
1846 | 1847 | | |
1847 | 1848 | | |
| 1849 | + | |
1848 | 1850 | | |
1849 | 1851 | | |
1850 | 1852 | | |
| |||
1860 | 1862 | | |
1861 | 1863 | | |
1862 | 1864 | | |
| 1865 | + | |
1863 | 1866 | | |
1864 | 1867 | | |
1865 | 1868 | | |
| |||
1878 | 1881 | | |
1879 | 1882 | | |
1880 | 1883 | | |
| 1884 | + | |
1881 | 1885 | | |
1882 | 1886 | | |
1883 | 1887 | | |
| |||
1899 | 1903 | | |
1900 | 1904 | | |
1901 | 1905 | | |
| 1906 | + | |
1902 | 1907 | | |
1903 | 1908 | | |
1904 | 1909 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
755 | 755 | | |
756 | 756 | | |
757 | 757 | | |
| 758 | + | |
758 | 759 | | |
759 | 760 | | |
760 | 761 | | |
| |||
773 | 774 | | |
774 | 775 | | |
775 | 776 | | |
| 777 | + | |
776 | 778 | | |
777 | 779 | | |
778 | 780 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
75 | 76 | | |
76 | 77 | | |
77 | 78 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
0 commit comments