Fix float rounding drift in order amount calculations - #72
Open
Cassxbt wants to merge 1 commit into
Open
Conversation
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.
Summary
Motivation
Float arithmetic can round valid cent-boundary order sizes down before token decimal conversion. For example, a 16.90 size at 0.30 price can produce 5,067,000 instead of 5,070,000 maker amount, creating invalid sub-cent order amounts.
The Decimal rounding uses ROUND_HALF_UP for normal rounding to match the positive-value behavior of the TypeScript v2 client's Math.round path.
Closes #68.
Tests
Note
Medium Risk
Touches core order amount rounding/conversion logic used to compute maker/taker amounts; subtle rounding behavior changes could affect order validity at certain tick sizes, but scope is contained and covered by new regression tests.
Overview
Switches order-builder rounding helpers from float/math-based rounding to
Decimal.quantizewith explicitROUND_FLOOR/ROUND_CEILING/ROUND_HALF_UP, and updatesto_token_decimals/decimal_placesto avoid float drift when scaling to 6-decimal token units.Adds regression tests ensuring limit buy/sell amount calculations at
0.01tick size preserve cent-boundary sizes (e.g.,16.90at0.30) when converted to maker/taker token decimals.Reviewed by Cursor Bugbot for commit f0c6989. Bugbot is set up for automated code reviews on this repo. Configure here.