From 8c1fd0fdca6cccf2fb19ac8f0894370457e0ebef Mon Sep 17 00:00:00 2001 From: willclarktech Date: Tue, 18 Aug 2020 17:01:54 +0100 Subject: [PATCH] cosmwasm: Refactor FeeTable for SigningCosmWasmClient --- packages/cosmwasm/src/index.ts | 2 +- .../cosmwasm/src/signingcosmwasmclient.ts | 56 ++++++++----------- packages/cosmwasm/types/index.d.ts | 2 +- .../cosmwasm/types/signingcosmwasmclient.d.ts | 26 +++++++-- 4 files changed, 47 insertions(+), 39 deletions(-) diff --git a/packages/cosmwasm/src/index.ts b/packages/cosmwasm/src/index.ts index 52a495d9..f7900667 100644 --- a/packages/cosmwasm/src/index.ts +++ b/packages/cosmwasm/src/index.ts @@ -21,7 +21,7 @@ export { } from "./cosmwasmclient"; export { ExecuteResult, - FeeTable, + CosmWasmFeeTable, InstantiateOptions, InstantiateResult, MigrateResult, diff --git a/packages/cosmwasm/src/signingcosmwasmclient.ts b/packages/cosmwasm/src/signingcosmwasmclient.ts index 797686cf..0ab7b8c8 100644 --- a/packages/cosmwasm/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm/src/signingcosmwasmclient.ts @@ -5,8 +5,11 @@ import { BroadcastMode, BroadcastTxFailure, BroadcastTxResult, + buildFeeTable, Coin, - coins, + FeeTable, + GasLimits, + GasPrice, isBroadcastTxFailure, makeSignBytes, Msg, @@ -31,9 +34,9 @@ import { } from "./msgs"; /** - * Those fees are used by the higher level methods of SigningCosmWasmClient + * These fees are used by the higher level methods of SigningCosmWasmClient */ -export interface FeeTable { +export interface CosmWasmFeeTable extends FeeTable { readonly upload: StdFee; readonly init: StdFee; readonly exec: StdFee; @@ -52,31 +55,14 @@ function prepareBuilder(buider: string | undefined): string { } } -const defaultFees: FeeTable = { - upload: { - amount: coins(25000, "ucosm"), - gas: "1000000", // one million - }, - init: { - amount: coins(12500, "ucosm"), - gas: "500000", // 500k - }, - migrate: { - amount: coins(5000, "ucosm"), - gas: "200000", // 200k - }, - exec: { - amount: coins(5000, "ucosm"), - gas: "200000", // 200k - }, - send: { - amount: coins(2000, "ucosm"), - gas: "80000", // 80k - }, - changeAdmin: { - amount: coins(2000, "ucosm"), - gas: "80000", // 80k - }, +const defaultGasPrice = GasPrice.fromString("0.025ucosm"); +const defaultGasLimits: GasLimits = { + upload: 1000000, + init: 500000, + migrate: 200000, + exec: 200000, + send: 80000, + changeAdmin: 80000, }; export interface UploadMeta { @@ -158,6 +144,11 @@ function createBroadcastTxErrorMessage(result: BroadcastTxFailure): string { return `Error when broadcasting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`; } +/** Use for testing only */ +export interface PrivateSigningCosmWasmClient { + readonly fees: CosmWasmFeeTable; +} + export class SigningCosmWasmClient extends CosmWasmClient { public readonly senderAddress: string; @@ -173,22 +164,23 @@ export class SigningCosmWasmClient extends CosmWasmClient { * @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API) * @param senderAddress The address that will sign and send transactions using this instance * @param signer An implementation of OfflineSigner which can provide signatures for transactions, potentially requiring user input. - * @param customFees The fees that are paid for transactions + * @param gasPrice The price paid per unit of gas + * @param gasLimits Custom overrides for gas limits related to specific transaction types * @param broadcastMode Defines at which point of the transaction processing the broadcastTx method returns */ public constructor( apiUrl: string, senderAddress: string, signer: OfflineSigner, - customFees?: Partial, + gasPrice: GasPrice = defaultGasPrice, + gasLimits: Partial> = {}, broadcastMode = BroadcastMode.Block, ) { super(apiUrl, broadcastMode); this.anyValidAddress = senderAddress; - this.senderAddress = senderAddress; this.signer = signer; - this.fees = { ...defaultFees, ...(customFees || {}) }; + this.fees = buildFeeTable(gasPrice, defaultGasLimits, gasLimits); } public async getSequence(address?: string): Promise { diff --git a/packages/cosmwasm/types/index.d.ts b/packages/cosmwasm/types/index.d.ts index 1b875e29..f74c9ba5 100644 --- a/packages/cosmwasm/types/index.d.ts +++ b/packages/cosmwasm/types/index.d.ts @@ -20,7 +20,7 @@ export { } from "./cosmwasmclient"; export { ExecuteResult, - FeeTable, + CosmWasmFeeTable, InstantiateOptions, InstantiateResult, MigrateResult, diff --git a/packages/cosmwasm/types/signingcosmwasmclient.d.ts b/packages/cosmwasm/types/signingcosmwasmclient.d.ts index 8f03f640..58f2f6f4 100644 --- a/packages/cosmwasm/types/signingcosmwasmclient.d.ts +++ b/packages/cosmwasm/types/signingcosmwasmclient.d.ts @@ -1,10 +1,20 @@ -import { BroadcastMode, BroadcastTxResult, Coin, Msg, OfflineSigner, StdFee } from "@cosmjs/launchpad"; +import { + BroadcastMode, + BroadcastTxResult, + Coin, + FeeTable, + GasLimits, + GasPrice, + Msg, + OfflineSigner, + StdFee, +} from "@cosmjs/launchpad"; import { Account, CosmWasmClient, GetSequenceResult } from "./cosmwasmclient"; import { Log } from "./logs"; /** - * Those fees are used by the higher level methods of SigningCosmWasmClient + * These fees are used by the higher level methods of SigningCosmWasmClient */ -export interface FeeTable { +export interface CosmWasmFeeTable extends FeeTable { readonly upload: StdFee; readonly init: StdFee; readonly exec: StdFee; @@ -81,6 +91,10 @@ export interface ExecuteResult { /** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */ readonly transactionHash: string; } +/** Use for testing only */ +export interface PrivateSigningCosmWasmClient { + readonly fees: CosmWasmFeeTable; +} export declare class SigningCosmWasmClient extends CosmWasmClient { readonly senderAddress: string; private readonly signer; @@ -94,14 +108,16 @@ export declare class SigningCosmWasmClient extends CosmWasmClient { * @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API) * @param senderAddress The address that will sign and send transactions using this instance * @param signer An implementation of OfflineSigner which can provide signatures for transactions, potentially requiring user input. - * @param customFees The fees that are paid for transactions + * @param gasPrice The price paid per unit of gas + * @param gasLimits Custom overrides for gas limits related to specific transaction types * @param broadcastMode Defines at which point of the transaction processing the broadcastTx method returns */ constructor( apiUrl: string, senderAddress: string, signer: OfflineSigner, - customFees?: Partial, + gasPrice?: GasPrice, + gasLimits?: Partial>, broadcastMode?: BroadcastMode, ); getSequence(address?: string): Promise;