diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index b470ee0dde..d4dcab0c61 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -468,7 +468,7 @@ describe("RestClient", () => { .catch(error => expect(error).toMatch(`No contract with address ${beneficiaryAddress}`)); }); - fdescribe("contract state", () => { + describe("contract state", () => { const client = new RestClient(httpUrl); const noContract = makeRandomAddress(); const expectedKey = toAscii("config"); @@ -496,9 +496,8 @@ describe("RestClient", () => { const state = await client.getAllContractState(contractAddress!); expect(state.length).toEqual(1); const data = state[0]; - expect(data.key.toLowerCase()).toEqual(toHex(expectedKey)); - const value = JSON.parse(fromAscii(fromBase64(data.val))); - console.log(value); + expect(data.key).toEqual(expectedKey); + const value = JSON.parse(fromAscii(data.val)); expect(value.verifier).toBeDefined(); expect(value.beneficiary).toBeDefined(); diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 3de93e49e6..14857bdb17 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -1,7 +1,17 @@ import { Encoding } from "@iov/encoding"; import axios, { AxiosError, AxiosInstance } from "axios"; -import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, isAminoStdTx, StdTx, WasmData } from "./types"; +import { + AminoTx, + CodeInfo, + ContractInfo, + CosmosSdkAccount, + isAminoStdTx, + Model, + parseWasmData, + StdTx, + WasmData, +} from "./types"; const { fromBase64, fromUtf8, toHex, toUtf8 } = Encoding; @@ -311,15 +321,11 @@ export class RestClient { // Returns all contract state. // This is an empty array if no such contract, or contract has no data. - public async getAllContractState(address: string): Promise { + public async getAllContractState(address: string): Promise { const path = `/wasm/contract/${address}/state`; const responseData = (await this.get(path)) as WasmResponse; - console.log("all state"); - console.log(responseData); - console.log("***"); const r = unwrapWasmResponse(responseData); - console.log(r); - return r || []; + return r ? r.map(parseWasmData) : []; } // Returns the data at the key if present (unknown decoded json), @@ -340,7 +346,6 @@ export class RestClient { const responseData = (await this.get(path)) as WasmResponse; const result = unwrapWasmResponse(responseData); // no extra parse here for now, see https://github.com/confio/cosmwasm/issues/144 - console.log(result); return fromBase64(result.smart); } } diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 1d1c5cb24d..c7fcfe0fbf 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -1,3 +1,7 @@ +import { Encoding } from "@iov/encoding"; + +const { fromBase64, fromHex } = Encoding; + // We will move all needed *interfaces* from amino-js here // This means bcp can just import them from here (if needed at all) export interface Tx { @@ -193,3 +197,16 @@ export interface WasmData { // value is base64 encoded readonly val: string; } + +// Model is a parsed WasmData object +export interface Model { + readonly key: Uint8Array; + readonly val: Uint8Array; +} + +export function parseWasmData({ key, val }: WasmData): Model { + return { + key: fromHex(key), + val: fromBase64(val), + }; +} diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index eb9c7522bd..b83f731a0c 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -1,4 +1,4 @@ -import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, StdTx, WasmData } from "./types"; +import { AminoTx, CodeInfo, ContractInfo, CosmosSdkAccount, Model, StdTx } from "./types"; interface NodeInfo { readonly network: string; } @@ -102,7 +102,7 @@ export declare class RestClient { listContractAddresses(): Promise; listContractsByCodeId(id: number): Promise; getContractInfo(address: string): Promise; - getAllContractState(address: string): Promise; + getAllContractState(address: string): Promise; queryContractRaw(address: string, key: Uint8Array): Promise; queryContractSmart(address: string, query: object): Promise; } diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 791758cf81..9d61f3986e 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -143,4 +143,9 @@ export interface WasmData { readonly key: string; readonly val: string; } +export interface Model { + readonly key: Uint8Array; + readonly val: Uint8Array; +} +export declare function parseWasmData({ key, val }: WasmData): Model; export {};