diff --git a/packages/sdk38/src/coins.spec.ts b/packages/sdk38/src/coins.spec.ts new file mode 100644 index 00000000..91483913 --- /dev/null +++ b/packages/sdk38/src/coins.spec.ts @@ -0,0 +1,35 @@ +import { coin, coins } from "./coins"; + +describe("coins", () => { + describe("coin", () => { + it("works for basic values", () => { + expect(coin(123, "utoken")).toEqual({ amount: "123", denom: "utoken" }); + expect(coin(123.0, "utoken")).toEqual({ amount: "123", denom: "utoken" }); + expect(coin(Number.MAX_SAFE_INTEGER, "utoken")).toEqual({ + amount: "9007199254740991", + denom: "utoken", + }); + expect(coin(+0, "utoken")).toEqual({ amount: "0", denom: "utoken" }); + expect(coin(-0, "utoken")).toEqual({ amount: "0", denom: "utoken" }); + }); + + it("throws for non-safe-integer values", () => { + expect(() => coin(1.23, "utoken")).toThrow(); + expect(() => coin(NaN, "utoken")).toThrow(); + expect(() => coin(Number.POSITIVE_INFINITY, "utoken")).toThrow(); + expect(() => coin(Number.MAX_SAFE_INTEGER + 1, "utoken")).toThrow(); + }); + + it("throws for negative values", () => { + expect(() => coin(-1, "utoken")).toThrow(); + expect(() => coin(Number.MIN_SAFE_INTEGER, "utoken")).toThrow(); + expect(() => coin(Number.NEGATIVE_INFINITY, "utoken")).toThrow(); + }); + }); + + describe("coins", () => { + it("returns one element array of coin", () => { + expect(coins(123, "utoken")).toEqual([{ amount: "123", denom: "utoken" }]); + }); + }); +}); diff --git a/packages/sdk38/src/coins.ts b/packages/sdk38/src/coins.ts index 62f5f9ce..f14f996a 100644 --- a/packages/sdk38/src/coins.ts +++ b/packages/sdk38/src/coins.ts @@ -1,3 +1,5 @@ +import { Uint53 } from "@cosmjs/math"; + export interface Coin { readonly denom: string; readonly amount: string; @@ -5,7 +7,7 @@ export interface Coin { /** Creates a coin */ export function coin(amount: number, denom: string): Coin { - return { amount: amount.toString(), denom: denom }; + return { amount: new Uint53(amount).toString(), denom: denom }; } /** Creates a list of coins with one element */