From 47f13b231aebfc4b2323188fe7f3e9c354fa2d38 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Thu, 23 Jun 2022 16:42:51 +0200 Subject: [PATCH] Add SigningCosmWasmClient.executeMultiple --- CHANGELOG.md | 3 ++ packages/cosmwasm-stargate/src/index.ts | 1 + .../src/signingcosmwasmclient.ts | 35 +++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4f9bde772..ba5791f16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,10 @@ and this project adheres to `makeMultisignedTx` but returns bytes ready to broadcast ([#1176]). - @cosmjs/tendermint-rpc: Add fields `codespace` and `info` to `AbciQueryResponse`. +- @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.executeMultiple` + ([#1072]). +[#1072]: https://github.com/cosmos/cosmjs/issues/1072 [#1176]: https://github.com/cosmos/cosmjs/pull/1176 ### Fixed diff --git a/packages/cosmwasm-stargate/src/index.ts b/packages/cosmwasm-stargate/src/index.ts index 49908b5d19..4b00e8988b 100644 --- a/packages/cosmwasm-stargate/src/index.ts +++ b/packages/cosmwasm-stargate/src/index.ts @@ -21,6 +21,7 @@ export { } from "./modules"; export { ChangeAdminResult, + ExecuteInstruction, ExecuteResult, InstantiateOptions, InstantiateResult, diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index 69e2d71d4e..2065437c36 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -135,6 +135,12 @@ export interface MigrateResult { readonly gasUsed: number; } +export interface ExecuteInstruction { + contractAddress: string; + msg: Record; + funds?: readonly Coin[]; +} + export interface ExecuteResult { readonly logs: readonly logs.Log[]; /** Block height in which the transaction is included */ @@ -396,16 +402,33 @@ export class SigningCosmWasmClient extends CosmWasmClient { memo = "", funds?: readonly Coin[], ): Promise { - const executeContractMsg: MsgExecuteContractEncodeObject = { + const instruction: ExecuteInstruction = { + contractAddress: contractAddress, + msg: msg, + funds: funds, + }; + return this.executeMultiple(senderAddress, [instruction], fee, memo); + } + + /** + * Like `execute` but allows executing multiple messages in one transaction. + */ + public async executeMultiple( + senderAddress: string, + instructions: readonly ExecuteInstruction[], + fee: StdFee | "auto" | number, + memo = "", + ): Promise { + const msgs: MsgExecuteContractEncodeObject[] = instructions.map((i) => ({ typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", value: MsgExecuteContract.fromPartial({ sender: senderAddress, - contract: contractAddress, - msg: toUtf8(JSON.stringify(msg)), - funds: [...(funds || [])], + contract: i.contractAddress, + msg: toUtf8(JSON.stringify(i.msg)), + funds: [...(i.funds || [])], }), - }; - const result = await this.signAndBroadcast(senderAddress, [executeContractMsg], fee, memo); + })); + const result = await this.signAndBroadcast(senderAddress, msgs, fee, memo); if (isDeliverTxFailure(result)) { throw new Error(createDeliverTxResponseErrorMessage(result)); }