mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
Add parseBankToken/parseBankTokens
This commit is contained in:
parent
1e347d1de5
commit
da4648374a
70
packages/faucet/src/tokens.spec.ts
Normal file
70
packages/faucet/src/tokens.spec.ts
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import { parseBankToken, parseBankTokens } from "./tokens";
|
||||||
|
|
||||||
|
describe("tokens", () => {
|
||||||
|
describe("parseBankToken", () => {
|
||||||
|
it("works", () => {
|
||||||
|
expect(parseBankToken("COSM=10^6ucosm")).toEqual({
|
||||||
|
tickerSymbol: "COSM",
|
||||||
|
fractionalDigits: 6,
|
||||||
|
denom: "ucosm",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("parseBankTokens", () => {
|
||||||
|
it("works for one", () => {
|
||||||
|
expect(parseBankTokens("COSM=10^6ucosm")).toEqual([
|
||||||
|
{
|
||||||
|
tickerSymbol: "COSM",
|
||||||
|
fractionalDigits: 6,
|
||||||
|
denom: "ucosm",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("works for two", () => {
|
||||||
|
expect(parseBankTokens("COSM=10^6ucosm,STAKE=10^3mstake")).toEqual([
|
||||||
|
{
|
||||||
|
tickerSymbol: "COSM",
|
||||||
|
fractionalDigits: 6,
|
||||||
|
denom: "ucosm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tickerSymbol: "STAKE",
|
||||||
|
fractionalDigits: 3,
|
||||||
|
denom: "mstake",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores whitespace", () => {
|
||||||
|
expect(parseBankTokens("COSM=10^6ucosm, STAKE=10^3mstake\n")).toEqual([
|
||||||
|
{
|
||||||
|
tickerSymbol: "COSM",
|
||||||
|
fractionalDigits: 6,
|
||||||
|
denom: "ucosm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tickerSymbol: "STAKE",
|
||||||
|
fractionalDigits: 3,
|
||||||
|
denom: "mstake",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ignores empty elements", () => {
|
||||||
|
expect(parseBankTokens("COSM=10^6ucosm,STAKE=10^3mstake,")).toEqual([
|
||||||
|
{
|
||||||
|
tickerSymbol: "COSM",
|
||||||
|
fractionalDigits: 6,
|
||||||
|
denom: "ucosm",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
tickerSymbol: "STAKE",
|
||||||
|
fractionalDigits: 3,
|
||||||
|
denom: "mstake",
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,3 +1,5 @@
|
|||||||
|
import { Uint53 } from "@cosmjs/math";
|
||||||
|
|
||||||
export interface BankTokenMeta {
|
export interface BankTokenMeta {
|
||||||
readonly denom: string;
|
readonly denom: string;
|
||||||
/**
|
/**
|
||||||
@ -15,3 +17,26 @@ export interface BankTokenMeta {
|
|||||||
*/
|
*/
|
||||||
readonly fractionalDigits: number;
|
readonly fractionalDigits: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const parseBankTokenPattern = /^([a-zA-Z]{2,20})=10\^([0-9]+)([a-zA-Z]{2,20})$/;
|
||||||
|
|
||||||
|
export function parseBankToken(input: string): BankTokenMeta {
|
||||||
|
const match = input.match(parseBankTokenPattern);
|
||||||
|
if (!match) {
|
||||||
|
throw new Error("Token could not be parsed. Format: DISPLAY=10^DIGITSbase, e.g. ATOM=10^6uatom");
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
tickerSymbol: match[1],
|
||||||
|
fractionalDigits: Uint53.fromString(match[2]).toNumber(),
|
||||||
|
denom: match[3],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function parseBankTokens(input: string): BankTokenMeta[] {
|
||||||
|
return input
|
||||||
|
.trim()
|
||||||
|
.split(",")
|
||||||
|
.map((part) => part.trim())
|
||||||
|
.filter((part) => part !== "")
|
||||||
|
.map((part) => parseBankToken(part));
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user