From 75f5ac9e997e562473f4ddc5a7228d21b2238be2 Mon Sep 17 00:00:00 2001 From: eliasmpw Date: Thu, 12 Sep 2024 13:11:58 +0200 Subject: [PATCH] fix: batch client creation, better handle disconnects --- CHANGELOG.md | 9 +++++ packages/arch3-core/src/archwayclient.ts | 10 +++--- .../arch3-core/src/signingarchwayclient.ts | 5 ++- packages/arch3-core/src/utils.ts | 36 +++++++++++++------ 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6d2ee9..7781a8c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## v0.7.3 (Unreleased) + +### Fixed + +#### **arch3-core** + +- refactor `connectToRpcClient` into `connectCometWithBatchClient`, adding +back support for using `Comet38Client` with 0.38.x RPC Clients ([#123]) + ## v0.7.2 (2024-09-11) ### Fixed diff --git a/packages/arch3-core/src/archwayclient.ts b/packages/arch3-core/src/archwayclient.ts index b99289c..a91ff47 100644 --- a/packages/arch3-core/src/archwayclient.ts +++ b/packages/arch3-core/src/archwayclient.ts @@ -21,7 +21,7 @@ import { RewardsPool, RewardsRecord } from './types'; -import { connectToRpcClient } from './utils'; +import { connectCometWithBatchClient } from './utils'; /** * Extension to the {@link CosmWasmClient } with queries for Archway's modules. @@ -57,9 +57,11 @@ export class ArchwayClient extends CosmWasmClient implements IArchwayQueryClient * * @remarks This factory method doesn't support WebSocket endpoints. */ - public static async connectWithBatchClient(endpoint: string | HttpEndpoint, options?: Partial): Promise { - const rpcClient: RpcClient = new HttpBatchClient(endpoint, options); - const cometBatchClient = await connectToRpcClient(rpcClient); + public static async connectWithBatchClient( + endpoint: string | HttpEndpoint, + options?: Partial, + ): Promise { + const cometBatchClient = await connectCometWithBatchClient(endpoint, options); return ArchwayClient.create(cometBatchClient); } diff --git a/packages/arch3-core/src/signingarchwayclient.ts b/packages/arch3-core/src/signingarchwayclient.ts index e9e11c4..b6d570c 100644 --- a/packages/arch3-core/src/signingarchwayclient.ts +++ b/packages/arch3-core/src/signingarchwayclient.ts @@ -35,7 +35,7 @@ import { RewardsPool, RewardsRecord, } from './types'; -import { connectToRpcClient } from './utils'; +import { connectCometWithBatchClient } from './utils'; export interface SigningArchwayClientOptions extends SigningCosmWasmClientOptions { /** @@ -187,8 +187,7 @@ export class SigningArchwayClient extends SigningCosmWasmClient implements IArch options?: SigningArchwayClientOptions, batchClientOptions?: Partial, ): Promise { - const rpcClient: RpcClient = new HttpBatchClient(endpoint, batchClientOptions); - const cometBatchClient = await connectToRpcClient(rpcClient); + const cometBatchClient = await connectCometWithBatchClient(endpoint, batchClientOptions); return SigningArchwayClient.createWithSigner(cometBatchClient, signer, options); } diff --git a/packages/arch3-core/src/utils.ts b/packages/arch3-core/src/utils.ts index 915302c..1fb3678 100644 --- a/packages/arch3-core/src/utils.ts +++ b/packages/arch3-core/src/utils.ts @@ -1,26 +1,40 @@ -import { Tendermint37Client, RpcClient, Comet38Client, Tendermint34Client, CometClient } from '@cosmjs/tendermint-rpc'; +import { + Comet38Client, + CometClient, + HttpEndpoint, + HttpBatchClientOptions, + HttpBatchClient, + RpcClient, + Tendermint37Client, + Tendermint34Client, +} from '@cosmjs/tendermint-rpc'; /** - * Auto-detects Tendermint/CometBFT 0.34/0.37/0.38 version for connecting to an RPC Client. + * Auto-detects Tendermint/CometBFT 0.34/0.37/0.38 version for connecting to an RPC Client with an HttpBatchClient. * - * @param rpcClient - Instance of {@link RpcClient} to connect to. - * @returns Suitable client version of {@link CometClient}. + * @param endpoint - String URL of the RPC endpoint to connect or an {@link HttpEndpoint} object. + * @param options - Optional configuration to control how the {@link HttpBatchClient} will batch requests. */ -export async function connectToRpcClient(rpcClient: RpcClient): Promise { +export async function connectCometWithBatchClient( + endpoint: string | HttpEndpoint, + options?: Partial, +): Promise { + let rpcClient: RpcClient = new HttpBatchClient(endpoint, options); const tendermint37Client = await Tendermint37Client.create(rpcClient); const version = (await tendermint37Client.status()).nodeInfo.version; - // HACK: Force Tendermint37Client on 0.38.x versions, remove when client batching issue is fixed - if (version.startsWith('0.37.') || version.startsWith('0.38.')) { + if (version.startsWith('0.37.')) { return tendermint37Client; } tendermint37Client.disconnect(); + rpcClient.disconnect(); - // if (version.startsWith('0.38.')) { - // console.log('VERRRRR2222',version) - // return await Comet38Client.create(rpcClient); - // } + rpcClient = new HttpBatchClient(endpoint, options); + + if (version.startsWith('0.38.')) { + return await Comet38Client.create(rpcClient); + } return await Tendermint34Client.create(rpcClient); }