From a02451b250d9641de08ea039f737420a5215d7dc Mon Sep 17 00:00:00 2001 From: DrPresident Date: Mon, 30 Oct 2023 12:17:22 -0400 Subject: [PATCH] feat: rough start, needs work --- src/client/services/clientServices.ts | 58 +++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/client/services/clientServices.ts b/src/client/services/clientServices.ts index 10b2ef3..f5660f6 100644 --- a/src/client/services/clientServices.ts +++ b/src/client/services/clientServices.ts @@ -8,6 +8,8 @@ import { SecretNetworkClient } from 'secretjs'; import { createFetchClient } from '~/client/services/createFetch'; import { identifyQueryResponseErrors } from '~/errors'; +import { Buffer } from 'buffer'; + /** * query the contract using a secret client */ @@ -29,6 +31,40 @@ const secretClientContractQuery$ = ({ })), )); +type BatchQuery = { + contract: { + address: string, + code_hash: string, + }, + query: any, +} + +const secretClientBatchQuery$ = ({ + client, + queryRouterAddress, + queryRouterCodeHash, + queries, +}: { + client: SecretNetworkClient, + queryRouterAddress: string, + queryRouterCodeHash?: string + queries: BatchQuery[], +}) => createFetchClient(defer( + () => from(client.query.compute.queryContract({ + contract_address: queryRouterAddress, + code_hash: queryRouterCodeHash, + query: { + batch: { + queries: queries.map((q, i) => ({ + id: Buffer.from(i.toString(), 'binary').toString('base64'), + contract: q.contract, + query: Buffer.from(JSON.stringify(q.query), 'binary').toString('base64'), + })), + }, + }, + })), +)); + /** * sets up the service observable for calling the querying with the secret client */ @@ -53,6 +89,28 @@ const sendSecretClientContractQuery$ = ({ first(), ); +const sendSecretClientBatchQuery$ = ({ + client, + queryRouterAddress, + queryRouterCodeHash, + queries, +}: { + client: SecretNetworkClient, + queryRouterAddress: string, + queryRouterCodeHash?: string + queries: BatchQuery[], +}) => secretClientBatchQuery$({ + client, + queryRouterAddress, + queryRouterCodeHash, + queries, +}) + .pipe( + tap((response) => identifyQueryResponseErrors(response)), + first(), + ); + export { sendSecretClientContractQuery$, + sendSecretClientBatchQuery$, };