Add parseCoins

This commit is contained in:
Simon Warta 2020-08-11 12:12:57 +02:00
parent 98e4c7b958
commit 4f54b26ac3
6 changed files with 99 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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