From 6f22e00488dc1ffeb5ff3400465fdefef978ad99 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Khac Date: Wed, 22 Jun 2022 12:05:34 +1200 Subject: [PATCH 1/4] Fix `Decimal` can be constructed with a negative atomics --- packages/math/src/decimal.spec.ts | 4 ++++ packages/math/src/decimal.ts | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index 52b876f7d5..446c8ce10f 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -31,6 +31,10 @@ 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", () => { + expect(() => Decimal.fromAtomics("-1", 0)).toThrowError(/atomics must not be negative/i); + }); }); describe("fromUserInput", () => { diff --git a/packages/math/src/decimal.ts b/packages/math/src/decimal.ts index 91ec2cc181..a0012a2ed8 100644 --- a/packages/math/src/decimal.ts +++ b/packages/math/src/decimal.ts @@ -107,8 +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"); + } + this.data = { - atomics: new BN(atomics), + atomics: _atomics, fractionalDigits: fractionalDigits, }; } From 048169fd3f49bd67df4463b055bda0c193712fd4 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Khac Date: Sat, 25 Jun 2022 23:13:09 +1200 Subject: [PATCH 2/4] 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 446c8ce10f..6f0770751e 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 a0012a2ed8..0aac60ce3d 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, }; } From bd8c2b8c184db48fdad029a2da22d002da726966 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Khac Date: Sat, 25 Jun 2022 23:17:06 +1200 Subject: [PATCH 3/4] chore: update test & fix spelling --- packages/math/src/decimal.spec.ts | 4 +++- packages/math/src/decimal.ts | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index 6f0770751e..e8f7033054 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -33,7 +33,9 @@ describe("Decimal", () => { }); it("throws for atomics that are not non-negative integers", () => { - expect(() => Decimal.fromAtomics("-1", 0)).toThrowError(/atomics must not be negative/i); + expect(() => Decimal.fromAtomics("-1", 0)).toThrowError( + /invalid string format. Only non-negative integers in decimal representation supported/i, + ); }); }); diff --git a/packages/math/src/decimal.ts b/packages/math/src/decimal.ts index 0aac60ce3d..0a6a798b2a 100644 --- a/packages/math/src/decimal.ts +++ b/packages/math/src/decimal.ts @@ -109,7 +109,7 @@ 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 suppored.", + "Invalid string format. Only non-negative integers in decimal representation supported.", ); } From 3175384a1f317a5e6f4528a3169ad8daa22cdbc5 Mon Sep 17 00:00:00 2001 From: Tien Nguyen Khac Date: Sat, 25 Jun 2022 23:18:41 +1200 Subject: [PATCH 4/4] test: use exact string match for error --- packages/math/src/decimal.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/math/src/decimal.spec.ts b/packages/math/src/decimal.spec.ts index e8f7033054..a37783913b 100644 --- a/packages/math/src/decimal.spec.ts +++ b/packages/math/src/decimal.spec.ts @@ -34,7 +34,7 @@ describe("Decimal", () => { 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/i, + "Invalid string format. Only non-negative integers in decimal representation supported.", ); }); });