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 7 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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ 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 docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
packages/ui-components/src/lib/services/historicalOrderCharts.tsOops! Something went wrong! :( ESLint: 8.57.1 Error: ESLint configuration in --config is invalid:
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 |
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.