Skip to content

fix(polymarket): round dollar amounts to base units instead of flooring them - #623

Open
A5Infinity wants to merge 4 commits into
BlockRunAI:mainfrom
A5Infinity:fix/polymarket-usd-rounding
Open

A5Infinity wants to merge 4 commits into
BlockRunAI:mainfrom
A5Infinity:fix/polymarket-usd-rounding

Conversation

@A5Infinity

@A5Infinity A5Infinity commented Nov 20, 2026 •

Copy link
Copy Markdown

fundVault and withdrawFunds convert the dollar amount with Math.floor(amount * 10 ** 6). That product is not exact in IEEE-754: 2.01 * 10 ** 6 is 2009999.9999999998, so a $2.01 deposit signs 2,009,999 base units instead of 2,010,000. Out of the 10,000 two-decimal amounts from $0.01 to $100, 151 lose one base unit this way.

Change

  • New src/polymarket/units.ts with usdToBaseUnits(usd, decimals), which rounds to the nearest base unit.
  • fund.ts (deposit, USDC) and withdraw.ts (amount_usd, pUSD) use it. The "withdraw everything" path (balanceRaw) is unchanged.

Tests
src/polymarket/units.test.ts checks 2.01 / 4.02 / 8.19, every two-decimal amount from $0.01 to $100, and 6-decimal inputs. With the old Math.floor conversion, 2 of its 3 tests fail; with usdToBaseUnits, all 3 pass. Prettier is clean on the changed files.

I ran the new test on its own with vitest. I could not run the full suite or typecheck here, because two dependencies are fetched from codeload.github.com, which my environment cannot reach.

🤖 Generated with Claude Code

https://claude.ai/code/session_014cRzwuyJe8BGwXowsQ3kpw

Summary by CodeRabbit

  • Bug Fixes

    • Improved precision when converting dollar amounts to token units for deposits and withdrawals, reducing floating-point rounding errors.
    • Withdrawals without a specified amount continue to use the full available balance.
  • Tests

    • Added coverage for accurate conversions across common dollar amounts and sub-cent values.

A5Infinity and others added 4 commits November 20, 2026 13:39
…nits

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a shared function that converts USD amounts to integer base units. Funding and explicit withdrawals use it, and tests cover cent and sub-cent amounts.

Changes: Amount conversion

Layer / File(s) Summary
Add and test the conversion function
src/polymarket/units.ts, src/polymarket/units.test.ts
The exported function scales a USD amount, rounds it, and returns a bigint. Tests cover floating-point cases, cent amounts, and sub-cent precision.
Use the conversion in funding and withdrawals
src/polymarket/fund.ts, src/polymarket/withdraw.ts
Funding and explicit withdrawals use the shared function to convert amounts. Withdrawals without an amount continue to use the full balance.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: vickyxai

Merge Risk: Minimal · up to 74293

This fixes off-by-one-base-unit errors when converting dollar amounts such as $2.01 for Polymarket deposits and explicit withdrawals. The change is small and localized, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: Polymarket dollar amounts now round to base units instead of being floored. The raw summary confirms that usdToBaseUnits replaces Math.floor(amount * 10 ** 6) in fund.…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: Polymarket dollar amounts now round to base units instead of being floored. The raw summary confirms that usdToBaseUnits replaces Math.floor(amount * 10 ** 6) in fund.ts and withdraw.ts, and that units.ts and its tests were added. The title is specific and matches the changeset, and it stays short.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fa06392 and 74293a6.

Files selected for processing: 4

  • src/polymarket/fund.ts
  • src/polymarket/units.test.ts
  • src/polymarket/units.ts
  • src/polymarket/withdraw.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant