Skip to content

Commit 8f6363f

Browse files
committed
SDK: Fix toUnits throwing error for values in scientific notation (#8112)
<!-- ## title your PR with this format: "[SDK/Dashboard/Portal] Feature/Fix: Concise title for the changes" If you did not copy the branch name from Linear, paste the issue tag here (format is TEAM-0000): ## Notes for the reviewer Anything important to call out? Be sure to also clarify these in your comments. ## How to test Unit tests, playground, etc. --> <!-- start pr-codex --> --- ## PR-Codex overview This PR enhances the `toUnits` function to handle both scientific notation and fixed-point notation, ensuring accurate conversion of token values with specified decimals. It also adds tests to verify the new functionality. ### Detailed summary - Modified `toUnits` function to check for scientific notation (e.g., "1e-18"). - Converted scientific notation to fixed-point notation using `Number(tokens).toFixed(decimals)`. - Added tests for `toUnits` to cover various scientific notation cases, including negative and positive exponents. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Unit conversion now accepts scientific notation (positive and negative exponents), normalizing values to the configured decimal precision for consistent fixed-point results. * **Tests** * Added comprehensive tests covering positive and negative exponent cases to validate parsing and conversion from scientific notation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent a83104e commit 8f6363f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

packages/thirdweb/src/utils/units.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,19 @@ describe("toUnits", () => {
199199
expect(toUnits("69.59000000059", 9)).toMatchInlineSnapshot("69590000001n");
200200
expect(toUnits("69.59000002359", 9)).toMatchInlineSnapshot("69590000024n");
201201
});
202+
203+
it("scientific notation", () => {
204+
// negative exponents
205+
expect(toUnits("1e-18", 18)).toMatchInlineSnapshot("1n");
206+
expect(toUnits("1e-16", 18)).toMatchInlineSnapshot("100n");
207+
expect(toUnits("1.23456789012345e-7", 18)).toMatchInlineSnapshot(
208+
"123456789012n",
209+
);
210+
// positive exponents
211+
expect(toUnits("1.234e3", 18)).toMatchInlineSnapshot(
212+
"1234000000000000000000n",
213+
);
214+
});
202215
});
203216

204217
describe("toWei", () => {

packages/thirdweb/src/utils/units.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ export function toEther(wei: bigint) {
7373
* @utils
7474
*/
7575
export function toUnits(tokens: string, decimals: number): bigint {
76+
if (tokens.includes("e")) {
77+
tokens = Number(tokens).toFixed(decimals);
78+
}
79+
7680
let [integerPart, fractionPart = ""] = tokens.split(".") as [string, string];
7781
const prefix = integerPart.startsWith("-") ? "-" : "";
7882
if (prefix) {

0 commit comments

Comments
 (0)