Repository navigation
fix: price impact scale normalization, batch concurrency/rate-limits, and anomaly NaN threshold - #1148
Open
ushpraise wants to merge 4 commits into
Open
fix: price impact scale normalization, batch concurrency/rate-limits, and anomaly NaN threshold#1148ushpraise wants to merge 4 commits into
ushpraise wants to merge 4 commits into
Conversation
|
@ushpraise Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
AMMCalculator.computePriceImpact(), spot price and effective price strings were parsed into scaled integers using differing scales (e.g. scale 1 for integer price strings without decimal points vs scale 10^12 for fractional price strings), resulting in incorrect price impact computations (e.g. reporting 0.00% instead of 9.00% for 100 swap on a 1000/1000 pool).InvoiceBatchProcessoraccepted invalid concurrency limits (non-positive integers, fractional numbers, NaN, and ±Infinity) and launched worker loops unbounded up tomaxConcurrenteven when only a few invoices were queued, causing dispatch failures or stalling the event loop on huge limits.InvoiceBatchProcessorallowed global 429 pause windows to be shortened if subsequent requests received smallerretryAfterMsvalues, workers sleeping on earlier deadlines did not recheck extended deadlines upon waking, and invalid/negative/non-finite retry and pause options were accepted.AnomalyDetector, thesensitivityThresholdvalidationthis.sensitivityThreshold <= 0 || this.sensitivityThreshold > 1acceptedNaNbecause comparison withNaNevaluates to false, which silently classified all anomaly scores as "normal".What changed
src/ammCalculator.ts: NormalizedspotScaledandeffectiveScaledto a common scale before computing the percentage price impact.src/invoiceBatchProcessor.ts:maxConcurrentis an integer >= 1, throwingRangeErrorotherwise.Math.min(maxConcurrent, invoiceIds.length).isValidPauseMsvalidation and safe fallback inretryAfterMsfor non-integer/negative values.rateLimitPauseMsas a non-negative integer.Math.max(pausedUntil, nextPausedUntil)and implementedwaitForGlobalPause()to re-check the global pause deadline after waking.src/anomalyDetector.ts: Added!Number.isFinite(this.sensitivityThreshold)check to rejectNaNand infinite values while preserving valid thresholds in(0, 1].test/ammCalculator.test.ts: Added normalization regression test forestimateSwapOutput.test/invoiceBatchProcessor.concurrency.test.ts: Added tests formaxConcurrentvalidation and initial launch capping.test/invoiceBatchProcessor.rateLimit.test.ts: Added tests forrateLimitPauseMsvalidation, malformed retry-after fallback, monotonic global pause enforcement, and wake-up pause re-checking.test/anomalyDetector.nan.test.ts: Added tests forNaNsensitivity threshold rejection and upper boundary acceptance.Testing
npx vitest run test/ammCalculator.test.ts: 13/13 tests passed.npx vitest run test/invoiceBatchProcessor.concurrency.test.ts: 7/7 tests passed.npx vitest run test/invoiceBatchProcessor.rateLimit.test.ts: 12/12 tests passed.npx vitest run test/invoiceBatchProcessor.test.ts: 6/6 tests passed.npx vitest run test/anomalyDetector.test.ts: 25/25 tests passed.npx vitest run test/anomalyDetector.nan.test.ts: 2/2 tests passed.npm test: 110/110 tests passed across client, retry policy, and network environment suites.Issues
closes #1008
closes #1009
closes #1010
closes #1011