Add CosmWasmClient.getHeight()

This commit is contained in:
Simon Warta 2020-03-07 17:30:43 +01:00
parent 2d85ecc840
commit e7f60e9abd
4 changed files with 22 additions and 3 deletions

View File

@ -138,8 +138,7 @@ export class CosmWasmConnection implements BlockchainConnection {
}
public async height(): Promise<number> {
const { header } = await this.cosmWasmClient.getBlock();
return header.height;
return this.cosmWasmClient.getHeight();
}
public async getToken(searchTicker: TokenTicker): Promise<Token | undefined> {

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/camelcase */
import { Sha256 } from "@iov/crypto";
import { Bech32, Encoding } from "@iov/encoding";
import { assert } from "@iov/utils";
import { assert, sleep } from "@iov/utils";
import { ReadonlyDate } from "readonly-date";
import { Code, CosmWasmClient } from "./cosmwasmclient";
@ -56,6 +56,18 @@ describe("CosmWasmClient", () => {
});
});
describe("getHeight", () => {
it("works", async () => {
pendingWithoutWasmd();
const client = new CosmWasmClient(wasmdEndpoint);
const height1 = await client.getHeight();
expect(height1).toBeGreaterThan(0);
await sleep(1_000);
const height2 = await client.getHeight();
expect(height2).toEqual(height1 + 1);
});
});
describe("getNonce", () => {
it("works", async () => {
pendingWithoutWasmd();

View File

@ -149,6 +149,13 @@ export class CosmWasmClient {
return response.node_info.network;
}
public async getHeight(): Promise<number> {
// Note: this gets inefficient when blocks contain a lot of transactions since it
// requires downloading and deserializing all transactions in the block.
const latest = await this.restClient.blocksLatest();
return parseInt(latest.block.header.height, 10);
}
/**
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)
*/

View File

@ -107,6 +107,7 @@ export declare class CosmWasmClient {
protected readonly restClient: RestClient;
constructor(url: string, broadcastMode?: BroadcastMode);
chainId(): Promise<string>;
getHeight(): Promise<number>;
/**
* Returns a 32 byte upper-case hex transaction hash (typically used as the transaction ID)
*/