Skip to content

Commit 91c980f

Browse files
authored
feat: allow to specify an AccountParser inside CosmWasmClient (#1928)
1 parent d95256e commit 91c980f

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ and this project adheres to
2121
using the `DynamicGasPriceConfig` interface for `gasPrice`. This then uses
2222
Osmosis' EIP-1559 implementation or the Skip fee market module to get the gas
2323
price from the chain. ([#1926])
24+
- @cosmjs/cosmwasm-stargate: Add the ability to specify a custom account parser
25+
for `CosmWasmClient`.
2426

2527
[#1883]: https://github.com/cosmos/cosmjs/issues/1883
2628
[#1916]: https://github.com/cosmos/cosmjs/pull/1916

packages/cosmwasm/src/cosmwasmclient.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Uint53 } from "@cosmjs/math";
33
import {
44
Account,
55
accountFromAny,
6+
AccountParser,
67
AuthExtension,
78
BankExtension,
89
Block,
@@ -81,35 +82,43 @@ export interface PrivateCosmWasmClient {
8182
| undefined;
8283
}
8384

85+
export interface CosmWasmClientOptions {
86+
readonly accountParser?: AccountParser;
87+
}
88+
8489
export class CosmWasmClient {
8590
private readonly cometClient: CometClient | undefined;
8691
private readonly queryClient:
8792
| (QueryClient & AuthExtension & BankExtension & TxExtension & WasmExtension)
8893
| undefined;
8994
private readonly codesCache = new Map<number, CodeDetails>();
9095
private chainId: string | undefined;
96+
private readonly accountParser: AccountParser;
9197

9298
/**
9399
* Creates an instance by connecting to the given CometBFT RPC endpoint.
94100
*
95101
* This uses auto-detection to decide between a CometBFT 1.x, CometBFT 0.38 and Tendermint 0.37 client.
96102
* To set the Comet client explicitly, use `create`.
97103
*/
98-
public static async connect(endpoint: string | HttpEndpoint): Promise<CosmWasmClient> {
104+
public static async connect(
105+
endpoint: string | HttpEndpoint,
106+
options: CosmWasmClientOptions = {},
107+
): Promise<CosmWasmClient> {
99108
const cometClient = await connectComet(endpoint);
100-
return CosmWasmClient.create(cometClient);
109+
return CosmWasmClient.create(cometClient, options);
101110
}
102111

103112
/**
104113
* Creates an instance from a manually created Comet client.
105114
* Use this to use `Comet38Client` or `Tendermint37Client` instead of
106115
* auto-detection.
107116
*/
108-
public static create(cometClient: CometClient): CosmWasmClient {
109-
return new CosmWasmClient(cometClient);
117+
public static create(cometClient: CometClient, options: CosmWasmClientOptions = {}): CosmWasmClient {
118+
return new CosmWasmClient(cometClient, options);
110119
}
111120

112-
protected constructor(cometClient: CometClient | undefined) {
121+
protected constructor(cometClient: CometClient | undefined, options: CosmWasmClientOptions = {}) {
113122
if (cometClient) {
114123
this.cometClient = cometClient;
115124
this.queryClient = QueryClient.withExtensions(
@@ -120,6 +129,8 @@ export class CosmWasmClient {
120129
setupTxExtension,
121130
);
122131
}
132+
const { accountParser = accountFromAny } = options;
133+
this.accountParser = accountParser;
123134
}
124135

125136
protected getCometClient(): CometClient | undefined {
@@ -165,7 +176,7 @@ export class CosmWasmClient {
165176
public async getAccount(searchAddress: string): Promise<Account | null> {
166177
try {
167178
const account = await this.forceGetQueryClient().auth.account(searchAddress);
168-
return account ? accountFromAny(account) : null;
179+
return account ? this.accountParser(account) : null;
169180
} catch (error: any) {
170181
if (/rpc error: code = NotFound/i.test(String(error))) {
171182
return null;

packages/cosmwasm/src/signingcosmwasmclient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
import { AccessConfig } from "cosmjs-types/cosmwasm/wasm/v1/types";
5454

5555
import { gzip } from "./compression";
56-
import { CosmWasmClient } from "./cosmwasmclient";
56+
import { CosmWasmClient, CosmWasmClientOptions } from "./cosmwasmclient";
5757
import {
5858
createWasmAminoConverters,
5959
JsonObject,
@@ -189,7 +189,7 @@ function createDeliverTxResponseErrorMessage(result: DeliverTxResponse): string
189189
return `Error when broadcasting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`;
190190
}
191191

192-
export interface SigningCosmWasmClientOptions {
192+
export interface SigningCosmWasmClientOptions extends CosmWasmClientOptions {
193193
readonly registry?: Registry;
194194
readonly aminoTypes?: AminoTypes;
195195
readonly broadcastTimeoutMs?: number;
@@ -261,7 +261,7 @@ export class SigningCosmWasmClient extends CosmWasmClient {
261261
signer: OfflineSigner,
262262
options: SigningCosmWasmClientOptions,
263263
) {
264-
super(cometClient);
264+
super(cometClient, options);
265265
const {
266266
registry = new Registry([...defaultStargateTypes, ...wasmTypes]),
267267
aminoTypes = new AminoTypes({

0 commit comments

Comments
 (0)