mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Merge pull request #232 from CosmWasm/test-and-harden-coin-helper
Harden and test coin/coins helpers
This commit is contained in:
commit
b9cce63514
35
packages/sdk38/src/coins.spec.ts
Normal file
35
packages/sdk38/src/coins.spec.ts
Normal file
@ -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" }]);
|
||||
});
|
||||
});
|
||||
});
|
@ -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 */
|
||||
|
Loading…
x
Reference in New Issue
Block a user