From 4a74d06e6cbf623fbc6d130c0885c68677140885 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 6 Mar 2020 00:03:18 +0100 Subject: [PATCH] Create warning type CosmosSdkArray and standardize normalization --- packages/sdk/src/restclient.ts | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index f08d2b26..3a3b24c6 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -181,6 +181,13 @@ type RestClientResponse = | WasmResponse | WasmResponse; +/** Unfortunately, Cosmos SDK encodes empty arrays as null */ +type CosmosSdkArray = ReadonlyArray | null; + +function normalizeArray(backend: CosmosSdkArray): ReadonlyArray { + return backend || []; +} + /** * The mode used to send transaction * @@ -349,8 +356,8 @@ export class RestClient { // wasm rest queries are listed here: https://github.com/cosmwasm/wasmd/blob/master/x/wasm/client/rest/query.go#L19-L27 public async listCodeInfo(): Promise { const path = `/wasm/code`; - const responseData = (await this.get(path)) as WasmResponse; - return unwrapWasmResponse(responseData); + const responseData = (await this.get(path)) as WasmResponse>; + return normalizeArray(unwrapWasmResponse(responseData)); } // this will download the original wasm bytecode by code id @@ -363,8 +370,8 @@ export class RestClient { public async listContractsByCodeId(id: number): Promise { const path = `/wasm/code/${id}/contracts`; - const responseData = (await this.get(path)) as WasmResponse; - return unwrapWasmResponse(responseData) || []; + const responseData = (await this.get(path)) as WasmResponse>; + return normalizeArray(unwrapWasmResponse(responseData)); } /** @@ -380,9 +387,8 @@ export class RestClient { // This is an empty array if no such contract, or contract has no data. public async getAllContractState(address: string): Promise { const path = `/wasm/contract/${address}/state`; - const responseData = (await this.get(path)) as WasmResponse; - const r = unwrapWasmResponse(responseData); - return r ? r.map(parseWasmData) : []; + const responseData = (await this.get(path)) as WasmResponse>; + return normalizeArray(unwrapWasmResponse(responseData)).map(parseWasmData); } // Returns the data at the key if present (unknown decoded json),