Update interfaces and test contract to 0.8

This commit is contained in:
Simon Warta 2020-05-20 14:59:30 +02:00
parent 7218afde51
commit 82b23c87b5
9 changed files with 44 additions and 24 deletions

View File

@ -23,7 +23,7 @@ import {
} from "./testutils.spec";
import { MsgSend, StdFee } from "./types";
const { fromAscii, fromHex, fromUtf8, toAscii, toBase64 } = Encoding;
const { fromHex, fromUtf8, toAscii, toBase64 } = Encoding;
const guest = {
address: "cosmos17d0jcz59jf68g52vq38tuuncmwwjk42u6mcxej",
@ -438,8 +438,8 @@ describe("CosmWasmClient", () => {
assert(contract);
const client = new CosmWasmClient(wasmd.endpoint);
const verifier = await client.queryContractSmart(contract.address, { verifier: {} });
expect(fromAscii(verifier)).toEqual(contract.initMsg.verifier);
const resultDocument = await client.queryContractSmart(contract.address, { verifier: {} });
expect(resultDocument).toEqual({ verifier: contract.initMsg.verifier });
});
it("errors for malformed query message", async () => {
@ -449,7 +449,7 @@ describe("CosmWasmClient", () => {
const client = new CosmWasmClient(wasmd.endpoint);
await client.queryContractSmart(contract.address, { broken: {} }).then(
() => fail("must not succeed"),
(error) => expect(error).toMatch(/Error parsing QueryMsg/i),
(error) => expect(error).toMatch(/query wasm contract failed: parsing hackatom::contract::QueryMsg/i),
);
});

View File

@ -4,7 +4,7 @@ import { Encoding } from "@iov/encoding";
import { Log, parseLogs } from "./logs";
import { decodeBech32Pubkey } from "./pubkey";
import { BroadcastMode, RestClient } from "./restclient";
import { Coin, CosmosSdkTx, PubKey, StdTx } from "./types";
import { Coin, CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
@ -391,12 +391,13 @@ export class CosmWasmClient {
}
/**
* Makes a "smart query" on the contract, returns raw data
* Makes a smart query on the contract, returns the parsed JSON document.
*
* Promise is rejected when contract does not exist.
* Promise is rejected for invalid query format.
* Promise is rejected for invalid response format.
*/
public async queryContractSmart(address: string, queryMsg: object): Promise<Uint8Array> {
public async queryContractSmart(address: string, queryMsg: object): Promise<JsonObject> {
try {
return await this.restClient.queryContractSmart(address, queryMsg);
} catch (error) {

View File

@ -1357,13 +1357,14 @@ describe("RestClient", () => {
pendingWithoutWasmd();
// we can query the verifier properly
const verifier = await client.queryContractSmart(contractAddress!, { verifier: {} });
expect(fromAscii(verifier)).toEqual(faucet.address);
const resultDocument = await client.queryContractSmart(contractAddress!, { verifier: {} });
expect(resultDocument).toEqual({ verifier: faucet.address });
// invalid query syntax throws an error
await client.queryContractSmart(contractAddress!, { nosuchkey: {} }).then(
() => fail("shouldn't succeed"),
(error) => expect(error).toMatch("Error parsing QueryMsg"),
(error) =>
expect(error).toMatch(/query wasm contract failed: parsing hackatom::contract::QueryMsg/),
);
// invalid address throws an error

View File

@ -1,9 +1,9 @@
import { Encoding, isNonNullObject } from "@iov/encoding";
import axios, { AxiosError, AxiosInstance } from "axios";
import { Coin, CosmosSdkTx, Model, parseWasmData, StdTx, WasmData } from "./types";
import { Coin, CosmosSdkTx, JsonObject, Model, parseWasmData, StdTx, WasmData } from "./types";
const { fromBase64, toHex, toUtf8 } = Encoding;
const { fromBase64, fromUtf8, toHex, toUtf8 } = Encoding;
export interface CosmosSdkAccount {
/** Bech32 account address */
@ -441,14 +441,16 @@ export class RestClient {
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<Uint8Array> {
/**
* Makes a smart query on the contract and parses the reponse as JSON.
* Throws error if no such contract exists, the query format is invalid or the response is invalid.
*/
public async queryContractSmart(address: string, query: object): Promise<JsonObject> {
const encoded = toHex(toUtf8(JSON.stringify(query)));
const path = `/wasm/contract/${address}/smart/${encoded}?encoding=hex`;
const responseData = (await this.get(path)) as WasmResponse<SmartQueryResponse>;
const result = unwrapWasmResponse(responseData);
// no extra parse here for now, see https://github.com/confio/cosmwasm/issues/144
return fromBase64(result.smart);
// By convention, smart queries must return a valid JSON document (see https://github.com/CosmWasm/cosmwasm/issues/144)
return JSON.parse(fromUtf8(fromBase64(result.smart)));
}
}

File diff suppressed because one or more lines are too long

View File

@ -169,3 +169,9 @@ export function parseWasmData({ key, val }: WasmData): Model {
val: fromBase64(val),
};
}
/**
* An object containing a parsed JSON document. The result of JSON.parse().
* This doen't privide any type safety over `any` but expresses intent in the code.
*/
export type JsonObject = any;

View File

@ -1,6 +1,6 @@
import { Log } from "./logs";
import { BroadcastMode, RestClient } from "./restclient";
import { Coin, CosmosSdkTx, PubKey, StdTx } from "./types";
import { Coin, CosmosSdkTx, JsonObject, PubKey, StdTx } from "./types";
export interface GetNonceResult {
readonly accountNumber: number;
readonly sequence: number;
@ -163,11 +163,12 @@ export declare class CosmWasmClient {
*/
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
/**
* Makes a "smart query" on the contract, returns raw data
* Makes a smart query on the contract, returns the parsed JSON document.
*
* Promise is rejected when contract does not exist.
* Promise is rejected for invalid query format.
* Promise is rejected for invalid response format.
*/
queryContractSmart(address: string, queryMsg: object): Promise<Uint8Array>;
queryContractSmart(address: string, queryMsg: object): Promise<JsonObject>;
private txsQuery;
}

View File

@ -1,4 +1,4 @@
import { Coin, CosmosSdkTx, Model, StdTx } from "./types";
import { Coin, CosmosSdkTx, JsonObject, Model, StdTx } from "./types";
export interface CosmosSdkAccount {
/** Bech32 account address */
readonly address: string;
@ -221,6 +221,10 @@ export declare class RestClient {
getContractInfo(address: string): Promise<ContractDetails | null>;
getAllContractState(address: string): Promise<readonly Model[]>;
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
queryContractSmart(address: string, query: object): Promise<Uint8Array>;
/**
* Makes a smart query on the contract and parses the reponse as JSON.
* Throws error if no such contract exists, the query format is invalid or the response is invalid.
*/
queryContractSmart(address: string, query: object): Promise<JsonObject>;
}
export {};

View File

@ -118,4 +118,9 @@ export interface Model {
readonly val: Uint8Array;
}
export declare function parseWasmData({ key, val }: WasmData): Model;
/**
* An object containing a parsed JSON document. The result of JSON.parse().
* This doen't privide any type safety over `any` but expresses intent in the code.
*/
export declare type JsonObject = any;
export {};