Cache chain ID
This commit is contained in:
parent
cbcf854a98
commit
a50369e43e
@ -54,6 +54,18 @@ describe("CosmWasmClient", () => {
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
expect(await client.getChainId()).toEqual("testing");
|
||||
});
|
||||
|
||||
it("caches chain ID", async () => {
|
||||
pendingWithoutWasmd();
|
||||
const client = new CosmWasmClient(wasmdEndpoint);
|
||||
const openedClient = (client as unknown) as PrivateCosmWasmClient;
|
||||
const getCodeSpy = spyOn(openedClient.restClient, "nodeInfo").and.callThrough();
|
||||
|
||||
expect(await client.getChainId()).toEqual("testing"); // from network
|
||||
expect(await client.getChainId()).toEqual("testing"); // from cache
|
||||
|
||||
expect(getCodeSpy).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getHeight", () => {
|
||||
|
||||
@ -148,14 +148,21 @@ export class CosmWasmClient {
|
||||
protected anyValidAddress: string | undefined;
|
||||
|
||||
private readonly codesCache = new Map<number, CodeDetails>();
|
||||
private chainId: string | undefined;
|
||||
|
||||
public constructor(url: string, broadcastMode = BroadcastMode.Block) {
|
||||
this.restClient = new RestClient(url, broadcastMode);
|
||||
}
|
||||
|
||||
public async getChainId(): Promise<string> {
|
||||
const response = await this.restClient.nodeInfo();
|
||||
return response.node_info.network;
|
||||
if (!this.chainId) {
|
||||
const response = await this.restClient.nodeInfo();
|
||||
const chainId = response.node_info.network;
|
||||
if (!chainId) throw new Error("Chain ID must not be empty");
|
||||
this.chainId = chainId;
|
||||
}
|
||||
|
||||
return this.chainId;
|
||||
}
|
||||
|
||||
public async getHeight(): Promise<number> {
|
||||
|
||||
1
packages/sdk/types/cosmwasmclient.d.ts
vendored
1
packages/sdk/types/cosmwasmclient.d.ts
vendored
@ -112,6 +112,7 @@ export declare class CosmWasmClient {
|
||||
/** Any address the chain considers valid (valid bech32 with proper prefix) */
|
||||
protected anyValidAddress: string | undefined;
|
||||
private readonly codesCache;
|
||||
private chainId;
|
||||
constructor(url: string, broadcastMode?: BroadcastMode);
|
||||
getChainId(): Promise<string>;
|
||||
getHeight(): Promise<number>;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user