Add code and contract getters to CosmWasmClient

This commit is contained in:
Simon Warta 2020-02-21 18:17:57 +01:00
parent 2502ce5d38
commit 17ded4b676
8 changed files with 127 additions and 54 deletions

View File

@ -48,6 +48,30 @@ export interface SearchTxFilter {
readonly maxHeight?: number;
}
export interface Code {
readonly id: number;
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly checksum: string;
readonly source?: string;
readonly builder?: string;
}
export interface CodeDetails {
/** The original wasm bytes */
readonly wasm: Uint8Array;
}
export interface Contract {
// TODO: add contract address (https://github.com/cosmwasm/wasmd/issues/75)
readonly codeId: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly initMsg: object;
}
export class CosmWasmClient {
protected readonly restClient: RestClient;
@ -166,6 +190,42 @@ export class CosmWasmClient {
};
}
public async getCodes(): Promise<readonly Code[]> {
const result = await this.restClient.listCodeInfo();
return result.map(r => ({
id: r.id,
creator: r.creator,
checksum: Encoding.toHex(Encoding.fromHex(r.code_hash)),
source: r.source || undefined,
builder: r.builder || undefined,
}));
}
public async getCodeDetails(codeId: number): Promise<CodeDetails> {
const result = await this.restClient.getCode(codeId);
return {
wasm: result,
};
}
public async getContracts(codeId: number): Promise<readonly Contract[]> {
const result = await this.restClient.listContractsByCodeId(codeId);
return result.map(r => ({
codeId: r.code_id,
creator: r.creator,
initMsg: r.init_msg,
}));
}
public async getContract(address: string): Promise<Contract> {
const result = await this.restClient.getContractInfo(address);
return {
codeId: result.code_id,
creator: result.creator,
initMsg: result.init_msg,
};
}
/**
* Returns the data at the key if present (raw contract dependent storage data)
* or null if no data at this key.

View File

@ -7,6 +7,9 @@ export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
Code,
CodeDetails,
Contract,
CosmWasmClient,
GetNonceResult,
PostTxResult,

View File

@ -1,16 +1,7 @@
import { Encoding } from "@iov/encoding";
import axios, { AxiosError, AxiosInstance } from "axios";
import {
CodeInfo,
ContractInfo,
CosmosSdkAccount,
CosmosSdkTx,
Model,
parseWasmData,
StdTx,
WasmData,
} from "./types";
import { CosmosSdkAccount, CosmosSdkTx, Model, parseWasmData, StdTx, WasmData } from "./types";
const { fromBase64, toHex, toUtf8 } = Encoding;
@ -131,6 +122,25 @@ interface EncodeTxResponse {
readonly tx: string;
}
export interface CodeInfo {
readonly id: number;
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly code_hash: string;
// TODO: these are not supported in current wasmd
readonly source?: string;
readonly builder?: string;
}
export interface ContractInfo {
readonly code_id: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly init_msg: object;
}
interface GetCodeResult {
// base64 encoded wasm
readonly code: string;

View File

@ -160,30 +160,6 @@ export interface CosmosSdkAccount {
readonly sequence: number;
}
export interface CodeInfo {
readonly id: number;
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly code_hash: string;
// TODO: these are not supported in current wasmd
readonly source?: string;
readonly builder?: string;
}
export interface CodeDetails {
// TODO: this should be base64 encoded string with content - not in current stack
readonly code: string;
}
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 interface WasmData {
// key is hex-encoded
readonly key: string;

View File

@ -25,6 +25,26 @@ export interface SearchTxFilter {
readonly minHeight?: number;
readonly maxHeight?: number;
}
export interface Code {
readonly id: number;
/** Bech32 account address */
readonly creator: string;
/** Hex-encoded sha256 hash of the code stored here */
readonly checksum: string;
readonly source?: string;
readonly builder?: string;
}
export interface CodeDetails {
/** The original wasm bytes */
readonly wasm: Uint8Array;
}
export interface Contract {
readonly codeId: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly initMsg: object;
}
export declare class CosmWasmClient {
protected readonly restClient: RestClient;
constructor(url: string, broadcastMode?: BroadcastMode);
@ -50,6 +70,10 @@ export declare class CosmWasmClient {
getBlock(height?: number): Promise<BlockResponse>;
searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise<readonly TxsResponse[]>;
postTx(tx: StdTx): Promise<PostTxResult>;
getCodes(): Promise<readonly Code[]>;
getCodeDetails(codeId: number): Promise<CodeDetails>;
getContracts(codeId: number): Promise<readonly Contract[]>;
getContract(address: string): Promise<Contract>;
/**
* Returns the data at the key if present (raw contract dependent storage data)
* or null if no data at this key.

View File

@ -6,6 +6,9 @@ export { unmarshalTx } from "./decoding";
export { makeSignBytes, marshalTx } from "./encoding";
export { BroadcastMode, RestClient, TxsResponse } from "./restclient";
export {
Code,
CodeDetails,
Contract,
CosmWasmClient,
GetNonceResult,
PostTxResult,

View File

@ -1,4 +1,4 @@
import { CodeInfo, ContractInfo, CosmosSdkAccount, CosmosSdkTx, Model, StdTx } from "./types";
import { CosmosSdkAccount, CosmosSdkTx, Model, StdTx } from "./types";
interface NodeInfo {
readonly network: string;
}
@ -92,6 +92,22 @@ export interface PostTxsResponse {
interface EncodeTxResponse {
readonly tx: string;
}
export interface CodeInfo {
readonly id: number;
/** 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 ContractInfo {
readonly code_id: number;
/** Bech32 account address */
readonly creator: string;
/** Argument passed on initialization of the contract */
readonly init_msg: object;
}
interface GetCodeResult {
readonly code: string;
}

View File

@ -116,25 +116,6 @@ export interface CosmosSdkAccount {
readonly account_number: number;
readonly sequence: number;
}
export interface CodeInfo {
readonly id: number;
/** 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 CodeDetails {
readonly code: string;
}
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 interface WasmData {
readonly key: string;
readonly val: string;