diff --git a/CHANGELOG.md b/CHANGELOG.md index 46e9e4c8..60eca0e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - @cosmjs/faucet: Log errors for failed send transactions. - @cosmjs/faucet: Add config variable `FAUCET_MEMO`. +- @cosmjs/launchpad: Add `parseCoins` helper. ## 0.22.1 (2020-08-11) diff --git a/packages/launchpad/src/coins.spec.ts b/packages/launchpad/src/coins.spec.ts index 91483913..5f74c4cb 100644 --- a/packages/launchpad/src/coins.spec.ts +++ b/packages/launchpad/src/coins.spec.ts @@ -1,4 +1,4 @@ -import { coin, coins } from "./coins"; +import { coin, coins, parseCoins } from "./coins"; describe("coins", () => { describe("coin", () => { @@ -32,4 +32,76 @@ describe("coins", () => { expect(coins(123, "utoken")).toEqual([{ amount: "123", denom: "utoken" }]); }); }); + + describe("parseCoins", () => { + it("works for empty", () => { + expect(parseCoins("")).toEqual([]); + }); + + it("works for one element", () => { + expect(parseCoins("7643ureef")).toEqual([ + { + amount: "7643", + denom: "ureef", + }, + ]); + }); + + it("works for two", () => { + expect(parseCoins("819966000ucosm,700000000ustake")).toEqual([ + { + amount: "819966000", + denom: "ucosm", + }, + { + amount: "700000000", + denom: "ustake", + }, + ]); + }); + + it("ignores empty elements", () => { + // start + expect(parseCoins(",819966000ucosm,700000000ustake")).toEqual([ + { + amount: "819966000", + denom: "ucosm", + }, + { + amount: "700000000", + denom: "ustake", + }, + ]); + // middle + expect(parseCoins("819966000ucosm,,700000000ustake")).toEqual([ + { + amount: "819966000", + denom: "ucosm", + }, + { + amount: "700000000", + denom: "ustake", + }, + ]); + // end + expect(parseCoins("819966000ucosm,700000000ustake,")).toEqual([ + { + amount: "819966000", + denom: "ucosm", + }, + { + amount: "700000000", + denom: "ustake", + }, + ]); + }); + + it("throws for invalid inputs", () => { + // denom missing + expect(() => parseCoins("3456")).toThrowError(/invalid coin string/i); + + // amount missing + expect(() => parseCoins("ucosm")).toThrowError(/invalid coin string/i); + }); + }); }); diff --git a/packages/launchpad/src/coins.ts b/packages/launchpad/src/coins.ts index f14f996a..6f7b5321 100644 --- a/packages/launchpad/src/coins.ts +++ b/packages/launchpad/src/coins.ts @@ -1,4 +1,4 @@ -import { Uint53 } from "@cosmjs/math"; +import { Uint53, Uint64 } from "@cosmjs/math"; export interface Coin { readonly denom: string; @@ -14,3 +14,21 @@ export function coin(amount: number, denom: string): Coin { export function coins(amount: number, denom: string): Coin[] { return [coin(amount, denom)]; } + +/** + * Takes a coins list like "819966000ucosm,700000000ustake" and parses it + */ +export function parseCoins(input: string): Coin[] { + return input + .replace(/\s/g, "") + .split(",") + .filter(Boolean) + .map((part) => { + const match = part.match(/^([0-9]+)([a-zA-Z]+)/); + if (!match) throw new Error("Got an invalid coin string"); + return { + amount: Uint64.fromString(match[1]).toString(), + denom: match[2], + }; + }); +} diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 6d6e2388..492622d6 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -2,7 +2,7 @@ import * as logs from "./logs"; export { logs }; export { pubkeyToAddress, rawSecp256k1PubkeyToAddress } from "./address"; -export { Coin, coin, coins } from "./coins"; +export { Coin, coin, coins, parseCoins } from "./coins"; export { Account, diff --git a/packages/launchpad/types/coins.d.ts b/packages/launchpad/types/coins.d.ts index 9208f3e2..2c956390 100644 --- a/packages/launchpad/types/coins.d.ts +++ b/packages/launchpad/types/coins.d.ts @@ -6,3 +6,7 @@ export interface Coin { export declare function coin(amount: number, denom: string): Coin; /** Creates a list of coins with one element */ export declare function coins(amount: number, denom: string): Coin[]; +/** + * Takes a coins list like "819966000ucosm,700000000ustake" and parses it + */ +export declare function parseCoins(input: string): Coin[]; diff --git a/packages/launchpad/types/index.d.ts b/packages/launchpad/types/index.d.ts index 03c9a87d..c6c21c94 100644 --- a/packages/launchpad/types/index.d.ts +++ b/packages/launchpad/types/index.d.ts @@ -1,7 +1,7 @@ import * as logs from "./logs"; export { logs }; export { pubkeyToAddress, rawSecp256k1PubkeyToAddress } from "./address"; -export { Coin, coin, coins } from "./coins"; +export { Coin, coin, coins, parseCoins } from "./coins"; export { Account, assertIsPostTxSuccess,