Add StargateClient.getHeight
This commit is contained in:
parent
331ecc605f
commit
eb1f3a5211
@ -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();
|
||||
|
||||
@ -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
|
||||
|
||||
@ -9,6 +9,7 @@ export const simapp = {
|
||||
chainId: "simd-testing",
|
||||
denomStaking: "ustake",
|
||||
denomFee: "ucosm",
|
||||
blockTime: 1_000, // ms
|
||||
};
|
||||
|
||||
/** Unused account */
|
||||
|
||||
1
packages/stargate/types/stargateclient.d.ts
vendored
1
packages/stargate/types/stargateclient.d.ts
vendored
@ -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>;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user