diff --git a/packages/launchpad/src/gas.ts b/packages/launchpad/src/gas.ts new file mode 100644 index 00000000..5f7251f3 --- /dev/null +++ b/packages/launchpad/src/gas.ts @@ -0,0 +1,42 @@ +import { coins } from "./coins"; +import { StdFee } from "./types"; + +/** + * These fees are used by the higher level methods of SigningCosmosClient + */ +export interface FeeTable { + readonly send: StdFee; +} + +export class GasPrice { + public readonly amount: number; + public readonly denom: string; + + constructor(amount: number, denom: string) { + this.amount = amount; + this.denom = denom; + } +} + +export type GasLimits = { + readonly [key in keyof FeeTable]: number; +}; + +function calculateFee(gasLimit: number, denom: string, price: number): StdFee { + const amount = Math.ceil(price * gasLimit); + return { + amount: coins(amount, denom), + gas: gasLimit.toString(), + }; +} + +export function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable { + return Object.entries(gasLimits).reduce((feeTable, [type, gasLimit]) => { + return gasLimit === undefined + ? feeTable + : { + ...feeTable, + [type]: calculateFee(gasLimit, denom, amount), + }; + }, {} as FeeTable); +} diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 671863ea..8d870a18 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -29,6 +29,7 @@ export { isSearchByTagsQuery, } from "./cosmosclient"; export { makeSignBytes } from "./encoding"; +export { buildFeeTable, FeeTable, GasPrice } from "./gas"; export { AuthAccountsResponse, AuthExtension, @@ -97,7 +98,7 @@ export { } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; export { encodeSecp256k1Signature, decodeSignature } from "./signature"; -export { FeeTable, SigningCosmosClient } from "./signingcosmosclient"; +export { SigningCosmosClient } from "./signingcosmosclient"; export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types"; export { AccountData, diff --git a/packages/launchpad/src/signingcosmosclient.spec.ts b/packages/launchpad/src/signingcosmosclient.spec.ts index 2d93abba..22537d77 100644 --- a/packages/launchpad/src/signingcosmosclient.spec.ts +++ b/packages/launchpad/src/signingcosmosclient.spec.ts @@ -3,9 +3,10 @@ import { assert } from "@cosmjs/utils"; import { Coin, coin, coins } from "./coins"; import { assertIsBroadcastTxSuccess, PrivateCosmosClient } from "./cosmosclient"; +import { GasPrice } from "./gas"; import { MsgDelegate } from "./msgs"; import { Secp256k1Wallet } from "./secp256k1wallet"; -import { GasPrice, SigningCosmosClient } from "./signingcosmosclient"; +import { SigningCosmosClient } from "./signingcosmosclient"; import { makeRandomAddress, pendingWithoutWasmd, validatorAddress } from "./testutils.spec"; const httpUrl = "http://localhost:1317"; diff --git a/packages/launchpad/src/signingcosmosclient.ts b/packages/launchpad/src/signingcosmosclient.ts index 5160160b..cbb8dcdb 100644 --- a/packages/launchpad/src/signingcosmosclient.ts +++ b/packages/launchpad/src/signingcosmosclient.ts @@ -1,54 +1,14 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Coin, coins } from "./coins"; +import { Coin } from "./coins"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; import { makeSignBytes } from "./encoding"; +import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./gas"; import { BroadcastMode } from "./lcdapi"; import { Msg, MsgSend } from "./msgs"; import { StdFee, StdTx } from "./types"; import { OfflineSigner } from "./wallet"; -/** - * These fees are used by the higher level methods of SigningCosmosClient - */ -export interface FeeTable { - readonly send: StdFee; -} - -export class GasPrice { - readonly amount: number; - readonly denom: string; - - constructor(amount: number, denom: string) { - this.amount = amount; - this.denom = denom; - } -} - -export type GasLimits = { - readonly [key in keyof FeeTable]: number; -}; - -function calculateFee(gasLimit: number, denom: string, price: number): StdFee { - const amount = Math.ceil(price * gasLimit); - return { - amount: coins(amount, denom), - gas: gasLimit.toString(), - }; -} - -function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable { - return Object.entries(gasLimits).reduce((feeTable, [type, gasLimit]) => { - if (gasLimit === undefined) { - return feeTable; - } - return { - ...feeTable, - [type]: calculateFee(gasLimit, denom, amount), - }; - }, {} as FeeTable); -} - -const defaultGasPrice: GasPrice = new GasPrice(0.025, "ucosm"); +const defaultGasPrice = new GasPrice(0.025, "ucosm"); const defaultGasLimits: GasLimits = { send: 80000 }; export class SigningCosmosClient extends CosmosClient { diff --git a/packages/launchpad/types/gas.d.ts b/packages/launchpad/types/gas.d.ts new file mode 100644 index 00000000..4aeee124 --- /dev/null +++ b/packages/launchpad/types/gas.d.ts @@ -0,0 +1,16 @@ +import { StdFee } from "./types"; +/** + * These fees are used by the higher level methods of SigningCosmosClient + */ +export interface FeeTable { + readonly send: StdFee; +} +export declare class GasPrice { + readonly amount: number; + readonly denom: string; + constructor(amount: number, denom: string); +} +export declare type GasLimits = { + readonly [key in keyof FeeTable]: number; +}; +export declare function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable; diff --git a/packages/launchpad/types/index.d.ts b/packages/launchpad/types/index.d.ts index 8c7d2c0c..4e93998a 100644 --- a/packages/launchpad/types/index.d.ts +++ b/packages/launchpad/types/index.d.ts @@ -27,6 +27,7 @@ export { isSearchByTagsQuery, } from "./cosmosclient"; export { makeSignBytes } from "./encoding"; +export { buildFeeTable, FeeTable, GasPrice } from "./gas"; export { AuthAccountsResponse, AuthExtension, @@ -95,7 +96,7 @@ export { } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; export { encodeSecp256k1Signature, decodeSignature } from "./signature"; -export { FeeTable, SigningCosmosClient } from "./signingcosmosclient"; +export { SigningCosmosClient } from "./signingcosmosclient"; export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types"; export { AccountData, diff --git a/packages/launchpad/types/signingcosmosclient.d.ts b/packages/launchpad/types/signingcosmosclient.d.ts index d8974309..ebfffc92 100644 --- a/packages/launchpad/types/signingcosmosclient.d.ts +++ b/packages/launchpad/types/signingcosmosclient.d.ts @@ -1,23 +1,14 @@ import { Coin } from "./coins"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; +import { FeeTable, GasLimits, GasPrice } from "./gas"; import { BroadcastMode } from "./lcdapi"; import { Msg } from "./msgs"; import { StdFee } from "./types"; import { OfflineSigner } from "./wallet"; -/** - * These fees are used by the higher level methods of SigningCosmosClient - */ -export interface FeeTable { - readonly send: StdFee; +/** Use for testing only */ +export interface PrivateSigningCosmosClient { + readonly fees: FeeTable; } -export declare class GasPrice { - readonly amount: number; - readonly denom: string; - constructor(amount: number, denom: string); -} -export declare type GasLimits = { - readonly [key in keyof FeeTable]: number; -}; export declare class SigningCosmosClient extends CosmosClient { readonly senderAddress: string; private readonly signer;