Add StargateClient.getHeight

This commit is contained in:
Simon Warta 2020-08-10 15:29:34 +02:00
parent 331ecc605f
commit eb1f3a5211
4 changed files with 22 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import { assert } from "@cosmjs/utils";
import { assert, sleep } from "@cosmjs/utils";
import { PrivateStargateClient, StargateClient } from "./stargateclient";
import { nonExistentAddress, pendingWithoutSimapp, simapp, unused, validator } from "./testutils.spec";
@ -33,6 +33,20 @@ describe("StargateClient", () => {
});
});
describe("getHeight", () => {
it("works", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const height1 = await client.getHeight();
expect(height1).toBeGreaterThan(0);
await sleep(simapp.blockTime * 1.4); // tolerate chain being 40% slower than expected
const height2 = await client.getHeight();
expect(height2).toBeGreaterThanOrEqual(height1 + 1);
expect(height2).toBeLessThanOrEqual(height1 + 2);
});
});
describe("getAccount", () => {
it("works for unused account", async () => {
pendingWithoutSimapp();

View File

@ -78,6 +78,11 @@ export class StargateClient {
return this.chainId;
}
public async getHeight(): Promise<number> {
const status = await this.tmClient.status();
return status.syncInfo.latestBlockHeight;
}
public async getAccount(searchAddress: string): Promise<Account | null> {
const { prefix, data: binAddress } = Bech32.decode(searchAddress);
// https://github.com/cosmos/cosmos-sdk/blob/8cab43c8120fec5200c3459cbf4a92017bb6f287/x/auth/types/keys.go#L29-L32

View File

@ -9,6 +9,7 @@ export const simapp = {
chainId: "simd-testing",
denomStaking: "ustake",
denomFee: "ucosm",
blockTime: 1_000, // ms
};
/** Unused account */

View File

@ -21,6 +21,7 @@ export declare class StargateClient {
static connect(endpoint: string): Promise<StargateClient>;
private constructor();
getChainId(): Promise<string>;
getHeight(): Promise<number>;
getAccount(searchAddress: string): Promise<Account | null>;
getSequence(address: string): Promise<SequenceResponse | null>;
getBalance(address: string, searchDenom: string): Promise<Coin | null>;