Cache chain ID

This commit is contained in:
Simon Warta 2020-03-11 10:39:37 +01:00
parent cbcf854a98
commit a50369e43e
3 changed files with 22 additions and 2 deletions

View File

@ -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", () => {

View File

@ -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> {

View File

@ -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>;