Skip to content

fix: don't silently use 0 when a wire price/size fails to parse (market order at 0) - #172

Merged
sonirico merged 1 commit into
sonirico:masterfrom
chiliec:fix/slippage-price-silent-zero
Aug 31, 2026
Merged

fix: don't silently use 0 when a wire price/size fails to parse (market order at 0)#172
sonirico merged 1 commit into
sonirico:masterfrom
chiliec:fix/slippage-price-silent-zero

Conversation

@chiliec

@chiliec chiliec commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Description

parseFloat returns 0.0 on any parse error (it swallows the error). Two call sites use it for financial values coming off the API, where a silent 0 is dangerous:

1. SlippagePrice — market order price. For a market order (px == nil) it parses the mid price:

price = parseFloat(midPriceStr)

A malformed mid price from AllMids becomes 0, and the caller places a market order priced at 0 with no error surfaced.

2. MarketClose — position size. It parses the position size:

szi := parseFloat(pos.Szi)

A malformed pos.Szi becomes 0, so size = abs(0) = 0 — a no-op / wrong close, again silently.

Fix

Add parseFloatStrict (error-returning) and use it at both sites. Both SlippagePrice and MarketClose already return an error, so the parse failure now propagates instead of turning into a 0. parseFloat is left unchanged for its existing display callers, so this is non-breaking.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)

Testing

  • Added TestParseFloatStrict — valid inputs match parseFloat; invalid inputs error instead of silently returning 0
  • go test ./ passes (full suite)
  • go vet ./ and gofmt clean

Code Quality

  • Follows project style
  • Self-reviewed
  • Commented the strict helper and why it exists
  • No new warnings

Additional Notes

First-time contributor. parseFloat's third caller is a display/formatting path where a 0 default is harmless, so I left it as-is — happy to convert that too if you'd prefer all parses to be strict.

parseFloat returns 0.0 on any parse error. Two financial call sites relied
on it for values coming off the API:

- SlippagePrice (market order price): a malformed mid price became a market
  order priced at 0, with no error.
- MarketClose (position size): a malformed pos.Szi became size 0 (a no-op /
  wrong close).

Add parseFloatStrict (error-returning) and use it at both sites, which both
already return an error. parseFloat is unchanged for its display callers, so
this is non-breaking. Adds a unit test contrasting the two parsers.
@chiliec
chiliec force-pushed the fix/slippage-price-silent-zero branch from 31b99d8 to aef9531 Compare August 31, 2026 09:51
@chiliec chiliec changed the title fix: don't silently place a market order at price 0 when mid price fails to parse fix: don't silently use 0 when a wire price/size fails to parse (market order at 0) Aug 31, 2026
@sonirico
sonirico merged commit cebf667 into sonirico:master Aug 31, 2026
2 checks passed
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.

2 participants