From 1de2abb8ffdf6bd0e0b1e6dc6d1aadaad7e93eb7 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 15 Feb 2020 16:00:09 +0100 Subject: [PATCH] Add CosmWasmClient.getBlock --- packages/sdk/package.json | 3 ++ packages/sdk/src/cosmwasmclient.spec.ts | 44 +++++++++++++++++++++++++ packages/sdk/src/cosmwasmclient.ts | 15 ++++++++- packages/sdk/types/cosmwasmclient.d.ts | 8 ++++- 4 files changed, 68 insertions(+), 2 deletions(-) diff --git a/packages/sdk/package.json b/packages/sdk/package.json index fba2fbb3..a45a3bee 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -42,5 +42,8 @@ "@iov/encoding": "^2.0.2", "@iov/utils": "^2.0.2", "axios": "^0.19.0" + }, + "devDependencies": { + "readonly-date": "^1.0.0" } } diff --git a/packages/sdk/src/cosmwasmclient.spec.ts b/packages/sdk/src/cosmwasmclient.spec.ts index ce821e7c..72321a1b 100644 --- a/packages/sdk/src/cosmwasmclient.spec.ts +++ b/packages/sdk/src/cosmwasmclient.spec.ts @@ -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(); diff --git a/packages/sdk/src/cosmwasmclient.ts b/packages/sdk/src/cosmwasmclient.ts index 1079dd85..b80e86f8 100644 --- a/packages/sdk/src/cosmwasmclient.ts +++ b/packages/sdk/src/cosmwasmclient.ts @@ -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 { + if (height !== undefined) { + return this.restClient.blocks(height); + } else { + return this.restClient.blocksLatest(); + } + } + public async searchTx(query: SearchTxQuery): Promise { // TODO: we need proper pagination support function limited(originalQuery: string): string { diff --git a/packages/sdk/types/cosmwasmclient.d.ts b/packages/sdk/types/cosmwasmclient.d.ts index 88eebd4c..bd66f743 100644 --- a/packages/sdk/types/cosmwasmclient.d.ts +++ b/packages/sdk/types/cosmwasmclient.d.ts @@ -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; @@ -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; + /** + * Gets block header and meta + * + * @param height The height of the block. If undefined, the latest height is used. + */ + getBlock(height?: number): Promise; searchTx(query: SearchTxQuery): Promise; postTx(tx: Uint8Array): Promise; /** Uploads code and returns a code ID */