diff --git a/packages/launchpad/src/gas.ts b/packages/launchpad/src/gas.ts index 5f7251f3..7f0fabe4 100644 --- a/packages/launchpad/src/gas.ts +++ b/packages/launchpad/src/gas.ts @@ -1,13 +1,6 @@ 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; @@ -18,25 +11,28 @@ export class GasPrice { } } -export type GasLimits = { - readonly [key in keyof FeeTable]: number; +export type GasLimits> = { + readonly [key in keyof T]: number; }; -function calculateFee(gasLimit: number, denom: string, price: number): StdFee { - const amount = Math.ceil(price * gasLimit); +function calculateFee(gasLimit: number, { denom, amount: gasPriceAmount }: GasPrice): StdFee { + const amount = Math.ceil(gasPriceAmount * 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); +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/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 8d870a18..c1b63346 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -29,7 +29,7 @@ export { isSearchByTagsQuery, } from "./cosmosclient"; export { makeSignBytes } from "./encoding"; -export { buildFeeTable, FeeTable, GasPrice } from "./gas"; +export { buildFeeTable, GasPrice } from "./gas"; export { AuthAccountsResponse, AuthExtension, @@ -98,7 +98,7 @@ export { } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; export { encodeSecp256k1Signature, decodeSignature } from "./signature"; -export { SigningCosmosClient } from "./signingcosmosclient"; +export { FeeTable, SigningCosmosClient } from "./signingcosmosclient"; export { isStdTx, pubkeyType, CosmosSdkTx, PubKey, StdFee, StdSignature, StdTx } from "./types"; export { AccountData, diff --git a/packages/launchpad/src/signingcosmosclient.ts b/packages/launchpad/src/signingcosmosclient.ts index 1889fb15..cfd75437 100644 --- a/packages/launchpad/src/signingcosmosclient.ts +++ b/packages/launchpad/src/signingcosmosclient.ts @@ -2,14 +2,21 @@ import { Coin } from "./coins"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; import { makeSignBytes } from "./encoding"; -import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./gas"; +import { buildFeeTable, 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 extends Record { + readonly send: StdFee; +} + const defaultGasPrice = new GasPrice(0.025, "ucosm"); -const defaultGasLimits: GasLimits = { send: 80000 }; +const defaultGasLimits: GasLimits = { send: 80000 }; /** Use for testing only */ export interface PrivateSigningCosmosClient { @@ -40,20 +47,14 @@ export class SigningCosmosClient extends CosmosClient { senderAddress: string, signer: OfflineSigner, gasPrice: GasPrice = defaultGasPrice, - gasLimits?: Partial, + gasLimits: Partial> = {}, broadcastMode = BroadcastMode.Block, ) { super(apiUrl, broadcastMode); this.anyValidAddress = senderAddress; - this.senderAddress = senderAddress; this.signer = signer; - - const mergedGasLimits = { - ...defaultGasLimits, - ...gasLimits, - }; - this.fees = buildFeeTable(gasPrice, mergedGasLimits); + this.fees = buildFeeTable(gasPrice, defaultGasLimits, gasLimits); } public async getSequence(address?: string): Promise { diff --git a/packages/launchpad/types/gas.d.ts b/packages/launchpad/types/gas.d.ts index 4aeee124..b4070f3e 100644 --- a/packages/launchpad/types/gas.d.ts +++ b/packages/launchpad/types/gas.d.ts @@ -1,16 +1,14 @@ 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 type GasLimits> = { + readonly [key in keyof T]: number; }; -export declare function buildFeeTable({ denom, amount }: GasPrice, gasLimits: GasLimits): FeeTable; +export declare function buildFeeTable>( + gasPrice: GasPrice, + defaultGasLimits: GasLimits, + gasLimits: Partial>, +): T; diff --git a/packages/launchpad/types/index.d.ts b/packages/launchpad/types/index.d.ts index 4e93998a..3f08cbbc 100644 --- a/packages/launchpad/types/index.d.ts +++ b/packages/launchpad/types/index.d.ts @@ -27,7 +27,7 @@ export { isSearchByTagsQuery, } from "./cosmosclient"; export { makeSignBytes } from "./encoding"; -export { buildFeeTable, FeeTable, GasPrice } from "./gas"; +export { buildFeeTable, GasPrice } from "./gas"; export { AuthAccountsResponse, AuthExtension, @@ -96,7 +96,7 @@ export { } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; export { encodeSecp256k1Signature, decodeSignature } from "./signature"; -export { SigningCosmosClient } from "./signingcosmosclient"; +export { FeeTable, 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 ebfffc92..c6a82eea 100644 --- a/packages/launchpad/types/signingcosmosclient.d.ts +++ b/packages/launchpad/types/signingcosmosclient.d.ts @@ -1,10 +1,16 @@ import { Coin } from "./coins"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; -import { FeeTable, GasLimits, GasPrice } from "./gas"; +import { 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 extends Record { + readonly send: StdFee; +} /** Use for testing only */ export interface PrivateSigningCosmosClient { readonly fees: FeeTable; @@ -31,7 +37,7 @@ export declare class SigningCosmosClient extends CosmosClient { senderAddress: string, signer: OfflineSigner, gasPrice?: GasPrice, - gasLimits?: Partial, + gasLimits?: Partial>, broadcastMode?: BroadcastMode, ); getSequence(address?: string): Promise;