From 278520a89ff99610ac57da4aa58bae78035075a2 Mon Sep 17 00:00:00 2001 From: elshenak Date: Thu, 3 Oct 2024 23:38:49 -0400 Subject: [PATCH] feat: allow to specify an AccountParser inside CosmWasmClient --- CHANGELOG.md | 4 ++++ .../cosmwasm-stargate/src/cosmwasmclient.ts | 20 +++++++++++++------ .../src/signingcosmwasmclient.ts | 6 +++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce5b2332c6..e75109164a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/cosmwasm-stargate/src/cosmwasmclient.ts b/packages/cosmwasm-stargate/src/cosmwasmclient.ts index c4b3e14c5c..ca84f48fb9 100644 --- a/packages/cosmwasm-stargate/src/cosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/cosmwasmclient.ts @@ -4,6 +4,7 @@ import { Uint53 } from "@cosmjs/math"; import { Account, accountFromAny, + AccountParser, AuthExtension, BankExtension, Block, @@ -82,6 +83,10 @@ export interface PrivateCosmWasmClient { | undefined; } +export interface CosmWasmClientOptions { + readonly accountParser?: AccountParser; +} + export class CosmWasmClient { private readonly cometClient: CometClient | undefined; private readonly queryClient: @@ -89,6 +94,7 @@ export class CosmWasmClient { | undefined; private readonly codesCache = new Map(); private chainId: string | undefined; + private readonly accountParser: AccountParser; /** * Creates an instance by connecting to the given CometBFT RPC endpoint. @@ -96,20 +102,20 @@ export class CosmWasmClient { * 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 { + public static async connect(endpoint: string | HttpEndpoint, options: CosmWasmClientOptions = {}): Promise { 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 { - return new CosmWasmClient(cometClient); + public static async create(cometClient: CometClient, options: CosmWasmClientOptions = {}): Promise { + 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( @@ -120,6 +126,8 @@ export class CosmWasmClient { setupTxExtension, ); } + const { accountParser = accountFromAny } = options; + this.accountParser = accountParser; } protected getCometClient(): CometClient | undefined { @@ -165,7 +173,7 @@ export class CosmWasmClient { public async getAccount(searchAddress: string): Promise { 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; diff --git a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts index bce739bbf2..cf25f5db03 100644 --- a/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts +++ b/packages/cosmwasm-stargate/src/signingcosmwasmclient.ts @@ -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, @@ -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; @@ -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({