Skip to content

Commit 3574cb5

Browse files
authored
fix(predict): round fee decimals to avoid underflow errors (#22361)
## **Description** Fixed a floating-point precision issue in the Polymarket fee calculation that was causing order placement failures. When calculating fees (e.g., for a $7.40 bet), JavaScript floating-point arithmetic would produce values with excessive decimal places (e.g., 0.29600000000000004), which would later fail when passed to `parseUnits`. The fix rounds the fee to 4 decimal places to prevent these precision errors while maintaining sufficient accuracy for fee calculations. ## **Changelog** CHANGELOG entry: null ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: Polymarket bet placement Scenario: user places a bet with amount that triggers floating-point precision issue Given user is on the Predict feature And user has selected a Polymarket prediction market When user enters a bet amount of $7.40 And user confirms the bet placement Then the order should be successfully placed without errors ``` ## **Screenshots/Recordings** ### **Before** Order placement would fail with parseUnits error when fee calculation resulted in values like 0.29600000000000004 ### **After** Order placement succeeds as fee is properly rounded to 0.2960 (4 decimal places) ## **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] > Round `totalFee` to 4 decimals in `calculateFees` to prevent floating-point precision issues. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit b198cda. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent deb715d commit 3574cb5

File tree

1 file changed

+3
-0
lines changed
  • app/components/UI/Predict/providers/polymarket

1 file changed

+3
-0
lines changed

app/components/UI/Predict/providers/polymarket/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,9 @@ export async function calculateFees({
756756

757757
totalFee = (userBetAmount * FEE_PERCENTAGE) / 100;
758758

759+
// Round to 4 decimals
760+
totalFee = Math.round(totalFee * 10000) / 10000;
761+
759762
// split total 50/50 between metamask and provider
760763
const metamaskFee = totalFee / 2;
761764
const providerFee = totalFee - metamaskFee;

0 commit comments

Comments
 (0)