From 048169fd3f49bd67df4463b055bda0c193712fd4 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Khac Date: Sat, 25 Jun 2022 23:13:09 +1200 Subject: [PATCH] refactor: stricter check for non-negative integers --- packages/math/src/decimal.spec.ts | 2 +- packages/math/src/decimal.ts | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index 446c8ce1..6f077075 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -32,7 +32,7 @@ describe("Decimal", () => { expect(Decimal.fromAtomics("44", 4).toString()).toEqual("0.0044"); }); - it("throws for atomics that are not non-negative", () => { + it("throws for atomics that are not non-negative integers", () => { expect(() => Decimal.fromAtomics("-1", 0)).toThrowError(/atomics must not be negative/i); }); }); diff --git a/packages/math/src/decimal.ts b/packages/math/src/decimal.ts index a0012a2e..0aac60ce 100644 --- a/packages/math/src/decimal.ts +++ b/packages/math/src/decimal.ts @@ -107,14 +107,14 @@ export class Decimal { }; private constructor(atomics: string, fractionalDigits: number) { - const _atomics = new BN(atomics); - - if (_atomics.isNeg()) { - throw new Error("Atomics must not be negative"); + if (!atomics.match(/^[0-9]+$/)) { + throw new Error( + "Invalid string format. Only non-negative integers in decimal representation suppored.", + ); } this.data = { - atomics: _atomics, + atomics: new BN(atomics), fractionalDigits: fractionalDigits, }; }