Skip to content

feat(utils): add parseUnits and formatUnits for any number of decimals - #1768

Merged
PhilippeR26 merged 1 commit into
starknet-io:developfrom
PhilippeR26:feat/parseUnit
Nov 26, 2026
Merged

PhilippeR26 merged 1 commit into
starknet-io:developfrom
PhilippeR26:feat/parseUnit

Conversation

@PhilippeR26

Copy link
Copy Markdown
Collaborator

Motivation and Resolution

starknet.js could only convert amounts with 18 decimals, through units() (STRK ↔ fri). A token with another number of decimals (6 for many stablecoins, for example) could not be converted, and examples had to write amounts by hand (1n * 10n ** 18n).

Resolution: two new functions, with the names used by viem and ethers.

  • parseUnits(value: string, decimals: BigNumberish, tokenName?: string): bigint converts a decimal amount written as text ('1.5') into an integer in the smallest unit of a token.
  • formatUnits(value: BigNumberish, decimals: BigNumberish, tokenName?: string): string converts such an integer back into a decimal amount, to display it.

They follow the rules of units(): an optional minus sign, then digits with at most one dot; extra decimals are refused unless they are zeros; the output has no trailing zero. decimals is an integer from 0 to 255 (the decimals of an ERC-20 token is a u8); it accepts a bigint, as a contract call returns it, or a hex string, as a node returns it. The optional tokenName is written in the error messages ('1,5' is not a valid USDC amount).

parseUnits only reads text, on purpose: an amount read on-chain is already in the smallest unit (a bigint from a contract call, a hex string from a node). Passed to parseUnits by mistake, it would be multiplied by 10^decimals without an error. A JavaScript number is refused too, as it can be inexact (0.1 + 0.2). The JSDoc explains this choice.

units() now uses the same internal functions. Its results do not change: the old and new versions give the same result, or the same refusal, on 400,000 random and edge-case inputs. The checks reuse isBigNumberish, assertInRange, RANGE_U8 and assert.

RPC version (if applicable)

N/A

Usage related changes

  • New parseUnits and formatUnits, exported from the package root.
  • The error messages of units() no longer start with units: , and some wording changes: '1.2.3' is not a valid strk amount instead of units: '1.2.3' is not a strk amount. Results and accepted inputs do not change.
  • The ERC20 guide (use_ERC20.md, "Interact with an ERC20") reads decimals() and uses parseUnits / formatUnits for token amounts.

Development related changes

  • New __tests__/utils/units.test.ts (44 tests): the tests of units() move there from __tests__/utils/utils.test.ts, split into tests with one goal each, and the tests of parseUnits and formatUnits are added.

Checklist:

  • Performed a self-review of the code
  • Rebased to the last commit of the target branch (or merged it into my branch)
  • Documented the changes in code (API docs will be generated automatically)
  • Updated the tests
  • All tests are passing

parseUnits(value, decimals, tokenName?) converts a decimal text into a bigint in the smallest unit of a token, and formatUnits(value, decimals, tokenName?) does the reverse, for decimals from 0 to 255. They follow the rules of units(). parseUnits only reads text: an amount read on-chain, already in the smallest unit, would otherwise be multiplied by 10^decimals without an error.

units() now shares their code and returns the same results; its error messages lose the "units: " prefix, e.g. "'1.2.3' is not a valid strk amount".

The tests of units() move from utils.test.ts to the new units.test.ts, and the ERC20 guide uses the two functions to write and display token amounts.
@PhilippeR26
PhilippeR26 merged commit 3965479 into starknet-io:develop Nov 26, 2026
9 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 26, 2026
# [11.10.0](v11.9.3...v11.10.0) (2026-11-26)

### Features

* **utils:** add parseUnits and formatUnits for any number of decimals ([#1768](#1768)) ([3965479](3965479))
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 11.10.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant