-
Notifications
You must be signed in to change notification settings - Fork 35
fix(#2324,#2246): guard bigint→float conversions and route price→E6 through one helper #2535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { bigintToFloat, bigintRatio } from "@/lib/formatters"; | ||
| import { toE6 } from "@/lib/format"; | ||
|
|
||
| /** | ||
| * GH#2324 — `Number(someBigint)` loses precision above `Number.MAX_SAFE_INTEGER` | ||
| * and says nothing about it: it returns a plausible, wrong number. | ||
| * | ||
| * GH#2246 — the `price -> E6` conversion was inlined rather than shared, so the | ||
| * one place that could grow a guard was five places that could not. | ||
| * | ||
| * The two are the same underlying problem — arithmetic done inline at the render | ||
| * boundary instead of behind a helper that can be given a rule. | ||
| */ | ||
|
|
||
| describe("bigintToFloat refuses silently-wrong conversions (GH#2324)", () => { | ||
| const MAX = BigInt(Number.MAX_SAFE_INTEGER); | ||
|
|
||
| it("converts ordinary amounts", () => { | ||
| expect(bigintToFloat(1_500_000n, 6)).toBe(1.5); | ||
| expect(bigintToFloat(0n, 6)).toBe(0); | ||
| expect(bigintToFloat(-2_000_000n, 6)).toBe(-2); | ||
| }); | ||
|
|
||
| it("returns null above MAX_SAFE_INTEGER instead of a wrong number", () => { | ||
| // The failure this exists for: Number() on this returns a real-looking float. | ||
| const tooBig = MAX + 1n; | ||
| expect(Number.isFinite(Number(tooBig))).toBe(true); // the trap | ||
| expect(bigintToFloat(tooBig, 6)).toBeNull(); // the guard | ||
| }); | ||
|
|
||
| it("guards the NEGATIVE side too", () => { | ||
| // A large loss is as easy to hit as a large gain, and `raw < 0` would slip | ||
| // past a naive `raw > MAX` check. | ||
| expect(bigintToFloat(-(MAX + 1n), 6)).toBeNull(); | ||
| }); | ||
|
|
||
| it("checks the RAW magnitude, not the scaled one", () => { | ||
| // Dividing first would hide the loss: (MAX+1)/1e6 is small and finite, but | ||
| // the precision is already gone by then. | ||
| const tooBig = MAX + 1n; | ||
| expect(bigintToFloat(tooBig, 18)).toBeNull(); | ||
| }); | ||
|
|
||
| it("accepts exactly MAX_SAFE_INTEGER — the boundary is inclusive", () => { | ||
| expect(bigintToFloat(MAX, 0)).toBe(Number.MAX_SAFE_INTEGER); | ||
| }); | ||
| }); | ||
|
|
||
| describe("bigintRatio keeps large-over-large exact (GH#2324)", () => { | ||
| it("computes a small quotient from two huge operands", () => { | ||
| // Both sides far exceed MAX_SAFE_INTEGER; the quotient is 0.5. Converting | ||
| // each side to a float first would lose the precision before dividing. | ||
| const huge = BigInt("100000000000000000000000"); | ||
| expect(bigintRatio(huge, huge * 2n)).toBeCloseTo(0.5, 9); | ||
| }); | ||
|
|
||
| it("returns null on a zero denominator rather than Infinity or NaN", () => { | ||
| expect(bigintRatio(1n, 0n)).toBeNull(); | ||
| }); | ||
|
|
||
| it("handles an ordinary ratio", () => { | ||
| expect(bigintRatio(1n, 4n)).toBeCloseTo(0.25, 9); | ||
| }); | ||
| }); | ||
|
|
||
| describe("toE6 is the one place price->E6 happens (GH#2246)", () => { | ||
| it("converts a price to E6 base units", () => { | ||
| expect(toE6(1)).toBe(1_000_000n); | ||
| expect(toE6(0.5)).toBe(500_000n); | ||
| expect(toE6(123.456789)).toBe(123_456_789n); | ||
| }); | ||
|
|
||
| it("rounds rather than truncating", () => { | ||
| // 1.0000005 * 1e6 = 1000000.5 → rounds to 1000001, not 1000000. | ||
| expect(toE6(1.0000005)).toBe(1_000_001n); | ||
| }); | ||
|
|
||
| it("throws on a non-finite input rather than producing garbage", () => { | ||
| // BigInt(Math.round(NaN)) throws RangeError — the inline form did this too, | ||
| // but from five different places with five different stack traces. | ||
| expect(() => toE6(NaN)).toThrow(); | ||
| expect(() => toE6(Infinity)).toThrow(); | ||
| }); | ||
| }); |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Preserve the precision-guard failure instead of displaying zero.
bigintToFloatandbigintRatiousenullto indicate that a value cannot be displayed safely. These fallbacks convert that state into a valid zero value. An active position above the safe range then displays zero balance, zero redeemable value, zero share, or zero position value.app/hooks/useLpPositions.ts#L264-L264: Preserve an unsafe LP balance as nullable state and render a placeholder in the list row.app/hooks/useLpPositions.ts#L274-L274: Preserve an unsafe redeemable value as nullable state and render a placeholder.app/hooks/useLpPositions.ts#L278-L281: Preserve an unsafe share as nullable state and render a placeholder.app/components/earn/LpPositionDashboard.tsx#L49-L51: Render a placeholder whenbigintRatioreturnsnullinstead of passing0toAnimatedNumber.📍 Affects 2 files
app/hooks/useLpPositions.ts#L264-L264(this comment)app/hooks/useLpPositions.ts#L274-L274app/hooks/useLpPositions.ts#L278-L281app/components/earn/LpPositionDashboard.tsx#L49-L51🤖 Prompt for AI Agents