-
Notifications
You must be signed in to change notification settings - Fork 16
fix: improve float handling in calldata encoder #199
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
base: v2-dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||
| import { describe, it, expect } from "vitest"; | ||||||||||
| import { encode } from "../src/abi/calldata/encoder"; | ||||||||||
| import { decode } from "../src/abi/calldata/decoder"; | ||||||||||
|
Comment on lines
+2
to
+3
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Use path aliases As per coding guidelines: ♻️ Proposed fix-import { encode } from "../src/abi/calldata/encoder";
-import { decode } from "../src/abi/calldata/decoder";
+import { encode } from "`@/abi/calldata/encoder`";
+import { decode } from "`@/abi/calldata/decoder`";📝 Committable suggestion
Suggested change
🤖 Prompt for AI AgentsSource: Coding guidelines |
||||||||||
|
|
||||||||||
| describe("calldata encoder - float handling", () => { | ||||||||||
| it("should encode integer numbers correctly", () => { | ||||||||||
| const encoded = encode(42); | ||||||||||
| const decoded = decode(encoded); | ||||||||||
| expect(decoded).toBe(42n); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it("should encode float with no fractional part as integer (e.g. 1.0)", () => { | ||||||||||
| const encoded = encode(1.0); | ||||||||||
| const decoded = decode(encoded); | ||||||||||
| expect(decoded).toBe(1n); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it("should throw descriptive error for true float values", () => { | ||||||||||
| expect(() => encode(1.5)).toThrow( | ||||||||||
| "calldata encoding error: float value '1.5' is not supported" | ||||||||||
| ); | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| it("should throw descriptive error for NaN", () => { | ||||||||||
| expect(() => encode(NaN)).toThrow(); | ||||||||||
| }); | ||||||||||
| }); | ||||||||||
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
Dead code: the whole-number float coercion branch is unreachable.
Number.isInteger(1.0)returnstruein JavaScript because1.0 === 1. TheNumber.isIntegerspecification checksMath.floor(x) === xfor finite numbers — the same condition at line 90. Therefore, when!Number.isInteger(data)istrue,Math.floor(data) === datais guaranteedfalsefor all finite numbers, making lines 90–94 unreachable.The PR's stated goal of handling
1.0is already achieved by the existingNumber.isIntegercheck at line 89 routing to line 100. The new code never executes.🔧 Proposed fix: remove dead branch, keep improved error message
case "number": { if (!Number.isInteger(data)) { - if (Number.isFinite(data) && Math.floor(data) === data) { - // Safe: float with no fractional part (e.g. 1.0 → 1) - encodeNum(to, BigInt(Math.trunc(data))); - return; - } throw new Error( `calldata encoding error: float value '${data}' is not supported. ` + `Convert to an integer or pass as a string instead.` ); } encodeNum(to, BigInt(data)); return; }📝 Committable suggestion
🤖 Prompt for AI Agents