From 6f35fad87c411302fb9e733d3267646c134aecca Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 10 Feb 2020 20:29:51 +0100 Subject: [PATCH] WIP: update return types for queries --- packages/sdk/src/restclient.spec.ts | 4 ++-- packages/sdk/src/restclient.ts | 16 ++++++++-------- packages/sdk/src/types.ts | 4 ++-- packages/sdk/types/restclient.d.ts | 4 ++-- packages/sdk/types/types.d.ts | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 53312d5e..4f927d3b 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -23,7 +23,7 @@ import { StdTx, } from "./types"; -const { fromBase64, fromHex, toAscii, toBase64, toHex } = Encoding; +const { fromAscii, fromBase64, fromHex, toAscii, toBase64, toHex } = Encoding; const httpUrl = "http://localhost:1317"; const defaultNetworkId = "testing"; @@ -528,7 +528,7 @@ describe("RestClient", () => { // we can query the verifier properly const verifier = await client.queryContractSmart(contractAddress!, { verifier: {} }); - expect(verifier).toEqual(faucet.address); + expect(fromAscii(verifier)).toEqual(faucet.address); // invalid query syntax throws an error await client.queryContractSmart(contractAddress!, { nosuchkey: {} }).then( diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 14809dae..503f1543 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -308,23 +308,23 @@ 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); - return parseWasmResponse(responseData as WasmResponse); + const responseData = (await this.get(path)) as WasmResponse; + return unwrapWasmResponse(responseData); } // Returns the data at the key if present (unknown decoded json), // or null if no data at this (contract address, key) pair - public async queryContractRaw(address: string, key: Uint8Array): Promise { + public async queryContractRaw(address: string, key: Uint8Array): Promise { const hexKey = toHex(key); const path = `/wasm/contract/${address}/raw/${hexKey}?encoding=hex`; - const responseData = await this.get(path); - const data: readonly WasmData[] = parseWasmResponse(responseData as WasmResponse); - return data.length === 0 ? null : data[0].val; + const responseData = (await this.get(path)) as WasmResponse; + const data = unwrapWasmResponse(responseData); + return data.length === 0 ? null : fromBase64(data[0].val); } // Makes a "smart query" on the contract, returns response verbatim (json.RawMessage) // Throws error if no such contract or invalid query format - public async queryContractSmart(address: string, query: object): Promise { + public async queryContractSmart(address: string, query: object): Promise { const encoded = toHex(toUtf8(JSON.stringify(query))); const path = `/wasm/contract/${address}/smart/${encoded}?encoding=hex`; const responseData = (await this.get(path)) as WasmResponse; @@ -332,6 +332,6 @@ export class RestClient { throw new Error(responseData.error); } // no extra parse here for now, see https://github.com/confio/cosmwasm/issues/144 - return responseData.result; + return fromBase64(responseData.result); } } diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index d9ac9998..1d1c5cb2 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -190,6 +190,6 @@ export interface ContractInfo { export interface WasmData { // key is hex-encoded readonly key: string; - // value can be any decoded json, often an object but can be anything - readonly val: unknown; + // value is base64 encoded + readonly val: string; } diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index ad5f735d..eb9c7522 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -103,7 +103,7 @@ export declare class RestClient { listContractsByCodeId(id: number): Promise; getContractInfo(address: string): Promise; getAllContractState(address: string): Promise; - queryContractRaw(address: string, key: Uint8Array): Promise; - queryContractSmart(address: string, query: object): Promise; + queryContractRaw(address: string, key: Uint8Array): Promise; + queryContractSmart(address: string, query: object): Promise; } export {}; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 20b65f4d..791758cf 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -141,6 +141,6 @@ export interface ContractInfo { } export interface WasmData { readonly key: string; - readonly val: unknown; + readonly val: string; } export {};