Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Decimal can be constructed with a negative atomics #1188

Merged
merged 4 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/math/src/decimal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/math/src/decimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down