From a69006b8c3d253f0143c01bd6847e568e367e57f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 25 Jul 2020 15:09:28 +0200 Subject: [PATCH] Add uint64ToNumber and uint64ToString --- packages/sdk38/src/index.ts | 2 + packages/sdk38/src/lcdapi/index.ts | 5 ++ packages/sdk38/src/lcdapi/utils.spec.ts | 67 +++++++++++++++++++++++++ packages/sdk38/src/lcdapi/utils.ts | 23 +++++++++ packages/sdk38/types/index.d.ts | 2 + packages/sdk38/types/lcdapi/index.d.ts | 1 + packages/sdk38/types/lcdapi/utils.d.ts | 14 ++++++ 7 files changed, 114 insertions(+) create mode 100644 packages/sdk38/src/lcdapi/utils.spec.ts create mode 100644 packages/sdk38/src/lcdapi/utils.ts create mode 100644 packages/sdk38/types/lcdapi/utils.d.ts diff --git a/packages/sdk38/src/index.ts b/packages/sdk38/src/index.ts index 4afe6c26..305cdbee 100644 --- a/packages/sdk38/src/index.ts +++ b/packages/sdk38/src/index.ts @@ -78,6 +78,8 @@ export { StakingPoolResponse, SupplyExtension, TxsResponse, + uint64ToNumber, + uint64ToString, } from "./lcdapi"; export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs"; export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey"; diff --git a/packages/sdk38/src/lcdapi/index.ts b/packages/sdk38/src/lcdapi/index.ts index 5a3fc580..767f3cd6 100644 --- a/packages/sdk38/src/lcdapi/index.ts +++ b/packages/sdk38/src/lcdapi/index.ts @@ -77,3 +77,8 @@ export { TxsResponse, } from "./base"; export { LcdApiArray, LcdClient, normalizeLcdApiArray } from "./lcdclient"; + +// +// Utils for interacting with the client/API +// +export { uint64ToNumber, uint64ToString } from "./utils"; diff --git a/packages/sdk38/src/lcdapi/utils.spec.ts b/packages/sdk38/src/lcdapi/utils.spec.ts new file mode 100644 index 00000000..a4628096 --- /dev/null +++ b/packages/sdk38/src/lcdapi/utils.spec.ts @@ -0,0 +1,67 @@ +import { uint64ToNumber, uint64ToString } from "./utils"; + +describe("utils", () => { + describe("uint64ToNumber", () => { + it("works for numeric inputs", () => { + expect(uint64ToNumber(0)).toEqual(0); + expect(uint64ToNumber(1)).toEqual(1); + expect(uint64ToNumber(Number.MAX_SAFE_INTEGER)).toEqual(Number.MAX_SAFE_INTEGER); + }); + + it("works for string inputs", () => { + expect(uint64ToNumber("0")).toEqual(0); + expect(uint64ToNumber("1")).toEqual(1); + expect(uint64ToNumber("9007199254740991")).toEqual(Number.MAX_SAFE_INTEGER); + }); + + it("throws for invalid numbers", () => { + expect(() => uint64ToNumber(NaN)).toThrow(); + expect(() => uint64ToNumber(1.1)).toThrow(); + expect(() => uint64ToNumber(-1)).toThrow(); + expect(() => uint64ToNumber(Number.MAX_SAFE_INTEGER + 1)).toThrow(); + }); + + it("throws for invalid strings", () => { + expect(() => uint64ToNumber("")).toThrow(); + expect(() => uint64ToNumber("0x22")).toThrow(); + expect(() => uint64ToNumber("-1")).toThrow(); + expect(() => uint64ToNumber("1.1")).toThrow(); + expect(() => uint64ToNumber("9007199254740992")).toThrow(); + }); + }); + + describe("uint64ToString", () => { + it("works for numeric inputs", () => { + expect(uint64ToString(0)).toEqual("0"); + expect(uint64ToString(1)).toEqual("1"); + expect(uint64ToString(Number.MAX_SAFE_INTEGER)).toEqual("9007199254740991"); + }); + + it("works for string inputs", () => { + expect(uint64ToString("0")).toEqual("0"); + expect(uint64ToString("1")).toEqual("1"); + expect(uint64ToString("9007199254740991")).toEqual("9007199254740991"); + }); + + it("works for large string values", () => { + // for the string -> string version, the full uint64 range is supported + expect(uint64ToString("9007199254740992")).toEqual("9007199254740992"); + expect(uint64ToString("18446744073709551615")).toEqual("18446744073709551615"); + }); + + it("throws for invalid numbers", () => { + expect(() => uint64ToString(NaN)).toThrow(); + expect(() => uint64ToString(1.1)).toThrow(); + expect(() => uint64ToString(-1)).toThrow(); + expect(() => uint64ToString(Number.MAX_SAFE_INTEGER + 1)).toThrow(); + }); + + it("throws for invalid strings", () => { + expect(() => uint64ToString("")).toThrow(); + expect(() => uint64ToString("0x22")).toThrow(); + expect(() => uint64ToString("-1")).toThrow(); + expect(() => uint64ToString("1.1")).toThrow(); + expect(() => uint64ToString("18446744073709551616")).toThrow(); + }); + }); +}); diff --git a/packages/sdk38/src/lcdapi/utils.ts b/packages/sdk38/src/lcdapi/utils.ts new file mode 100644 index 00000000..4268fc13 --- /dev/null +++ b/packages/sdk38/src/lcdapi/utils.ts @@ -0,0 +1,23 @@ +import { Uint64 } from "@cosmjs/math"; + +/** + * Converts an integer expressed as number or string to a number. + * Throws if input is not a valid uint64 or if the value exceeds MAX_SAFE_INTEGER. + * + * This is needed for supporting Comsos SDK 0.37/0.38/0.39 with one client. + */ +export function uint64ToNumber(input: number | string): number { + const value = typeof input === "number" ? Uint64.fromNumber(input) : Uint64.fromString(input); + return value.toNumber(); +} + +/** + * Converts an integer expressed as number or string to a string. + * Throws if input is not a valid uint64. + * + * This is needed for supporting Comsos SDK 0.37/0.38/0.39 with one client. + */ +export function uint64ToString(input: number | string): string { + const value = typeof input === "number" ? Uint64.fromNumber(input) : Uint64.fromString(input); + return value.toString(); +} diff --git a/packages/sdk38/types/index.d.ts b/packages/sdk38/types/index.d.ts index d84be9f2..b375c43e 100644 --- a/packages/sdk38/types/index.d.ts +++ b/packages/sdk38/types/index.d.ts @@ -76,6 +76,8 @@ export { StakingPoolResponse, SupplyExtension, TxsResponse, + uint64ToNumber, + uint64ToString, } from "./lcdapi"; export { isMsgDelegate, isMsgSend, Msg, MsgDelegate, MsgSend } from "./msgs"; export { decodeBech32Pubkey, encodeBech32Pubkey, encodeSecp256k1Pubkey } from "./pubkey"; diff --git a/packages/sdk38/types/lcdapi/index.d.ts b/packages/sdk38/types/lcdapi/index.d.ts index 6a06331d..38460f39 100644 --- a/packages/sdk38/types/lcdapi/index.d.ts +++ b/packages/sdk38/types/lcdapi/index.d.ts @@ -68,3 +68,4 @@ export { TxsResponse, } from "./base"; export { LcdApiArray, LcdClient, normalizeLcdApiArray } from "./lcdclient"; +export { uint64ToNumber, uint64ToString } from "./utils"; diff --git a/packages/sdk38/types/lcdapi/utils.d.ts b/packages/sdk38/types/lcdapi/utils.d.ts new file mode 100644 index 00000000..a7be38a5 --- /dev/null +++ b/packages/sdk38/types/lcdapi/utils.d.ts @@ -0,0 +1,14 @@ +/** + * Converts an integer expressed as number or string to a number. + * Throws if input is not a valid uint64 or if the value exceeds MAX_SAFE_INTEGER. + * + * This is needed for supporting Comsos SDK 0.37/0.38/0.39 with one client. + */ +export declare function uint64ToNumber(input: number | string): number; +/** + * Converts an integer expressed as number or string to a string. + * Throws if input is not a valid uint64. + * + * This is needed for supporting Comsos SDK 0.37/0.38/0.39 with one client. + */ +export declare function uint64ToString(input: number | string): string;