Add CosmWasmClient.getBlock

This commit is contained in:
Simon Warta 2020-02-15 16:00:09 +01:00
parent fa78a7460c
commit 1de2abb8ff
4 changed files with 68 additions and 2 deletions

View File

@ -42,5 +42,8 @@
"@iov/encoding": "^2.0.2",
"@iov/utils": "^2.0.2",
"axios": "^0.19.0"
},
"devDependencies": {
"readonly-date": "^1.0.0"
}
}

View File

@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/camelcase */
import { Bech32, Encoding } from "@iov/encoding";
import { assert, sleep } from "@iov/utils";
import { ReadonlyDate } from "readonly-date";
import { CosmWasmClient } from "./cosmwasmclient";
import { makeSignBytes, marshalTx } from "./encoding";
@ -74,6 +75,49 @@ describe("CosmWasmClient", () => {
});
});
describe("getBlock", () => {
it("works for latest block", async () => {
pendingWithoutCosmos();
const client = CosmWasmClient.makeReadOnly(httpUrl);
const response = await client.getBlock();
// id
expect(response.block_id.hash).toMatch(/^[0-9A-F]{64}$/);
// header
expect(parseInt(response.block.header.height, 10)).toBeGreaterThanOrEqual(1);
expect(response.block.header.chain_id).toEqual(await client.chainId());
expect(new ReadonlyDate(response.block.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.block.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
// data
expect(response.block.data.txs === null || Array.isArray(response.block.data.txs)).toEqual(true);
});
it("works for block by height", async () => {
pendingWithoutCosmos();
const client = CosmWasmClient.makeReadOnly(httpUrl);
const height = parseInt((await client.getBlock()).block.header.height, 10);
const response = await client.getBlock(height - 1);
// id
expect(response.block_id.hash).toMatch(/^[0-9A-F]{64}$/);
// header
expect(response.block.header.height).toEqual(`${height - 1}`);
expect(response.block.header.chain_id).toEqual(await client.chainId());
expect(new ReadonlyDate(response.block.header.time).getTime()).toBeLessThan(ReadonlyDate.now());
expect(new ReadonlyDate(response.block.header.time).getTime()).toBeGreaterThanOrEqual(
ReadonlyDate.now() - 5_000,
);
// data
expect(response.block.data.txs === null || Array.isArray(response.block.data.txs)).toEqual(true);
});
});
describe("getIdentifier", () => {
it("works", async () => {
pendingWithoutCosmos();

View File

@ -3,7 +3,7 @@ import { Encoding } from "@iov/encoding";
import { makeSignBytes, marshalTx } from "./encoding";
import { findAttribute, Log, parseLogs } from "./logs";
import { RestClient, TxsResponse } from "./restclient";
import { BlockResponse, RestClient, TxsResponse } from "./restclient";
import {
Coin,
CosmosSdkTx,
@ -157,6 +157,19 @@ export class CosmWasmClient {
};
}
/**
* Gets block header and meta
*
* @param height The height of the block. If undefined, the latest height is used.
*/
public async getBlock(height?: number): Promise<BlockResponse> {
if (height !== undefined) {
return this.restClient.blocks(height);
} else {
return this.restClient.blocksLatest();
}
}
public async searchTx(query: SearchTxQuery): Promise<readonly TxsResponse[]> {
// TODO: we need proper pagination support
function limited(originalQuery: string): string {

View File

@ -1,5 +1,5 @@
import { Log } from "./logs";
import { TxsResponse } from "./restclient";
import { BlockResponse, TxsResponse } from "./restclient";
import { Coin, CosmosSdkTx, StdSignature } from "./types";
export interface SigningCallback {
(signBytes: Uint8Array): Promise<StdSignature>;
@ -46,6 +46,12 @@ export declare class CosmWasmClient {
* @param address returns data for this address. When unset, the client's sender adddress is used.
*/
getNonce(address?: string): Promise<GetNonceResult>;
/**
* Gets block header and meta
*
* @param height The height of the block. If undefined, the latest height is used.
*/
getBlock(height?: number): Promise<BlockResponse>;
searchTx(query: SearchTxQuery): Promise<readonly TxsResponse[]>;
postTx(tx: Uint8Array): Promise<PostTxResult>;
/** Uploads code and returns a code ID */