Add uint64ToNumber and uint64ToString

This commit is contained in:
Simon Warta 2020-07-25 15:09:28 +02:00
parent 4a8bbf014f
commit a69006b8c3
7 changed files with 114 additions and 0 deletions

View File

@ -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";

View File

@ -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";

View File

@ -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();
});
});
});

View File

@ -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();
}

View File

@ -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";

View File

@ -68,3 +68,4 @@ export {
TxsResponse,
} from "./base";
export { LcdApiArray, LcdClient, normalizeLcdApiArray } from "./lcdclient";
export { uint64ToNumber, uint64ToString } from "./utils";

14
packages/sdk38/types/lcdapi/utils.d.ts vendored Normal file
View File

@ -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;