diff --git a/packages/thirdweb/src/utils/units.test.ts b/packages/thirdweb/src/utils/units.test.ts index 147a15c8900..439a9f43a35 100644 --- a/packages/thirdweb/src/utils/units.test.ts +++ b/packages/thirdweb/src/utils/units.test.ts @@ -199,6 +199,19 @@ describe("toUnits", () => { expect(toUnits("69.59000000059", 9)).toMatchInlineSnapshot("69590000001n"); expect(toUnits("69.59000002359", 9)).toMatchInlineSnapshot("69590000024n"); }); + + it("scientific notation", () => { + // negative exponents + expect(toUnits("1e-18", 18)).toMatchInlineSnapshot("1n"); + expect(toUnits("1e-16", 18)).toMatchInlineSnapshot("100n"); + expect(toUnits("1.23456789012345e-7", 18)).toMatchInlineSnapshot( + "123456789012n", + ); + // positive exponents + expect(toUnits("1.234e3", 18)).toMatchInlineSnapshot( + "1234000000000000000000n", + ); + }); }); describe("toWei", () => { diff --git a/packages/thirdweb/src/utils/units.ts b/packages/thirdweb/src/utils/units.ts index a9a2d56a999..4d049e59d35 100644 --- a/packages/thirdweb/src/utils/units.ts +++ b/packages/thirdweb/src/utils/units.ts @@ -73,6 +73,10 @@ export function toEther(wei: bigint) { * @utils */ export function toUnits(tokens: string, decimals: number): bigint { + if (tokens.includes("e")) { + tokens = Number(tokens).toFixed(decimals); + } + let [integerPart, fractionPart = ""] = tokens.split(".") as [string, string]; const prefix = integerPart.startsWith("-") ? "-" : ""; if (prefix) {