From 770d3c6b4a3a6d8df4b31d0664c5ee9dcd4702d8 Mon Sep 17 00:00:00 2001 From: Zetazzz Date: Tue, 14 Jan 2025 06:26:06 +0800 Subject: [PATCH] make tx support multiple messages --- .../interchain-react/src/helper-func-types.ts | 55 +++++++++++-------- libs/interchain-react/src/react-query.ts | 2 +- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/libs/interchain-react/src/helper-func-types.ts b/libs/interchain-react/src/helper-func-types.ts index ece526a8..237f1fd9 100644 --- a/libs/interchain-react/src/helper-func-types.ts +++ b/libs/interchain-react/src/helper-func-types.ts @@ -22,28 +22,28 @@ export interface QueryBuilderOptions { } export function buildQuery(opts: QueryBuilderOptions) { - registerDependencies(opts.deps ?? []); + registerDependencies(opts.deps ?? []); - return async (request: TReq) => { - let rpc: Rpc | undefined; + return async (request: TReq) => { + let rpc: Rpc | undefined; - if(isRpc(opts.clientResolver)) { - rpc = opts.clientResolver; - } else { - rpc = opts.clientResolver ? await getRpcClient(opts.clientResolver) : undefined; - } + if (isRpc(opts.clientResolver)) { + rpc = opts.clientResolver; + } else { + rpc = opts.clientResolver ? await getRpcClient(opts.clientResolver) : undefined; + } - if (!rpc) throw new Error("Query Rpc is not initialized"); + if (!rpc) throw new Error("Query Rpc is not initialized"); - const data = opts.encode(request).finish(); - const response = await rpc.request(opts.service, opts.method, data); - return opts.decode(response); - }; + const data = opts.encode(request).finish(); + const response = await rpc.request(opts.service, opts.method, data); + return opts.decode(response); + }; } export interface ITxArgs { signerAddress: string; - message: TMsg; + message: TMsg | TMsg[]; fee: StdFee | 'auto'; memo: string; } @@ -86,14 +86,14 @@ export function buildTx(opts: TxBuilderOptions) { return async ( signerAddress: string, - message: TMsg, + message: TMsg | TMsg[], fee: StdFee | 'auto', memo: string ): Promise => { let client: ISigningClient | undefined; // if opts.getSigningClient is a function, call it to get the SigningClient instance - if(isISigningClient(opts.clientResolver)) { + if (isISigningClient(opts.clientResolver)) { client = opts.clientResolver; } @@ -103,12 +103,21 @@ export function buildTx(opts: TxBuilderOptions) { client.addEncoders(opts.encoders ?? []); client.addConverters(opts.converters ?? []); - const data = [ - { - typeUrl: opts.typeUrl, - value: message, - }, - ]; + const data = Array.isArray(message) ? + message.map(msg => { + return { + typeUrl: opts.typeUrl, + value: msg, + } + }) : + [ + { + typeUrl: opts.typeUrl, + value: message, + }, + ]; + + return client.signAndBroadcast!(signerAddress, data, fee, memo); }; } @@ -196,7 +205,7 @@ export interface AminoConverter { } export type SigningClientResolver = string | HttpEndpoint | ISigningClient; -export type RpcResolver = string | HttpEndpoint | Rpc ; +export type RpcResolver = string | HttpEndpoint | Rpc; function registerDependencies(deps: TelescopeGeneratedCodec[]) { for (const dep of deps) { diff --git a/libs/interchain-react/src/react-query.ts b/libs/interchain-react/src/react-query.ts index 42a92f1e..98bd85d6 100644 --- a/libs/interchain-react/src/react-query.ts +++ b/libs/interchain-react/src/react-query.ts @@ -147,7 +147,7 @@ export interface ReactMutationParams { builderMutationFn: (clientResolver?: SigningClientResolver) => ( signerAddress: string, - message: TMsg, + message: TMsg | TMsg[], fee: StdFee | 'auto', memo: string ) => Promise,