Skip to content

fix: clamp computePnlPercent and computeFundingRateAnnualized instead of throwing - #328

Open
Ayomisco wants to merge 1 commit into
dcccrypto:mainfrom
Ayomisco:fix/sdk-7-clamp-pnl-funding-overflow
Open

fix: clamp computePnlPercent and computeFundingRateAnnualized instead of throwing#328
Ayomisco wants to merge 1 commit into
dcccrypto:mainfrom
Ayomisco:fix/sdk-7-clamp-pnl-funding-overflow

Conversation

@Ayomisco

Copy link
Copy Markdown
Contributor

Fixes #327

Changes

  • src/math/trading.ts
    • computePnlPercent: replace throw with a clamped return (Number.MAX_SAFE_INTEGER / 100) when scaledPct exceeds the safe integer range
    • computeFundingRateAnnualized: replace throw with return Infinity / return -Infinity for the same boundary condition

Why clamp instead of throw

Both functions are called from UI render paths with no try/catch. Returning a sentinel value (a very large percentage or Infinity) is correct behaviour for "effectively infinite" results and does not crash the page. The original throw was added to surface precision concerns but broke the caller contract for a pure display utility.

… of throwing

Both functions threw an unhandled Error when their BigInt intermediate result
exceeded Number.MAX_SAFE_INTEGER. In the UI these are called inside render
and event handlers with no surrounding try/catch, so an extremely large position
or extreme funding rate would crash the page.

computePnlPercent now returns a clamped sentinel (MAX_SAFE_INTEGER / 100) for
values beyond the safe range. computeFundingRateAnnualized returns +/-Infinity,
which is the mathematically correct value and is handled safely by JS formatters.
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@Ayomisco, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 49 minutes and 13 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits.

🚦 How do rate limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1b0cc5ca-b42d-41b7-ba86-f5b56789da2a

📥 Commits

Reviewing files that changed from the base of the PR and between 673bc47 and 2ff86f7.

📒 Files selected for processing (1)
  • src/math/trading.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

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.

SDK-7: computePnlPercent and computeFundingRateAnnualized throw on large values, crashing UI

1 participant