Skip to content

Commit

Permalink
fix: batch client creation, better handle disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
eliasmpw committed Sep 12, 2024
1 parent 6b48cec commit 75f5ac9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions packages/arch3-core/src/archwayclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<HttpBatchClientOptions>): Promise<ArchwayClient> {
const rpcClient: RpcClient = new HttpBatchClient(endpoint, options);
const cometBatchClient = await connectToRpcClient(rpcClient);
public static async connectWithBatchClient(
endpoint: string | HttpEndpoint,
options?: Partial<HttpBatchClientOptions>,
): Promise<ArchwayClient> {
const cometBatchClient = await connectCometWithBatchClient(endpoint, options);
return ArchwayClient.create(cometBatchClient);
}

Expand Down
5 changes: 2 additions & 3 deletions packages/arch3-core/src/signingarchwayclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
RewardsPool,
RewardsRecord,
} from './types';
import { connectToRpcClient } from './utils';
import { connectCometWithBatchClient } from './utils';

export interface SigningArchwayClientOptions extends SigningCosmWasmClientOptions {
/**
Expand Down Expand Up @@ -187,8 +187,7 @@ export class SigningArchwayClient extends SigningCosmWasmClient implements IArch
options?: SigningArchwayClientOptions,
batchClientOptions?: Partial<HttpBatchClientOptions>,
): Promise<SigningArchwayClient> {
const rpcClient: RpcClient = new HttpBatchClient(endpoint, batchClientOptions);
const cometBatchClient = await connectToRpcClient(rpcClient);
const cometBatchClient = await connectCometWithBatchClient(endpoint, batchClientOptions);
return SigningArchwayClient.createWithSigner(cometBatchClient, signer, options);
}

Expand Down
36 changes: 25 additions & 11 deletions packages/arch3-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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<CometClient> {
export async function connectCometWithBatchClient(
endpoint: string | HttpEndpoint,
options?: Partial<HttpBatchClientOptions>,
): Promise<CometClient> {
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);
}

0 comments on commit 75f5ac9

Please sign in to comment.