WIP: update return types for queries

This commit is contained in:
Ethan Frey 2020-02-10 20:29:51 +01:00
parent b5e600f5b5
commit 6f35fad87c
5 changed files with 15 additions and 15 deletions

View File

@ -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(

View File

@ -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<readonly WasmData[]> {
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<WasmData[]>;
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<unknown | null> {
public async queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null> {
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<WasmData[]>;
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<string> {
public async queryContractSmart(address: string, query: object): Promise<Uint8Array> {
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);
}
}

View File

@ -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;
}

View File

@ -103,7 +103,7 @@ export declare class RestClient {
listContractsByCodeId(id: number): Promise<readonly ContractInfo[]>;
getContractInfo(address: string): Promise<ContractInfo>;
getAllContractState(address: string): Promise<readonly WasmData[]>;
queryContractRaw(address: string, key: Uint8Array): Promise<unknown | null>;
queryContractSmart(address: string, query: object): Promise<string>;
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
queryContractSmart(address: string, query: object): Promise<Uint8Array>;
}
export {};

View File

@ -141,6 +141,6 @@ export interface ContractInfo {
}
export interface WasmData {
readonly key: string;
readonly val: unknown;
readonly val: string;
}
export {};