stargate: Add getBlock method to client

This commit is contained in:
willclarktech 2020-08-12 12:28:04 +02:00
parent f969e661e2
commit cd7c5c2efa
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 72 additions and 3 deletions

View File

@ -3,6 +3,7 @@ import { Bech32, fromBase64 } from "@cosmjs/encoding";
import { Secp256k1Wallet } from "@cosmjs/launchpad";
import { makeSignBytes, omitDefaults, Registry } from "@cosmjs/proto-signing";
import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
import { cosmos } from "./generated/codecimpl";
import { assertIsBroadcastTxSuccess, PrivateStargateClient, StargateClient } from "./stargateclient";
@ -12,6 +13,7 @@ import {
nonExistentAddress,
pendingWithoutSimapp,
simapp,
tendermintIdMatcher,
unused,
validator,
} from "./testutils.spec";
@ -133,6 +135,53 @@ describe("StargateClient", () => {
});
});
describe("getBlock", () => {
it("works for latest block", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const response = await client.getBlock();
expect(response).toEqual(
jasmine.objectContaining({
id: jasmine.stringMatching(tendermintIdMatcher),
header: jasmine.objectContaining({
chainId: await client.getChainId(),
}),
txs: [],
}),
);
expect(response.header.height).toBeGreaterThanOrEqual(1);
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
});
it("works for block by height", async () => {
pendingWithoutSimapp();
const client = await StargateClient.connect(simapp.tendermintUrl);
const height = (await client.getBlock()).header.height;
const response = await client.getBlock(height - 1);
expect(response).toEqual(
jasmine.objectContaining({
id: jasmine.stringMatching(tendermintIdMatcher),
header: jasmine.objectContaining({
height: height - 1,
chainId: await client.getChainId(),
}),
txs: [],
}),
);
expect(new ReadonlyDate(response.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
});
});
describe("getBalance", () => {
it("works for different existing balances", async () => {
pendingWithoutSimapp();

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Bech32, toAscii, toHex } from "@cosmjs/encoding";
import { Coin, decodeAminoPubkey, PubKey } from "@cosmjs/launchpad";
import { Uint64 } from "@cosmjs/math";
import { Block, Coin, decodeAminoPubkey, PubKey } from "@cosmjs/launchpad";
import { Uint53, Uint64 } from "@cosmjs/math";
import { decodeAny } from "@cosmjs/proto-signing";
import { broadcastTxCommitSuccess, Client as TendermintClient } from "@cosmjs/tendermint-rpc";
import { arrayContentEquals, assert, assertDefined } from "@cosmjs/utils";
@ -151,6 +151,23 @@ export class StargateClient {
}
}
public async getBlock(height?: number): Promise<Block> {
const response = await this.tmClient.block(height);
return {
id: toHex(response.blockId.hash).toUpperCase(),
header: {
version: {
block: new Uint53(response.block.header.version.block).toString(),
app: new Uint53(response.block.header.version.app).toString(),
},
height: response.block.header.height,
chainId: response.block.header.chainId,
time: response.block.header.time.toISOString(),
},
txs: response.block.txs,
};
}
public async getBalance(address: string, searchDenom: string): Promise<Coin | null> {
// balance key is a bit tricker, using some prefix stores
// https://github.com/cosmwasm/cosmos-sdk/blob/80f7ff62f79777a487d0c7a53c64b0f7e43c47b9/x/bank/keeper/view.go#L74-L77

View File

@ -54,3 +54,5 @@ export const validator = {
};
export const nonExistentAddress = "cosmos1p79apjaufyphcmsn4g07cynqf0wyjuezqu84hd";
export const tendermintIdMatcher = /^[0-9A-F]{64}$/;

View File

@ -1,4 +1,4 @@
import { Coin, PubKey } from "@cosmjs/launchpad";
import { Block, Coin, PubKey } from "@cosmjs/launchpad";
import { Client as TendermintClient } from "@cosmjs/tendermint-rpc";
export interface Account {
/** Bech32 account address */
@ -46,6 +46,7 @@ export declare class StargateClient {
getHeight(): Promise<number>;
getAccount(searchAddress: string): Promise<Account | null>;
getSequence(address: string): Promise<SequenceResponse | null>;
getBlock(height?: number): Promise<Block>;
getBalance(address: string, searchDenom: string): Promise<Coin | null>;
/**
* Queries all balances for all denoms that belong to this address.