diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index 2e56a4a4..9782f198 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -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 { + 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 { + 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 { + 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 { + const path = `/wasm/contract/${address}`; + const responseData = await this.get(path); + const code = JSON.parse((responseData as WasmResponse).result); + return code as ContractInfo; + } } diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts index 78f21208..62fc2b48 100644 --- a/packages/sdk/src/types.ts +++ b/packages/sdk/src/types.ts @@ -162,3 +162,24 @@ export interface BaseAccount { /** The data we need from BaseAccount to create a nonce */ export type NonceInfo = Pick; + +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; +} diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index e235d842..59934755 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -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; txsById(id: string): Promise; postTx(tx: Uint8Array): Promise; + listCodeInfo(): Promise; + getCodeInfo(id: number): Promise; + listContractAddresses(): Promise; + getContractInfo(address: string): Promise; } export {}; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts index 83f5ace6..fd4e8605 100644 --- a/packages/sdk/types/types.d.ts +++ b/packages/sdk/types/types.d.ts @@ -120,4 +120,22 @@ export interface BaseAccount { } /** The data we need from BaseAccount to create a nonce */ export declare type NonceInfo = Pick; +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 {};