Merge pull request #729 from cosmos/657-independent-stargate-6

Reorganise coins/CosmosFeeTable/search types and helpers
This commit is contained in:
Simon Warta 2021-03-24 23:04:37 +01:00 committed by GitHub
commit 133898c2f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 133 additions and 214 deletions

View File

@ -1,18 +1,26 @@
import { Coin } from "@cosmjs/amino";
import { Uint53, Uint64 } from "@cosmjs/math";
/** Creates a coin */
export interface Coin {
readonly denom: string;
readonly amount: string;
}
/**
* 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 */
/**
* 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
* Takes a coins list like "819966000ucosm,700000000ustake" and parses it.
*/
export function parseCoins(input: string): Coin[] {
return input

View File

@ -1,4 +1,5 @@
export { pubkeyToAddress, pubkeyToRawAddress, rawSecp256k1PubkeyToRawAddress } from "./addresses";
export { Coin, coin, coins, parseCoins } from "./coins";
export {
decodeAminoPubkey,
decodeBech32Pubkey,
@ -23,6 +24,6 @@ export { makeCosmoshubPath } from "./paths";
export { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
export { Secp256k1Wallet } from "./secp256k1wallet";
export { decodeSignature, encodeSecp256k1Signature, StdSignature } from "./signature";
export { AminoMsg, Coin, makeSignDoc, serializeSignDoc, StdFee, StdSignDoc } from "./signdoc";
export { AminoMsg, makeSignDoc, serializeSignDoc, StdFee, StdSignDoc } from "./signdoc";
export { AccountData, Algo, AminoSignResponse, OfflineAminoSigner } from "./signer";
export { executeKdf, KdfConfiguration } from "./wallet";

View File

@ -2,16 +2,13 @@
import { toUtf8 } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
import { Coin } from "./coins";
export interface AminoMsg {
readonly type: string;
readonly value: any;
}
export interface Coin {
readonly denom: string;
readonly amount: string;
}
export interface StdFee {
readonly amount: readonly Coin[];
readonly gas: string;

View File

@ -7,25 +7,23 @@ import {
JsonObject,
} from "@cosmjs/cosmwasm-launchpad";
import { fromAscii, toHex } from "@cosmjs/encoding";
import {
Block,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
SearchTxFilter,
SearchTxQuery,
} from "@cosmjs/launchpad";
import { Uint53 } from "@cosmjs/math";
import {
Account,
accountFromAny,
AuthExtension,
BankExtension,
Block,
BroadcastTxResponse,
Coin,
coinFromProto,
IndexedTx,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
QueryClient,
SearchTxFilter,
SearchTxQuery,
SequenceResponse,
setupAuthExtension,
setupBankExtension,

View File

@ -13,7 +13,6 @@ import {
} from "@cosmjs/cosmwasm-launchpad";
import { sha256 } from "@cosmjs/crypto";
import { fromBase64, toHex, toUtf8 } from "@cosmjs/encoding";
import { CosmosFeeTable } from "@cosmjs/launchpad";
import { Int53, Uint53 } from "@cosmjs/math";
import {
EncodeObject,
@ -30,6 +29,7 @@ import {
BroadcastTxResponse,
buildFeeTable,
Coin,
CosmosFeeTable,
defaultRegistryTypes,
GasLimits,
GasPrice,

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", () => {

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,47 +0,0 @@
import { Uint53, Uint64 } from "@cosmjs/math";
/**
* This is the same as Coin from @cosmjs/launchpad but those might diverge in the future.
*/
export interface Coin {
readonly denom: string;
readonly amount: string;
}
/**
* Creates a coin.
*
* This is the same as coin from @cosmjs/launchpad but those might diverge in the future.
*/
export function coin(amount: number, denom: string): Coin {
return { amount: new Uint53(amount).toString(), denom: denom };
}
/**
* Creates a list of coins with one element
*
* This is the same as coins from @cosmjs/launchpad but those might diverge in the future.
*/
export function coins(amount: number, denom: string): Coin[] {
return [coin(amount, denom)];
}
/**
* Takes a coins list like "819966000ucosm,700000000ustake" and parses it
*
* This is the same as parseCoins from @cosmjs/launchpad but those might diverge in the future.
*/
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,7 +1,7 @@
import { coins } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { coins } from "./coins";
import { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet";
import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing";
import { faucet, testVectors } from "./testutils.spec";

View File

@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coins } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { coins } from "./coins";
import { DirectSecp256k1Wallet } from "./directsecp256k1wallet";
import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing";
import { testVectors } from "./testutils.spec";

View File

@ -1,4 +1,6 @@
export { Coin, coin, coins, parseCoins } from "./coins";
// This type happens to be shared between Amino and Direct sign modes
export { Coin, coin, coins, parseCoins } from "@cosmjs/amino";
export {
isPbjsGeneratedType,
isTsProtoGeneratedType,

View File

@ -1,13 +1,6 @@
import { StdFee } from "@cosmjs/amino";
import { Decimal, Uint53 } from "@cosmjs/math";
import { Coin, coins } from "@cosmjs/proto-signing";
/**
* This is the same as StdFee from @cosmjs/launchpad but those might diverge in the future.
*/
export interface StdFee {
readonly amount: readonly Coin[];
readonly gas: string;
}
import { coins } from "@cosmjs/proto-signing";
/**
* This is the same as FeeTable from @cosmjs/launchpad but those might diverge in the future.

View File

@ -1,8 +1,9 @@
export { StdFee } from "@cosmjs/amino";
export { Coin, coin, coins, parseCoins } from "@cosmjs/proto-signing";
export { Account, accountFromAny } from "./accounts";
export { AminoConverter, AminoTypes } from "./aminotypes";
export { buildFeeTable, FeeTable, GasLimits, GasPrice, StdFee } from "./fee";
export { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
export * as logs from "./logs";
export { makeMultisignedTx } from "./multisignature";
export {
@ -21,8 +22,20 @@ export {
setupStakingExtension,
StakingExtension,
} from "./queries";
export {
SearchByHeightQuery,
SearchBySentFromOrToQuery,
SearchByTagsQuery,
SearchTxQuery,
SearchTxFilter,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
} from "./search";
export {
assertIsBroadcastTxSuccess,
Block,
BlockHeader,
BroadcastTxFailure,
BroadcastTxResponse,
BroadcastTxSuccess,
@ -34,6 +47,7 @@ export {
StargateClient,
} from "./stargateclient";
export {
CosmosFeeTable,
defaultRegistryTypes,
SignerData,
SigningStargateClient,

View File

@ -1,3 +1,4 @@
import { StdFee } from "@cosmjs/amino";
import { Bech32 } from "@cosmjs/encoding";
import { encodePubkey } from "@cosmjs/proto-signing";
import Long from "long";
@ -7,7 +8,6 @@ import { CompactBitArray, MultiSignature } from "./codec/cosmos/crypto/multisig/
import { SignMode } from "./codec/cosmos/tx/signing/v1beta1/signing";
import { AuthInfo, SignerInfo } from "./codec/cosmos/tx/v1beta1/tx";
import { TxRaw } from "./codec/cosmos/tx/v1beta1/tx";
import { StdFee } from "./fee";
export function makeCompactBitArray(bits: readonly boolean[]): CompactBitArray {
const byteCount = Math.ceil(bits.length / 8);

View File

@ -0,0 +1,34 @@
export interface SearchByHeightQuery {
readonly height: number;
}
export interface SearchBySentFromOrToQuery {
readonly sentFromOrTo: string;
}
/**
* This query type allows you to pass arbitrary key/value pairs to the backend. It is
* more powerful and slightly lower level than the other search options.
*/
export interface SearchByTagsQuery {
readonly tags: ReadonlyArray<{ readonly key: string; readonly value: string }>;
}
export type SearchTxQuery = SearchByHeightQuery | SearchBySentFromOrToQuery | SearchByTagsQuery;
export function isSearchByHeightQuery(query: SearchTxQuery): query is SearchByHeightQuery {
return (query as SearchByHeightQuery).height !== undefined;
}
export function isSearchBySentFromOrToQuery(query: SearchTxQuery): query is SearchBySentFromOrToQuery {
return (query as SearchBySentFromOrToQuery).sentFromOrTo !== undefined;
}
export function isSearchByTagsQuery(query: SearchTxQuery): query is SearchByTagsQuery {
return (query as SearchByTagsQuery).tags !== undefined;
}
export interface SearchTxFilter {
readonly minHeight?: number;
readonly maxHeight?: number;
}

View File

@ -1,6 +1,5 @@
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino, StdFee } from "@cosmjs/amino";
import { fromBase64 } from "@cosmjs/encoding";
import { CosmosFeeTable } from "@cosmjs/launchpad";
import { Int53 } from "@cosmjs/math";
import {
EncodeObject,
@ -57,9 +56,18 @@ import {
MsgConnectionOpenInit,
MsgConnectionOpenTry,
} from "./codec/ibc/core/connection/v1/tx";
import { buildFeeTable, GasLimits, GasPrice, StdFee } from "./fee";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
import { BroadcastTxResponse, StargateClient } from "./stargateclient";
/**
* These fees are used by the higher level methods of SigningCosmosClient
*
* This is the same as CosmosFeeTable from @cosmjs/launchpad but those might diverge in the future.
*/
export interface CosmosFeeTable extends FeeTable {
readonly send: StdFee;
}
const defaultGasPrice = GasPrice.fromString("0.025ucosm");
const defaultGasLimits: GasLimits<CosmosFeeTable> = { send: 80000 };

View File

@ -1,13 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { toHex } from "@cosmjs/encoding";
import {
Block,
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
SearchTxFilter,
SearchTxQuery,
} from "@cosmjs/launchpad";
import { Uint53 } from "@cosmjs/math";
import {
broadcastTxCommitSuccess,
@ -20,6 +12,38 @@ import { Account, accountFromAny } from "./accounts";
import { MsgData, TxMsgData } from "./codec/cosmos/base/abci/v1beta1/abci";
import { Coin } from "./codec/cosmos/base/v1beta1/coin";
import { AuthExtension, BankExtension, QueryClient, setupAuthExtension, setupBankExtension } from "./queries";
import {
isSearchByHeightQuery,
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
SearchTxFilter,
SearchTxQuery,
} from "./search";
/**
* This is the same as BlockHeader from @cosmjs/launchpad but those might diverge in the future.
*/
export interface BlockHeader {
readonly version: {
readonly block: string;
readonly app: string;
};
readonly height: number;
readonly chainId: string;
/** An RFC 3339 time string like e.g. '2020-02-15T10:39:10.4696305Z' */
readonly time: string;
}
/**
* This is the same as Block from @cosmjs/launchpad but those might diverge in the future.
*/
export interface Block {
/** The ID is a hash of the block header (uppercase hex) */
readonly id: string;
readonly header: BlockHeader;
/** Array of raw transactions */
readonly txs: readonly Uint8Array[];
}
/** A transaction that is indexed as part of the transaction history */
export interface IndexedTx {