Repository navigation
feat(utils): add parseUnits and formatUnits for any number of decimals - #1768
Merged
Merged
Conversation
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.
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))
Contributor
|
🎉 This PR is included in version 11.10.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.
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): bigintconverts 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): stringconverts 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.decimalsis an integer from 0 to 255 (thedecimalsof an ERC-20 token is au8); it accepts a bigint, as a contract call returns it, or a hex string, as a node returns it. The optionaltokenNameis written in the error messages ('1,5' is not a valid USDC amount).parseUnitsonly 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 toparseUnitsby 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 reuseisBigNumberish,assertInRange,RANGE_U8andassert.RPC version (if applicable)
N/A
Usage related changes
parseUnitsandformatUnits, exported from the package root.units()no longer start withunits:, and some wording changes:'1.2.3' is not a valid strk amountinstead ofunits: '1.2.3' is not a strk amount. Results and accepted inputs do not change.use_ERC20.md, "Interact with an ERC20") readsdecimals()and usesparseUnits/formatUnitsfor token amounts.Development related changes
__tests__/utils/units.test.ts(44 tests): the tests ofunits()move there from__tests__/utils/utils.test.ts, split into tests with one goal each, and the tests ofparseUnitsandformatUnitsare added.Checklist: