diff --git a/packages/launchpad/src/fee.spec.ts b/packages/launchpad/src/fee.spec.ts index 7a94238a..4f5c0163 100644 --- a/packages/launchpad/src/fee.spec.ts +++ b/packages/launchpad/src/fee.spec.ts @@ -12,12 +12,38 @@ describe("GasPrice", () => { }); }); - it("can be constructed from a config string", () => { - const inputs = ["3.14", "3", "0.14"]; - inputs.forEach((input) => { - const gasPrice = GasPrice.fromString(`${input}utest`); - expect(gasPrice.amount.toString()).toEqual(input); - expect(gasPrice.denom).toEqual("utest"); + describe("fromString", () => { + it("works", () => { + const inputs = ["3.14", "3", "0.14"]; + inputs.forEach((input) => { + const gasPrice = GasPrice.fromString(`${input}utest`); + expect(gasPrice.amount.toString()).toEqual(input); + expect(gasPrice.denom).toEqual("utest"); + }); + }); + + it("errors for invalid gas price", () => { + // Checks basic format + expect(() => GasPrice.fromString("")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("utkn")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("@utkn")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("234")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("-234tkn")).toThrowError(/Invalid gas price string/i); + // Checks details of + expect(() => GasPrice.fromString("234t")).toThrowError( + /denomination must be between 3 and 127 characters/i, + ); + expect(() => GasPrice.fromString("234tt")).toThrowError( + /denomination must be between 3 and 127 characters/i, + ); + expect(() => + GasPrice.fromString( + "234tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt", + ), + ).toThrowError(/denomination must be between 3 and 127 characters/i); + // Checks details of + expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Fractional part missing/i); + expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i); }); }); }); diff --git a/packages/launchpad/src/fee.ts b/packages/launchpad/src/fee.ts index c76062cc..f1c4e8fc 100644 --- a/packages/launchpad/src/fee.ts +++ b/packages/launchpad/src/fee.ts @@ -13,7 +13,7 @@ export class GasPrice { } public static fromString(gasPrice: string): GasPrice { - const matchResult = gasPrice.match(/^(?.+?)(?[a-z]+)$/); + const matchResult = gasPrice.match(/^(?[0-9.]+?)(?[a-z]+)$/); if (!matchResult) { throw new Error("Invalid gas price string"); } diff --git a/packages/stargate/src/fee.spec.ts b/packages/stargate/src/fee.spec.ts index 7a94238a..4f5c0163 100644 --- a/packages/stargate/src/fee.spec.ts +++ b/packages/stargate/src/fee.spec.ts @@ -12,12 +12,38 @@ describe("GasPrice", () => { }); }); - it("can be constructed from a config string", () => { - const inputs = ["3.14", "3", "0.14"]; - inputs.forEach((input) => { - const gasPrice = GasPrice.fromString(`${input}utest`); - expect(gasPrice.amount.toString()).toEqual(input); - expect(gasPrice.denom).toEqual("utest"); + describe("fromString", () => { + it("works", () => { + const inputs = ["3.14", "3", "0.14"]; + inputs.forEach((input) => { + const gasPrice = GasPrice.fromString(`${input}utest`); + expect(gasPrice.amount.toString()).toEqual(input); + expect(gasPrice.denom).toEqual("utest"); + }); + }); + + it("errors for invalid gas price", () => { + // Checks basic format + expect(() => GasPrice.fromString("")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("utkn")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("@utkn")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("234")).toThrowError(/Invalid gas price string/i); + expect(() => GasPrice.fromString("-234tkn")).toThrowError(/Invalid gas price string/i); + // Checks details of + expect(() => GasPrice.fromString("234t")).toThrowError( + /denomination must be between 3 and 127 characters/i, + ); + expect(() => GasPrice.fromString("234tt")).toThrowError( + /denomination must be between 3 and 127 characters/i, + ); + expect(() => + GasPrice.fromString( + "234tttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt", + ), + ).toThrowError(/denomination must be between 3 and 127 characters/i); + // Checks details of + expect(() => GasPrice.fromString("3.utkn")).toThrowError(/Fractional part missing/i); + expect(() => GasPrice.fromString("..utkn")).toThrowError(/More than one separator found/i); }); }); }); diff --git a/packages/stargate/src/fee.ts b/packages/stargate/src/fee.ts index 628c0350..a41c7ce2 100644 --- a/packages/stargate/src/fee.ts +++ b/packages/stargate/src/fee.ts @@ -20,7 +20,7 @@ export class GasPrice { } public static fromString(gasPrice: string): GasPrice { - const matchResult = gasPrice.match(/^(?.+?)(?[a-z]+)$/); + const matchResult = gasPrice.match(/^(?[0-9.]+?)(?[a-z]+)$/); if (!matchResult) { throw new Error("Invalid gas price string"); }