diff --git a/packages/cli/examples/simulate.ts b/packages/cli/examples/simulate.ts index 3bec3df2..c01c6119 100644 --- a/packages/cli/examples/simulate.ts +++ b/packages/cli/examples/simulate.ts @@ -34,6 +34,16 @@ const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet console.log("Successfully broadcasted:", result); } +// Send transaction (using sendTokens with auto gas and custom muliplier) +{ + const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; + const amount = coins(1234567, "ucosm"); + const memo = "With simulate"; + const result = await client.sendTokens(account.address, recipient, amount, 1.2, memo); + assertIsBroadcastTxSuccess(result); + console.log("Successfully broadcasted:", result); +} + // Send transaction (using sendTokens with manual gas) { const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; @@ -72,6 +82,24 @@ const client = await SigningStargateClient.connectWithSigner(rpcEndpoint, wallet console.log("Successfully broadcasted:", result); } +// Send transaction (using signAndBroadcast with auto gas and custom muliplier) +{ + const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; + const amount = coins(1234567, "ucosm"); + const sendMsg: MsgSendEncodeObject = { + typeUrl: "/cosmos.bank.v1beta1.MsgSend", + value: { + fromAddress: account.address, + toAddress: recipient, + amount: amount, + }, + }; + const memo = "With simulate"; + const result = await client.signAndBroadcast(account.address, [sendMsg], 1.4, memo); + assertIsBroadcastTxSuccess(result); + console.log("Successfully broadcasted:", result); +} + // Send transaction (using signAndBroadcast with manual gas) { const recipient = "cosmos1xv9tklw7d82sezh9haa573wufgy59vmwe6xxe5"; diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index b8465523..0e6582b7 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -224,7 +224,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { public async upload( senderAddress: string, wasmCode: Uint8Array, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const compressed = pako.gzip(wasmCode, { level: 9 }); @@ -258,7 +258,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { codeId: number, msg: Record, label: string, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, options: InstantiateOptions = {}, ): Promise { const instantiateContractMsg: MsgInstantiateContractEncodeObject = { @@ -289,7 +289,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { senderAddress: string, contractAddress: string, newAdmin: string, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const updateAdminMsg: MsgUpdateAdminEncodeObject = { @@ -313,7 +313,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { public async clearAdmin( senderAddress: string, contractAddress: string, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const clearAdminMsg: MsgClearAdminEncodeObject = { @@ -338,7 +338,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { contractAddress: string, codeId: number, migrateMsg: Record, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const migrateContractMsg: MsgMigrateContractEncodeObject = { @@ -364,7 +364,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { senderAddress: string, contractAddress: string, msg: Record, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", funds?: readonly Coin[], ): Promise { @@ -391,7 +391,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { senderAddress: string, recipientAddress: string, amount: readonly Coin[], - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const sendMsg: MsgSendEncodeObject = { @@ -409,7 +409,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { delegatorAddress: string, validatorAddress: string, amount: Coin, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const delegateMsg: MsgDelegateEncodeObject = { @@ -423,7 +423,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { delegatorAddress: string, validatorAddress: string, amount: Coin, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const undelegateMsg: MsgUndelegateEncodeObject = { @@ -436,7 +436,7 @@ export class SigningCosmWasmClient extends CosmWasmClient { public async withdrawRewards( delegatorAddress: string, validatorAddress: string, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const withdrawDelegatorRewardMsg: MsgWithdrawDelegatorRewardEncodeObject = { @@ -457,14 +457,15 @@ export class SigningCosmWasmClient extends CosmWasmClient { public async signAndBroadcast( signerAddress: string, messages: readonly EncodeObject[], - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { let usedFee: StdFee; - if (fee == "auto") { + if (fee == "auto" || typeof fee === "number") { assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used."); const gasEstimation = await this.simulate(signerAddress, messages, memo); - usedFee = calculateFee(Math.round(gasEstimation * 1.3), this.gasPrice); + const muliplier = typeof fee === "number" ? fee : 1.3; + usedFee = calculateFee(Math.round(gasEstimation * muliplier), this.gasPrice); } else { usedFee = fee; } diff --git a/packages/stargate/src/signingstargateclient.ts b/packages/stargate/src/signingstargateclient.ts index ec8d55af..bb8895b0 100644 --- a/packages/stargate/src/signingstargateclient.ts +++ b/packages/stargate/src/signingstargateclient.ts @@ -208,7 +208,7 @@ export class SigningStargateClient extends StargateClient { senderAddress: string, recipientAddress: string, amount: readonly Coin[], - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const sendMsg: MsgSendEncodeObject = { @@ -226,7 +226,7 @@ export class SigningStargateClient extends StargateClient { delegatorAddress: string, validatorAddress: string, amount: Coin, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const delegateMsg: MsgDelegateEncodeObject = { @@ -244,7 +244,7 @@ export class SigningStargateClient extends StargateClient { delegatorAddress: string, validatorAddress: string, amount: Coin, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const undelegateMsg: MsgUndelegateEncodeObject = { @@ -261,7 +261,7 @@ export class SigningStargateClient extends StargateClient { public async withdrawRewards( delegatorAddress: string, validatorAddress: string, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const withdrawMsg: MsgWithdrawDelegatorRewardEncodeObject = { @@ -283,7 +283,7 @@ export class SigningStargateClient extends StargateClient { timeoutHeight: Height | undefined, /** timeout in seconds */ timeoutTimestamp: number | undefined, - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { const timeoutTimestampNanoseconds = timeoutTimestamp @@ -307,14 +307,15 @@ export class SigningStargateClient extends StargateClient { public async signAndBroadcast( signerAddress: string, messages: readonly EncodeObject[], - fee: StdFee | "auto", + fee: StdFee | "auto" | number, memo = "", ): Promise { let usedFee: StdFee; - if (fee == "auto") { + if (fee == "auto" || typeof fee === "number") { assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used."); const gasEstimation = await this.simulate(signerAddress, messages, memo); - usedFee = calculateFee(Math.round(gasEstimation * 1.3), this.gasPrice); + const muliplier = typeof fee === "number" ? fee : 1.3; + usedFee = calculateFee(Math.round(gasEstimation * muliplier), this.gasPrice); } else { usedFee = fee; }