diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index 52b876f7d5..a37783913b 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -31,6 +31,12 @@ describe("Decimal", () => { expect(Decimal.fromAtomics("44", 3).toString()).toEqual("0.044"); expect(Decimal.fromAtomics("44", 4).toString()).toEqual("0.0044"); }); + + it("throws for atomics that are not non-negative integers", () => { + expect(() => Decimal.fromAtomics("-1", 0)).toThrowError( + "Invalid string format. Only non-negative integers in decimal representation supported.", + ); + }); }); describe("fromUserInput", () => { diff --git a/packages/math/src/decimal.ts b/packages/math/src/decimal.ts index 91ec2cc181..0a6a798b2a 100644 --- a/packages/math/src/decimal.ts +++ b/packages/math/src/decimal.ts @@ -107,6 +107,12 @@ export class Decimal { }; private constructor(atomics: string, fractionalDigits: number) { + if (!atomics.match(/^[0-9]+$/)) { + throw new Error( + "Invalid string format. Only non-negative integers in decimal representation supported.", + ); + } + this.data = { atomics: new BN(atomics), fractionalDigits: fractionalDigits,