Add some basic wasm queries (manually tested with curl)

This commit is contained in:
Ethan Frey 2020-02-05 11:23:18 +01:00
parent ae8acefc81
commit e0b3cb006c
4 changed files with 109 additions and 4 deletions

View File

@ -1,7 +1,7 @@
import { Encoding } from "@iov/encoding";
import axios, { AxiosInstance } from "axios";
import { AminoTx, BaseAccount, isAminoStdTx, StdTx } from "./types";
import { AminoTx, BaseAccount, CodeInfo, CodeInfoWithId, ContractInfo, isAminoStdTx, StdTx } from "./types";
const { fromUtf8 } = Encoding;
@ -41,6 +41,34 @@ interface AuthAccountsResponse {
};
}
// Currently all wasm query responses return json-encoded strings...
// later deprecate this and use the specific types below
interface WasmResponse {
readonly result: string;
}
interface ListCodeResponse {
// TODO: this is returning json.encoded string now, parse on client or server?
readonly result: readonly CodeInfoWithId[];
}
interface GetCodeResponse {
// TODO: this is returning json.encoded string now, parse on client or server?
readonly result: CodeInfo;
}
interface ListContractResponse {
// TODO: this is returning json.encoded string now, parse on client or server?
// contains list of all contract addresses
readonly result: readonly string[];
}
interface GetContractResponse {
// TODO: this is returning json.encoded string now, parse on client or server?
// contains list of all contract addresses
readonly result: ContractInfo;
}
export interface TxsResponse {
readonly height: string;
readonly txhash: string;
@ -84,7 +112,8 @@ type RestClientResponse =
| TxsResponse
| SearchTxsResponse
| PostTxsResponse
| EncodeTxResponse;
| EncodeTxResponse
| WasmResponse;
type BroadcastMode = "block" | "sync" | "async";
@ -198,4 +227,33 @@ export class RestClient {
}
return responseData as PostTxsResponse;
}
// 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<readonly CodeInfoWithId[]> {
const path = `/wasm/code`;
const responseData = await this.get(path);
const codes = JSON.parse((responseData as WasmResponse).result);
return codes as CodeInfoWithId[];
}
public async getCodeInfo(id: number): Promise<CodeInfo> {
const path = `/wasm/code/${id}`;
const responseData = await this.get(path);
const code = JSON.parse((responseData as WasmResponse).result);
return code as CodeInfo;
}
public async listContractAddresses(): Promise<readonly string[]> {
const path = `/wasm/contract`;
const responseData = await this.get(path);
const codes = JSON.parse((responseData as WasmResponse).result);
return codes as string[];
}
public async getContractInfo(address: string): Promise<ContractInfo> {
const path = `/wasm/contract/${address}`;
const responseData = await this.get(path);
const code = JSON.parse((responseData as WasmResponse).result);
return code as ContractInfo;
}
}

View File

@ -162,3 +162,24 @@ export interface BaseAccount {
/** The data we need from BaseAccount to create a nonce */
export type NonceInfo = Pick<BaseAccount, "account_number" | "sequence">;
export interface CodeInfo {
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly code_hash: string;
readonly source?: string;
readonly builder?: string;
}
export interface CodeInfoWithId extends CodeInfo {
readonly id: number;
}
export interface ContractInfo {
readonly code_id: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly init_msg: object;
}

View File

@ -1,4 +1,4 @@
import { AminoTx, BaseAccount, StdTx } from "./types";
import { AminoTx, BaseAccount, CodeInfo, CodeInfoWithId, ContractInfo, StdTx } from "./types";
interface NodeInfo {
readonly network: string;
}
@ -29,6 +29,9 @@ interface AuthAccountsResponse {
readonly value: BaseAccount;
};
}
interface WasmResponse {
readonly result: string;
}
export interface TxsResponse {
readonly height: string;
readonly txhash: string;
@ -66,7 +69,8 @@ declare type RestClientResponse =
| TxsResponse
| SearchTxsResponse
| PostTxsResponse
| EncodeTxResponse;
| EncodeTxResponse
| WasmResponse;
declare type BroadcastMode = "block" | "sync" | "async";
export declare class RestClient {
private readonly client;
@ -83,5 +87,9 @@ export declare class RestClient {
txs(query: string): Promise<SearchTxsResponse>;
txsById(id: string): Promise<TxsResponse>;
postTx(tx: Uint8Array): Promise<PostTxsResponse>;
listCodeInfo(): Promise<readonly CodeInfoWithId[]>;
getCodeInfo(id: number): Promise<CodeInfo>;
listContractAddresses(): Promise<readonly string[]>;
getContractInfo(address: string): Promise<ContractInfo>;
}
export {};

View File

@ -120,4 +120,22 @@ export interface BaseAccount {
}
/** The data we need from BaseAccount to create a nonce */
export declare type NonceInfo = Pick<BaseAccount, "account_number" | "sequence">;
export interface CodeInfo {
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly code_hash: string;
readonly source?: string;
readonly builder?: string;
}
export interface CodeInfoWithId extends CodeInfo {
readonly id: number;
}
export interface ContractInfo {
readonly code_id: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly init_msg: object;
}
export {};