0xMiden / 0xMiden/web-sdk

formatAssetAmount / parseAssetAmount mishandle negative amounts

Open Beginner friendly
#282 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1
Forks
21
Avg merge
12h 14m
Merged PRs (30d)
41

Description

`formatAssetAmount` / `parseAssetAmount` in `packages/react-sdk/src/utils/amounts.ts`
(also exported from the package's public API in `src/index.ts`) mishandle
negative values.

**parseAssetAmount** splits the input on `.` and combines whole + fraction as
`BigInt(whole) * factor + BigInt(fraction)`. When the input is negative, the
minus sign only affects the whole part, not the combined magnitude:

parseAssetAmount("-5.25", 2) // => -475n, expected -525n
parseAssetAmount("-0.5", 2) // => 50n, expected -50n
// (sign lost entirely — BigInt("-0") === 0n)

**formatAssetAmount** has the mirror problem: dividing/modding a negative
BigInt by a positive factor yields a negative remainder, producing an
invalid string:

formatAssetAmount(-525n, 2) // => "-5.-25"

No throw, no warning — just silently wrong output.

I noticed the CONTRIBUTING process asks contributors to wait for an issue
to be assigned before opening a PR apologies, I missed that and already
opened a PR with the fix and regression tests: #275. Happy to close it and
wait for assignment if you'd prefer I follow the process from scratch
just wanted to flag the bug here too and let you decide. No pressure
either way, thanks for the awesome project!

Contributor guide

Open the contributing guide

Research direction

The bug is in `packages/react-sdk/src/utils/amounts.ts` in the `formatAssetAmount` and `parseAssetAmount` functions. Start by reading the existing code to understand the current logic for handling negative numbers. Write tests to reproduce the described failures, then modify the functions to correctly process negative amounts. Verify the fix by running the test suite.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend-api-design
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.