fix: guard weighted-average merge against zero outputAmountSum in historicalOrderCharts#2777
fix: guard weighted-average merge against zero outputAmountSum in historicalOrderCharts#2777thedavidmeister wants to merge 10 commits into
Conversation
…toricalOrderCharts When two or more trades share a timestamp and their outputVaultBalanceChange.amount values sum to zero, dividing ioratioSum by outputAmountSum produced NaN/Infinity. Falls back to unweighted mean of ioratio values when outputAmountSum === 0. Closes #2766 Co-Authored-By: Claude <noreply@anthropic.com>
|
Warning Review limit reached
Next review available in: 42 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the 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 reviews. How do review 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 refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthrough
Divide-by-zero fix and test
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
… helper
id parameter and orderHash literal in the inline test helper must satisfy
the `0x${string}` template literal type required by RaindexTrade.
Co-Authored-By: Claude <noreply@anthropic.com>
RaindexTrade is a WASM class with a private constructor and additional required fields (chainId, formattedIoRatio, ioRatio, owner). The inline test helper builds a partial object literal, so cast through unknown rather than enumerate all required properties. Co-Authored-By: Claude <noreply@anthropic.com>
Run prettier on historicalOrderCharts.ts.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/ui-components/src/lib/services/historicalOrderCharts.ts`:
- Around line 51-55: The fallback calculation for ioratioAverage when
outputAmountSum === 0 does not guard against d.value being non-finite (Infinity
or NaN), which can occur when outputVaultBalanceChange.formattedAmount is "0"
from the initialization at lines 22-25. Filter out non-finite values from the
fallback average calculation by adding a check to ensure each d.value is finite
before including it in the reduce operation. Additionally, update the test
fixture that currently hardcodes "100" to test with "0" values that would expose
this non-finite path and verify the fix handles it correctly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 68d77525-228b-4304-b28e-e4ae2687eef8
📒 Files selected for processing (1)
packages/ui-components/src/lib/services/historicalOrderCharts.ts
When outputVaultBalanceChange.amount is BigInt(0) the formattedAmount is also "0", making the per-trade value Infinity/NaN. The previous fallback divided Infinity × N / N = Infinity. Filter non-finite values before the unweighted mean; if every entry is non-finite (i.e. all output amounts are zero), return 0. Update the test fixture to use formattedAmount:"0" (consistent with amount BigInt(0)) and tighten the assertion to expect value 0. Co-Authored-By: Claude <noreply@anthropic.com>
…-historical-chart-divide-zero
|
screenshot pending (manual): historicalOrderCharts.ts produces data for lightweight-charts canvas rendering — canvas output requires a headless Chromium harness to screenshot; the change itself is a computation fix (unweighted-mean fallback when outputAmountSum is 0) with no layout change |
…-historical-chart-divide-zero
|
🤖 ai:vetter |
|
Rework note (human): simplify so all branches are reachable + covered. The const ioratioAverage =
outputAmountSum === 0 ? 0 : ioratioSum / outputAmountSum;Delete the |
…-historical-chart-divide-zero
…e (rework) outputAmountSum === 0 implies every merged value is Infinity (division by the zero formatted amount), so the finite-values unweighted-mean branch could never execute; the bucket charts as zero directly. Co-Authored-By: Claude <noreply@anthropic.com>
|
🤖 ai:producer |
|
🤖 ai:producer |
|
🤖 ai:producer |
Summary
prepareHistoricalOrderChartDatawhen two or more trades share a timestamp and theiroutputVaultBalanceChange.amountvalues sum to zerooutputAmountSum === 0, falls back to unweighted mean of ioratio values instead of dividing by zero (which would produceNaNor±Infinity)Note: this is a pure data computation function with no rendering, so a UI screenshot is not applicable.
Closes #2766
Co-Authored-By: Claude noreply@anthropic.com
Summary by CodeRabbit
Bug Fixes
0instead of error-prone results.Tests
0.