Skip to content

fix(utils): allow integer prices written with trailing decimal zeros in formatPrice#136

Open
Nexory wants to merge 1 commit into
nktkas:mainfrom
Nexory:fix/format-price-integer-trailing-zeros
Open

fix(utils): allow integer prices written with trailing decimal zeros in formatPrice#136
Nexory wants to merge 1 commit into
nktkas:mainfrom
Nexory:fix/format-price-integer-trailing-zeros

Conversation

@Nexory

@Nexory Nexory commented Jun 10, 2026

Copy link
Copy Markdown

Bug

formatPrice documents 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 to toFixedTruncate (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:

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
formatPrice("100001.00", 0) // "100000"   wrong

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" RangeError still fires.

price = StringMath.toFixedTruncate(price, maxDecimals);

// Integer prices are always allowed, including values that became integers
// after decimal truncation (e.g. "100001.0" -> "100001").
if (/^-?\d+$/.test(price) && !/^-?0+$/.test(price)) {
  return formatDecimalString(price);
}

price = StringMath.toPrecisionTruncate(price, 5);

Tests

Added regression cases to tests/utils/format.test.ts:

assertEquals(formatPrice("100001.0", 0), "100001");
assertEquals(formatPrice("123456.0", 0), "123456");
assertEquals(formatPrice("100001.00", 0), "100001");
assertEquals(formatPrice("-123456.0", 0), "-123456");
assertEquals(formatPrice("100001.0", 0, "spot"), "100001");

Verification (local)

  • The new cases fail on main (return "100000" / "123450") and pass with this change.
  • The full formatPrice + formatSize suite stays green (46 steps, including the reference-asset validation against PURR/DYDX/SOL/BNB/ETH/BTC and the spot pairs).
  • The "price too small -> RangeError" edge case still throws (formatPrice("0.0000001", 0)), and non-integer truncation is unchanged (formatPrice("100000.5", 0) -> "100000", formatPrice("12345.6", 0) -> "12345").
  • deno fmt --check and deno lint are clean.

…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.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant