Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to specify an AccountParser inside CosmWasmClient #1610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to

## [Unreleased]

### Added

- @cosmjs/cosmwasm-stargate: Added the ability to specify a custom account parser for `CosmWasmClient`

## [0.32.4] - 2024-06-26

### Fixed
Expand Down
20 changes: 14 additions & 6 deletions packages/cosmwasm-stargate/src/cosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Uint53 } from "@cosmjs/math";
import {
Account,
accountFromAny,
AccountParser,
AuthExtension,
BankExtension,
Block,
Expand Down Expand Up @@ -82,34 +83,39 @@ export interface PrivateCosmWasmClient {
| undefined;
}

export interface CosmWasmClientOptions {
readonly accountParser?: AccountParser;
}

export class CosmWasmClient {
private readonly cometClient: CometClient | undefined;
private readonly queryClient:
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
| undefined;
private readonly codesCache = new Map<number, CodeDetails>();
private chainId: string | undefined;
private readonly accountParser: AccountParser;

/**
* Creates an instance by connecting to the given CometBFT RPC endpoint.
*
* This uses auto-detection to decide between a CometBFT 0.38, Tendermint 0.37 and 0.34 client.
* To set the Comet client explicitly, use `create`.
*/
public static async connect(endpoint: string | HttpEndpoint): Promise<CosmWasmClient> {
public static async connect(endpoint: string | HttpEndpoint, options: CosmWasmClientOptions = {}): Promise<CosmWasmClient> {
const cometClient = await connectComet(endpoint);
return CosmWasmClient.create(cometClient);
return CosmWasmClient.create(cometClient, options);
}

/**
* Creates an instance from a manually created Comet client.
* Use this to use `Comet38Client` or `Tendermint37Client` instead of `Tendermint34Client`.
*/
public static async create(cometClient: CometClient): Promise<CosmWasmClient> {
return new CosmWasmClient(cometClient);
public static async create(cometClient: CometClient, options: CosmWasmClientOptions = {}): Promise<CosmWasmClient> {
return new CosmWasmClient(cometClient, options);
}

protected constructor(cometClient: CometClient | undefined) {
protected constructor(cometClient: CometClient | undefined, options: CosmWasmClientOptions = {}) {
if (cometClient) {
this.cometClient = cometClient;
this.queryClient = QueryClient.withExtensions(
Expand All @@ -120,6 +126,8 @@ export class CosmWasmClient {
setupTxExtension,
);
}
const { accountParser = accountFromAny } = options;
this.accountParser = accountParser;
}

protected getCometClient(): CometClient | undefined {
Expand Down Expand Up @@ -165,7 +173,7 @@ export class CosmWasmClient {
public async getAccount(searchAddress: string): Promise<Account | null> {
try {
const account = await this.forceGetQueryClient().auth.account(searchAddress);
return account ? accountFromAny(account) : null;
return account ? this.accountParser(account) : null;
} catch (error: any) {
if (/rpc error: code = NotFound/i.test(error.toString())) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
import { AccessConfig } from "cosmjs-types/cosmwasm/wasm/v1/types";
import pako from "pako";

import { CosmWasmClient } from "./cosmwasmclient";
import { CosmWasmClient, CosmWasmClientOptions } from "./cosmwasmclient";
import {
createWasmAminoConverters,
JsonObject,
Expand Down Expand Up @@ -186,7 +186,7 @@ function createDeliverTxResponseErrorMessage(result: DeliverTxResponse): string
return `Error when broadcasting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`;
}

export interface SigningCosmWasmClientOptions {
export interface SigningCosmWasmClientOptions extends CosmWasmClientOptions {
readonly registry?: Registry;
readonly aminoTypes?: AminoTypes;
readonly broadcastTimeoutMs?: number;
Expand Down Expand Up @@ -254,7 +254,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
signer: OfflineSigner,
options: SigningCosmWasmClientOptions,
) {
super(cometClient);
super(cometClient, options);
const {
registry = new Registry([...defaultStargateTypes, ...wasmTypes]),
aminoTypes = new AminoTypes({
Expand Down