Skip to content

Commit

Permalink
Merge pull request #77 from cosmology-tech/tx-multiple-msg
Browse files Browse the repository at this point in the history
make tx support multiple messages
  • Loading branch information
Zetazzz authored Jan 13, 2025
2 parents 25d1cf0 + 770d3c6 commit 4c76a80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
55 changes: 32 additions & 23 deletions libs/interchain-react/src/helper-func-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ export interface QueryBuilderOptions<TReq, TRes> {
}

export function buildQuery<TReq, TRes>(opts: QueryBuilderOptions<TReq, TRes>) {
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<TMsg> {
signerAddress: string;
message: TMsg;
message: TMsg | TMsg[];
fee: StdFee | 'auto';
memo: string;
}
Expand Down Expand Up @@ -86,14 +86,14 @@ export function buildTx<TMsg>(opts: TxBuilderOptions) {

return async (
signerAddress: string,
message: TMsg,
message: TMsg | TMsg[],
fee: StdFee | 'auto',
memo: string
): Promise<DeliverTxResponse> => {
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;
}

Expand All @@ -103,12 +103,21 @@ export function buildTx<TMsg>(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);
};
}
Expand Down Expand Up @@ -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<any, any, any>[]) {
for (const dep of deps) {
Expand Down
2 changes: 1 addition & 1 deletion libs/interchain-react/src/react-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export interface ReactMutationParams<TData, TError, TVariables, TContext = unkno
export interface UseMutationBuilderOptions<TMsg> {
builderMutationFn: (clientResolver?: SigningClientResolver) => (
signerAddress: string,
message: TMsg,
message: TMsg | TMsg[],
fee: StdFee | 'auto',
memo: string
) => Promise<DeliverTxResponse>,
Expand Down

0 comments on commit 4c76a80

Please sign in to comment.