From f9081acd121e72bb9a479148a7f573c0e97525e0 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 15 Feb 2020 11:41:42 +0100 Subject: [PATCH] Improve BlockResponse types --- packages/sdk/src/restclient.ts | 42 ++++++++++++++++++++++-------- packages/sdk/types/restclient.d.ts | 20 +++++++------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 2126cb7c..7c09e9e4 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -22,7 +22,7 @@ interface NodeInfoResponse { readonly node_info: NodeInfo; } -interface BlockMeta { +export interface BlockMeta { readonly header: { readonly height: number; readonly time: string; @@ -33,13 +33,33 @@ interface BlockMeta { }; } -interface Block { - readonly header: { - readonly height: number; - }; +export interface BlockHeader { + readonly height: number; + readonly chain_id: string; + // TODO: add all of those + // header: { + // version: [Object], + // chain_id: 'testing', + // height: '41121', + // time: '2020-02-15T10:39:10.4696305Z', + // last_block_id: [Object], + // last_commit_hash: '9C68EDA02AEB5F6A76AA03F7F7E6834D73424A8906FE5A79B04D310C2DB5EFFC', + // data_hash: '', + // validators_hash: '4412A0B61BAC7D1EEDA531F3FBA8E90BBB3DDF6CCA85B28CA1D8300818F0E7EA', + // next_validators_hash: '4412A0B61BAC7D1EEDA531F3FBA8E90BBB3DDF6CCA85B28CA1D8300818F0E7EA', + // consensus_hash: '048091BC7DDC283F77BFBF91D73C44DA58C3DF8A9CBC867405D8B7F3DAADA22F', + // app_hash: 'DD58B3B4AB1031ECB0CED45C017B57EE2E6E22FA7F0ECA355268CC205C6A1A64', + // last_results_hash: '', + // evidence_hash: '', + // proposer_address: '3FBF50B72FE062495F150AEB78D1981E7DAEBE60' + // }, } -interface BlocksResponse { +export interface Block { + readonly header: BlockHeader; +} + +export interface BlockResponse { readonly block_meta: BlockMeta; readonly block: Block; } @@ -119,7 +139,7 @@ interface SmartQueryResponse { type RestClientResponse = | NodeInfoResponse - | BlocksResponse + | BlockResponse | AuthAccountsResponse | TxsResponse | SearchTxsResponse @@ -209,20 +229,20 @@ export class RestClient { return responseData as NodeInfoResponse; } - public async blocksLatest(): Promise { + public async blocksLatest(): Promise { const responseData = await this.get("/blocks/latest"); if (!(responseData as any).block) { throw new Error("Unexpected response data format"); } - return responseData as BlocksResponse; + return responseData as BlockResponse; } - public async blocks(height: number): Promise { + public async blocks(height: number): Promise { const responseData = await this.get(`/blocks/${height}`); if (!(responseData as any).block) { throw new Error("Unexpected response data format"); } - return responseData as BlocksResponse; + return responseData as BlockResponse; } /** returns the amino-encoding of the transaction performed by the server */ diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index 8b82fb29..28af8a9d 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -5,7 +5,7 @@ interface NodeInfo { interface NodeInfoResponse { readonly node_info: NodeInfo; } -interface BlockMeta { +export interface BlockMeta { readonly header: { readonly height: number; readonly time: string; @@ -15,12 +15,14 @@ interface BlockMeta { readonly hash: string; }; } -interface Block { - readonly header: { - readonly height: number; - }; +export interface BlockHeader { + readonly height: number; + readonly chain_id: string; } -interface BlocksResponse { +export interface Block { + readonly header: BlockHeader; +} +export interface BlockResponse { readonly block_meta: BlockMeta; readonly block: Block; } @@ -79,7 +81,7 @@ interface GetCodeResult { } declare type RestClientResponse = | NodeInfoResponse - | BlocksResponse + | BlockResponse | AuthAccountsResponse | TxsResponse | SearchTxsResponse @@ -95,8 +97,8 @@ export declare class RestClient { get(path: string): Promise; post(path: string, params: PostTxsParams): Promise; nodeInfo(): Promise; - blocksLatest(): Promise; - blocks(height: number): Promise; + blocksLatest(): Promise; + blocks(height: number): Promise; /** returns the amino-encoding of the transaction performed by the server */ encodeTx(tx: CosmosSdkTx): Promise; authAccounts(address: string): Promise;