diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts index ef941b9c..cf8b03b7 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.spec.ts @@ -2,7 +2,6 @@ import { Code } from "@cosmjs/cosmwasm-launchpad"; import { sha256 } from "@cosmjs/crypto"; import { Bech32, fromAscii, fromBase64, fromHex, toAscii, toBase64 } from "@cosmjs/encoding"; -import { StdFee } from "@cosmjs/launchpad"; import { Int53 } from "@cosmjs/math"; import { DirectSecp256k1HdWallet, @@ -11,7 +10,7 @@ import { makeSignDoc, Registry, } from "@cosmjs/proto-signing"; -import { assertIsBroadcastTxSuccess, coins, logs } from "@cosmjs/stargate"; +import { assertIsBroadcastTxSuccess, coins, logs, StdFee } from "@cosmjs/stargate"; import { TxRaw } from "@cosmjs/stargate/build/codec/cosmos/tx/v1beta1/tx"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; diff --git a/packages/cosmwasm-stargate/src/queries/wasm.spec.ts b/packages/cosmwasm-stargate/src/queries/wasm.spec.ts index fd647cd8..d6cb3869 100644 --- a/packages/cosmwasm-stargate/src/queries/wasm.spec.ts +++ b/packages/cosmwasm-stargate/src/queries/wasm.spec.ts @@ -1,7 +1,6 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { sha256 } from "@cosmjs/crypto"; import { fromAscii, fromHex, toAscii, toHex } from "@cosmjs/encoding"; -import { StdFee } from "@cosmjs/launchpad"; import { DirectSecp256k1HdWallet, OfflineDirectSigner, Registry } from "@cosmjs/proto-signing"; import { assertIsBroadcastTxSuccess, @@ -11,6 +10,7 @@ import { coins, logs, SigningStargateClient, + StdFee, } from "@cosmjs/stargate"; import { assert, assertDefined } from "@cosmjs/utils"; import Long from "long"; diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts index 3d8c818c..efc666d1 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts @@ -2,9 +2,9 @@ import { UploadMeta } from "@cosmjs/cosmwasm-launchpad"; import { sha256 } from "@cosmjs/crypto"; import { toHex } from "@cosmjs/encoding"; -import { GasPrice, MsgDelegate as LaunchpadMsgDelegate, Secp256k1HdWallet } from "@cosmjs/launchpad"; +import { MsgDelegate as LaunchpadMsgDelegate, Secp256k1HdWallet } from "@cosmjs/launchpad"; import { DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; -import { AminoTypes, assertIsBroadcastTxSuccess, coin, coins } from "@cosmjs/stargate"; +import { AminoTypes, assertIsBroadcastTxSuccess, coin, coins, GasPrice } from "@cosmjs/stargate"; import { DeepPartial, MsgSend } from "@cosmjs/stargate/build/codec/cosmos/bank/v1beta1/tx"; import { Coin } from "@cosmjs/stargate/build/codec/cosmos/base/v1beta1/coin"; import { MsgDelegate } from "@cosmjs/stargate/build/codec/cosmos/staking/v1beta1/tx"; diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 5890cbd8..78cb1dd4 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -13,14 +13,7 @@ import { } from "@cosmjs/cosmwasm-launchpad"; import { sha256 } from "@cosmjs/crypto"; import { fromBase64, toHex, toUtf8 } from "@cosmjs/encoding"; -import { - buildFeeTable, - CosmosFeeTable, - GasLimits, - GasPrice, - makeSignDoc as makeSignDocAmino, - StdFee, -} from "@cosmjs/launchpad"; +import { CosmosFeeTable, makeSignDoc as makeSignDocAmino } from "@cosmjs/launchpad"; import { Int53, Uint53 } from "@cosmjs/math"; import { EncodeObject, @@ -35,10 +28,14 @@ import { AminoTypes, BroadcastTxFailure, BroadcastTxResponse, + buildFeeTable, Coin, defaultRegistryTypes, + GasLimits, + GasPrice, isBroadcastTxFailure, logs, + StdFee, } from "@cosmjs/stargate"; import { SignMode } from "@cosmjs/stargate/build/codec/cosmos/tx/signing/v1beta1/signing"; import { TxRaw } from "@cosmjs/stargate/build/codec/cosmos/tx/v1beta1/tx"; diff --git a/packages/launchpad/src/cosmosclient.spec.ts b/packages/launchpad/src/cosmosclient.spec.ts index 4a17c340..768b27b6 100644 --- a/packages/launchpad/src/cosmosclient.spec.ts +++ b/packages/launchpad/src/cosmosclient.spec.ts @@ -4,6 +4,7 @@ import { ReadonlyDate } from "readonly-date"; import { assertIsBroadcastTxSuccess, CosmosClient, PrivateCosmosClient } from "./cosmosclient"; import { makeSignDoc } from "./encoding"; +import { StdFee } from "./fee"; import { findAttribute } from "./logs"; import { MsgSend } from "./msgs"; import { Secp256k1HdWallet } from "./secp256k1hdwallet"; @@ -17,7 +18,6 @@ import { unused, } from "./testutils.spec"; import { isWrappedStdTx, makeStdTx } from "./tx"; -import { StdFee } from "./types"; const blockTime = 1_000; // ms diff --git a/packages/launchpad/src/encoding.ts b/packages/launchpad/src/encoding.ts index 6ce92d42..5d7b457d 100644 --- a/packages/launchpad/src/encoding.ts +++ b/packages/launchpad/src/encoding.ts @@ -2,8 +2,8 @@ import { toUtf8 } from "@cosmjs/encoding"; import { Uint53 } from "@cosmjs/math"; +import { StdFee } from "./fee"; import { Msg } from "./msgs"; -import { StdFee } from "./types"; function sortedObject(obj: any): any { if (typeof obj !== "object" || obj === null) { diff --git a/packages/launchpad/src/gas.spec.ts b/packages/launchpad/src/fee.spec.ts similarity index 95% rename from packages/launchpad/src/gas.spec.ts rename to packages/launchpad/src/fee.spec.ts index a92e4e08..7a94238a 100644 --- a/packages/launchpad/src/gas.spec.ts +++ b/packages/launchpad/src/fee.spec.ts @@ -1,6 +1,6 @@ import { Decimal } from "@cosmjs/math"; -import { GasPrice } from "./gas"; +import { GasPrice } from "./fee"; describe("GasPrice", () => { it("can be constructed", () => { diff --git a/packages/launchpad/src/gas.ts b/packages/launchpad/src/fee.ts similarity index 92% rename from packages/launchpad/src/gas.ts rename to packages/launchpad/src/fee.ts index c6cfe666..027d7554 100644 --- a/packages/launchpad/src/gas.ts +++ b/packages/launchpad/src/fee.ts @@ -1,7 +1,11 @@ import { Decimal, Uint53 } from "@cosmjs/math"; -import { coins } from "./coins"; -import { StdFee } from "./types"; +import { Coin, coins } from "./coins"; + +export interface StdFee { + readonly amount: readonly Coin[]; + readonly gas: string; +} export type FeeTable = Record; diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index d97792e4..04d91fb8 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -43,7 +43,7 @@ export { isSearchByTagsQuery, } from "./cosmosclient"; export { makeSignDoc, serializeSignDoc, StdSignDoc } from "./encoding"; -export { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./gas"; +export { buildFeeTable, FeeTable, GasLimits, GasPrice, StdFee } from "./fee"; export { AuthAccountsResponse, AuthExtension, @@ -145,7 +145,6 @@ export { findSequenceForSignedTx } from "./sequence"; export { AccountData, Algo, AminoSignResponse, OfflineSigner } from "./signer"; export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient"; export { isStdTx, isWrappedStdTx, makeStdTx, CosmosSdkTx, StdTx, WrappedStdTx, WrappedTx } from "./tx"; -export { StdFee } from "./types"; export { executeKdf, KdfConfiguration } from "./wallet"; export { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet"; export { Secp256k1Wallet } from "./secp256k1wallet"; diff --git a/packages/launchpad/src/lcdapi/lcdclient.spec.ts b/packages/launchpad/src/lcdapi/lcdclient.spec.ts index 1813b349..ff7495c7 100644 --- a/packages/launchpad/src/lcdapi/lcdclient.spec.ts +++ b/packages/launchpad/src/lcdapi/lcdclient.spec.ts @@ -4,6 +4,7 @@ import { assert, sleep } from "@cosmjs/utils"; import { Coin } from "../coins"; import { isBroadcastTxFailure } from "../cosmosclient"; import { makeSignDoc } from "../encoding"; +import { StdFee } from "../fee"; import { parseLogs } from "../logs"; import { MsgSend } from "../msgs"; import { makeCosmoshubPath } from "../paths"; @@ -21,7 +22,6 @@ import { unused, } from "../testutils.spec"; import { isWrappedStdTx, makeStdTx, StdTx } from "../tx"; -import { StdFee } from "../types"; import { setupAuthExtension } from "./auth"; import { TxsResponse } from "./base"; import { LcdApiArray, LcdClient } from "./lcdclient"; diff --git a/packages/launchpad/src/signingcosmosclient.spec.ts b/packages/launchpad/src/signingcosmosclient.spec.ts index d898a447..307243a9 100644 --- a/packages/launchpad/src/signingcosmosclient.spec.ts +++ b/packages/launchpad/src/signingcosmosclient.spec.ts @@ -3,7 +3,7 @@ import { assert } from "@cosmjs/utils"; import { Coin, coin, coins } from "./coins"; import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient"; -import { GasPrice } from "./gas"; +import { GasPrice } from "./fee"; import { MsgDelegate, MsgSend } from "./msgs"; import { makeCosmoshubPath } from "./paths"; import { Secp256k1HdWallet } from "./secp256k1hdwallet"; diff --git a/packages/launchpad/src/signingcosmosclient.ts b/packages/launchpad/src/signingcosmosclient.ts index 829aaa80..057efb4e 100644 --- a/packages/launchpad/src/signingcosmosclient.ts +++ b/packages/launchpad/src/signingcosmosclient.ts @@ -4,12 +4,11 @@ import equals from "fast-deep-equal"; import { Coin } from "./coins"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; import { makeSignDoc } from "./encoding"; -import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./gas"; +import { buildFeeTable, FeeTable, GasLimits, GasPrice, StdFee } from "./fee"; import { BroadcastMode } from "./lcdapi"; import { Msg, MsgSend } from "./msgs"; import { OfflineSigner } from "./signer"; import { makeStdTx, StdTx } from "./tx"; -import { StdFee } from "./types"; /** * These fees are used by the higher level methods of SigningCosmosClient diff --git a/packages/launchpad/src/tx.spec.ts b/packages/launchpad/src/tx.spec.ts index 848c0c56..894a8069 100644 --- a/packages/launchpad/src/tx.spec.ts +++ b/packages/launchpad/src/tx.spec.ts @@ -3,8 +3,8 @@ import { StdSignature } from "@cosmjs/amino"; import { coins } from "./coins"; import { makeSignDoc } from "./encoding"; +import { StdFee } from "./fee"; import { makeStdTx } from "./tx"; -import { StdFee } from "./types"; describe("tx", () => { describe("makeStdTx", () => { diff --git a/packages/launchpad/src/tx.ts b/packages/launchpad/src/tx.ts index e88ab00f..0ef1082d 100644 --- a/packages/launchpad/src/tx.ts +++ b/packages/launchpad/src/tx.ts @@ -1,8 +1,8 @@ import { StdSignature } from "@cosmjs/amino"; import { StdSignDoc } from "./encoding"; +import { StdFee } from "./fee"; import { Msg } from "./msgs"; -import { StdFee } from "./types"; /** * A Cosmos SDK StdTx diff --git a/packages/launchpad/src/types.ts b/packages/launchpad/src/types.ts deleted file mode 100644 index 91fc04c0..00000000 --- a/packages/launchpad/src/types.ts +++ /dev/null @@ -1,7 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { Coin } from "./coins"; - -export interface StdFee { - readonly amount: readonly Coin[]; - readonly gas: string; -} diff --git a/packages/stargate/src/fee.spec.ts b/packages/stargate/src/fee.spec.ts new file mode 100644 index 00000000..7a94238a --- /dev/null +++ b/packages/stargate/src/fee.spec.ts @@ -0,0 +1,23 @@ +import { Decimal } from "@cosmjs/math"; + +import { GasPrice } from "./fee"; + +describe("GasPrice", () => { + it("can be constructed", () => { + const inputs = ["3.14", "3", "0.14"]; + inputs.forEach((input) => { + const gasPrice = new GasPrice(Decimal.fromUserInput(input, 18), "utest"); + expect(gasPrice.amount.toString()).toEqual(input); + expect(gasPrice.denom).toEqual("utest"); + }); + }); + + it("can be constructed from a config string", () => { + const inputs = ["3.14", "3", "0.14"]; + inputs.forEach((input) => { + const gasPrice = GasPrice.fromString(`${input}utest`); + expect(gasPrice.amount.toString()).toEqual(input); + expect(gasPrice.denom).toEqual("utest"); + }); + }); +}); diff --git a/packages/stargate/src/fee.ts b/packages/stargate/src/fee.ts new file mode 100644 index 00000000..f82f0ddd --- /dev/null +++ b/packages/stargate/src/fee.ts @@ -0,0 +1,77 @@ +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; +} + +/** + * This is the same as FeeTable from @cosmjs/launchpad but those might diverge in the future. + */ +export type FeeTable = Record; + +/** + * This is the same as GasPrice from @cosmjs/launchpad but those might diverge in the future. + */ +export class GasPrice { + public readonly amount: Decimal; + public readonly denom: string; + + public constructor(amount: Decimal, denom: string) { + this.amount = amount; + this.denom = denom; + } + + public static fromString(gasPrice: string): GasPrice { + const matchResult = gasPrice.match(/^(?.+?)(?[a-z]+)$/); + if (!matchResult) { + throw new Error("Invalid gas price string"); + } + const { amount, denom } = matchResult.groups as { readonly amount: string; readonly denom: string }; + if (denom.length < 3 || denom.length > 127) { + throw new Error("Gas price denomination must be between 3 and 127 characters"); + } + const fractionalDigits = 18; + const decimalAmount = Decimal.fromUserInput(amount, fractionalDigits); + return new GasPrice(decimalAmount, denom); + } +} + +/** + * This is the same as GasLimits from @cosmjs/launchpad but those might diverge in the future. + */ +export type GasLimits> = { + readonly [key in keyof T]: number; +}; + +/** + * This is the same as calculateFee from @cosmjs/launchpad but those might diverge in the future. + */ +function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee { + const amount = Math.ceil(gasPriceAmount.multiply(new Uint53(gasLimit)).toFloatApproximation()); + return { + amount: coins(amount, denom), + gas: gasLimit.toString(), + }; +} + +/** + * This is the same as buildFeeTable from @cosmjs/launchpad but those might diverge in the future. + */ +export function buildFeeTable>( + gasPrice: GasPrice, + defaultGasLimits: GasLimits, + gasLimits: Partial>, +): T { + return Object.entries(defaultGasLimits).reduce( + (feeTable, [type, defaultGasLimit]) => ({ + ...feeTable, + [type]: calculateFee(gasLimits[type] || defaultGasLimit, gasPrice), + }), + {} as T, + ); +} diff --git a/packages/stargate/src/index.ts b/packages/stargate/src/index.ts index c05e786f..384f3d82 100644 --- a/packages/stargate/src/index.ts +++ b/packages/stargate/src/index.ts @@ -2,6 +2,7 @@ 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 * as logs from "./logs"; export { AuthExtension, diff --git a/packages/stargate/src/signingstargateclient.spec.ts b/packages/stargate/src/signingstargateclient.spec.ts index bed17b38..869c4f12 100644 --- a/packages/stargate/src/signingstargateclient.spec.ts +++ b/packages/stargate/src/signingstargateclient.spec.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/naming-convention,no-bitwise */ -import { GasPrice, MsgDelegate as LaunchpadMsgDelegate, Secp256k1HdWallet } from "@cosmjs/launchpad"; +import { MsgDelegate as LaunchpadMsgDelegate, Secp256k1HdWallet } from "@cosmjs/launchpad"; import { coin, coins, DirectSecp256k1HdWallet, Registry } from "@cosmjs/proto-signing"; import { assert, sleep } from "@cosmjs/utils"; import protobuf from "protobufjs/minimal"; @@ -9,6 +9,7 @@ import { MsgSend } from "./codec/cosmos/bank/v1beta1/tx"; import { Coin } from "./codec/cosmos/base/v1beta1/coin"; import { DeepPartial, MsgDelegate } from "./codec/cosmos/staking/v1beta1/tx"; import { Tx } from "./codec/cosmos/tx/v1beta1/tx"; +import { GasPrice } from "./fee"; import { PrivateSigningStargateClient, SigningStargateClient } from "./signingstargateclient"; import { assertIsBroadcastTxSuccess } from "./stargateclient"; import { diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index 5df8ba42..49b30cba 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -1,13 +1,6 @@ import { encodeSecp256k1Pubkey } from "@cosmjs/amino"; import { fromBase64 } from "@cosmjs/encoding"; -import { - buildFeeTable, - CosmosFeeTable, - GasLimits, - GasPrice, - makeSignDoc as makeSignDocAmino, - StdFee, -} from "@cosmjs/launchpad"; +import { CosmosFeeTable, makeSignDoc as makeSignDocAmino } from "@cosmjs/launchpad"; import { Int53 } from "@cosmjs/math"; import { EncodeObject, @@ -63,6 +56,7 @@ import { MsgConnectionOpenInit, MsgConnectionOpenTry, } from "./codec/ibc/core/connection/v1/tx"; +import { buildFeeTable, GasLimits, GasPrice, StdFee } from "./fee"; import { BroadcastTxResponse, StargateClient } from "./stargateclient"; const defaultGasPrice = GasPrice.fromString("0.025ucosm");