fix(utils): allow integer prices written with trailing decimal zeros in formatPrice#136
Open
Nexory wants to merge 1 commit into
Open
fix(utils): allow integer prices written with trailing decimal zeros in formatPrice#136Nexory wants to merge 1 commit into
Nexory wants to merge 1 commit into
Conversation
…in formatPrice
`formatPrice` documents that "integer prices are always allowed regardless
of significant figures", and bypasses the 5-sig-fig truncation when the raw
input matches `/^-?\d+$/`. That check only runs on the raw input, so a price
that is integer-valued but written with a trailing decimal (e.g. "100001.0")
fails the regex, falls through to `toFixedTruncate` (which strips the ".0",
yielding "100001"), and is then truncated by the significant-figure step to
"100000".
Result: the same integer price produces different output depending on
formatting:
formatPrice("100001", 0) === "100001" // ok
formatPrice("100001.0", 0) === "100000" // wrong, off by 1
formatPrice("123456.0", 0) === "123450" // wrong
formatPrice("-123456.0",0) === "-123450" // wrong
A float-derived price (e.g. `n.toFixed(1)` -> "100001.0") therefore submits
an order at a different price than intended.
Re-check the integer-bypass after decimal truncation, so a value that became
an integer is treated the same as a raw integer. A value that truncated all
the way to zero is excluded so the existing "too small" RangeError still
fires.
Adds regression cases to tests/utils/format.test.ts. The full formatPrice
suite (including the reference-asset validation) passes; deno fmt and deno
lint are clean.
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
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.
Bug
formatPricedocuments that "integer prices are always allowed regardless of significant figures" and bypasses the 5-significant-figure truncation when the input matches/^-?\d+$/. That check only runs on the raw input, so a price that is integer-valued but written with a trailing decimal (e.g."100001.0") fails the regex, falls through totoFixedTruncate(which strips the.0, yielding"100001"), and is then truncated by the sig-fig step to"100000".The same integer price produces different output depending on how it was formatted:
A float-derived price submitted as a string (e.g.
(100001).toFixed(1)->"100001.0", or any value that carries a trailing zero) therefore places an order at a different price than intended - a direct mispricing on high-value assets.Fix
Re-check the integer-bypass condition after
toFixedTruncate, so a value that has become an integer is treated the same as a raw integer input. A value that truncated all the way to zero is excluded, so the existing "price too small"RangeErrorstill fires.Tests
Added regression cases to
tests/utils/format.test.ts:Verification (local)
main(return"100000"/"123450") and pass with this change.formatPrice+formatSizesuite stays green (46 steps, including the reference-asset validation against PURR/DYDX/SOL/BNB/ETH/BTC and the spot pairs).formatPrice("0.0000001", 0)), and non-integer truncation is unchanged (formatPrice("100000.5", 0)->"100000",formatPrice("12345.6", 0)->"12345").deno fmt --checkanddeno lintare clean.