fix(react-sdk): correct sign handling in parseAssetAmount / formatAssetAmount - #275
Open
batuhankocyigit wants to merge 5 commits into
Open
batuhankocyigit wants to merge 5 commits into
batuhankocyigit wants to merge 5 commits into
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.
fix(react-sdk): correct sign handling in parseAssetAmount / formatAssetAmount for negative amounts
The bug
formatAssetAmount/parseAssetAmount(exported from@miden-sdk/react,src/utils/amounts.ts) mishandle negative values.parseAssetAmountsplits the input on.and combines the whole andfraction parts as
BigInt(whole) * factor + BigInt(fraction). When theinput is negative, the minus sign only affects the whole part, not the
combined magnitude:
formatAssetAmounthas the mirror problem: dividing/modding a negativeBigIntby a positive factor yields a negative remainder, which getsconcatenated after the decimal point, producing an invalid string:
Both functions are part of the package's public API surface
(
export { formatAssetAmount, parseAssetAmount } from "./utils/amounts"insrc/index.ts) andformatAssetAmountis also used internally inutils/notes.tsto render note amounts, so any consumer surfacing anegative amount (e.g. a balance delta, a signed transfer amount, a refund)
gets silently wrong output today — no throw, no warning.
The fix
Extract the sign once up front, operate on the absolute magnitude, and
re-apply the sign to the final result in both functions. Minimal, symmetric
change, no public API/signature change.
Testing
numbers, negative fractional amounts, and the "-0.x" edge case, plus a
round-trip test (
parseAssetAmount→formatAssetAmount→ same string)for negative inputs.
(853/853 passing, 845 existing + 8 new).
Notes for reviewers
unmodified).