launchpad: Use coins helpers from amino

This commit is contained in:
willclarktech 2021-03-24 18:29:27 +01:00
parent 41f6081363
commit 8ede6d3272
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
10 changed files with 10 additions and 153 deletions

View File

@ -1,107 +0,0 @@
import { coin, coins, parseCoins } from "./coins";
describe("coins", () => {
describe("coin", () => {
it("works for basic values", () => {
expect(coin(123, "utoken")).toEqual({ amount: "123", denom: "utoken" });
expect(coin(123.0, "utoken")).toEqual({ amount: "123", denom: "utoken" });
expect(coin(Number.MAX_SAFE_INTEGER, "utoken")).toEqual({
amount: "9007199254740991",
denom: "utoken",
});
expect(coin(+0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
expect(coin(-0, "utoken")).toEqual({ amount: "0", denom: "utoken" });
});
it("throws for non-safe-integer values", () => {
expect(() => coin(1.23, "utoken")).toThrow();
expect(() => coin(NaN, "utoken")).toThrow();
expect(() => coin(Number.POSITIVE_INFINITY, "utoken")).toThrow();
expect(() => coin(Number.MAX_SAFE_INTEGER + 1, "utoken")).toThrow();
});
it("throws for negative values", () => {
expect(() => coin(-1, "utoken")).toThrow();
expect(() => coin(Number.MIN_SAFE_INTEGER, "utoken")).toThrow();
expect(() => coin(Number.NEGATIVE_INFINITY, "utoken")).toThrow();
});
});
describe("coins", () => {
it("returns one element array of coin", () => {
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,30 +0,0 @@
import { Coin } from "@cosmjs/amino";
import { Uint53, Uint64 } from "@cosmjs/math";
/** Creates a coin */
export function coin(amount: number, denom: string): Coin {
return { amount: new Uint53(amount).toString(), denom: denom };
}
/** Creates a list of coins with one element */
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

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { coins } from "./coins";
import { CosmosClient, isBroadcastTxFailure } from "./cosmosclient";
import { LcdClient } from "./lcdapi";
import { isMsgSend, MsgSend } from "./msgs";

View File

@ -1,8 +1,6 @@
import { StdFee } from "@cosmjs/amino";
import { coins, StdFee } from "@cosmjs/amino";
import { Decimal, Uint53 } from "@cosmjs/math";
import { coins } from "./coins";
export type FeeTable = Record<string, StdFee>;
export class GasPrice {

View File

@ -5,6 +5,8 @@ export {
AminoMsg as Msg,
AminoSignResponse,
Coin,
coin,
coins,
KdfConfiguration,
OfflineAminoSigner as OfflineSigner,
Secp256k1HdWallet,
@ -23,6 +25,7 @@ export {
executeKdf,
makeCosmoshubPath,
makeSignDoc,
parseCoins,
pubkeyToAddress,
pubkeyType,
serializeSignDoc,
@ -34,7 +37,6 @@ export type PubKey = SinglePubkey;
import * as logs from "./logs";
export { logs };
export { coin, coins, parseCoins } from "./coins";
export {
Account,
assertIsBroadcastTxSuccess,

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coin, coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { Bech32 } from "@cosmjs/encoding";
import { sleep } from "@cosmjs/utils";
import { coin, coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { MsgDelegate } from "../msgs";
import { SigningCosmosClient } from "../signingcosmosclient";

View File

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { sleep } from "@cosmjs/utils";
import { coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { SigningCosmosClient } from "../signingcosmosclient";
import {

View File

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coin, coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { coin, coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { MsgDelegate, MsgUndelegate } from "../msgs";
import { SigningCosmosClient } from "../signingcosmosclient";

View File

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Coin, makeCosmoshubPath, Secp256k1HdWallet } from "@cosmjs/amino";
import { Coin, coin, coins, makeCosmoshubPath, Secp256k1HdWallet } from "@cosmjs/amino";
import { assert } from "@cosmjs/utils";
import { coin, coins } from "./coins";
import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient";
import { GasPrice } from "./fee";
import { MsgDelegate, MsgSend } from "./msgs";

View File

@ -1,8 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdFee, StdSignature } from "@cosmjs/amino";
import { coins, StdFee, StdSignature } from "@cosmjs/amino";
import { makeSignDoc } from "@cosmjs/amino/build/signdoc";
import { coins } from "./coins";
import { makeStdTx } from "./tx";
describe("tx", () => {