All contract queries except smart

This commit is contained in:
Ethan Frey 2020-02-05 17:14:08 +01:00
parent 620767b5c6
commit 90ac9f1387
3 changed files with 42 additions and 12 deletions

View File

@ -22,7 +22,7 @@ import {
StdTx,
} from "./types";
const { fromBase64, toBase64 } = Encoding;
const { fromBase64, fromHex, toAscii, toBase64, toHex } = Encoding;
const httpUrl = "http://localhost:1317";
const defaultNetworkId = "testing";
@ -380,7 +380,7 @@ describe("RestClient", () => {
// TODO: download code and check against auto-gen
});
it("can list contracts and query details", async () => {
it("can list contracts and get info", async () => {
pendingWithoutCosmos();
const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic);
const signer = await wallet.createIdentity("abc" as ChainId, faucetPath);
@ -401,8 +401,8 @@ describe("RestClient", () => {
} else {
const uploaded = await uploadContract(client, wallet, signer);
expect(uploaded.code).toBeFalsy();
const logs = parseSuccess(uploaded.raw_log);
const codeIdAttr = findAttribute(logs, "message", "code_id");
const uploadLogs = parseSuccess(uploaded.raw_log);
const codeIdAttr = findAttribute(uploadLogs, "message", "code_id");
codeId = Number.parseInt(codeIdAttr.value, 10);
}
@ -418,7 +418,6 @@ describe("RestClient", () => {
transferAmount,
);
expect(result.code).toBeFalsy();
// console.log("Raw log:", result.raw_log);
const logs = parseSuccess(result.raw_log);
const contractAddressAttr = findAttribute(logs, "message", "contract_address");
const myAddress = contractAddressAttr.value;
@ -443,11 +442,42 @@ describe("RestClient", () => {
.getContractInfo(beneficiaryAddress)
.then(() => fail("this shouldn't succeed"))
.catch(() => {});
});
// TODO: check code hash matches expectation
// expect(lastInfo.code_hash).toEqual(faucetAddress);
it("can list query contract state", async () => {
pendingWithoutCosmos();
const client = new RestClient(httpUrl);
const noContract = makeRandomAddress();
// TODO: download code and check against auto-gen
// find an existing contract (created above)
// we assume all contracts on this chain are the same (created by these tests)
const contractInfos = await client.listContractAddresses();
expect(contractInfos.length).toBeGreaterThan(0);
const contractAddress = contractInfos[0];
// get contract state
const expectedKey = toAscii("config");
const state = await client.getAllContractState(contractAddress);
expect(state.length).toEqual(1);
const data = state[0];
expect(data.key.toLowerCase()).toEqual(toHex(expectedKey));
// bad address is empty array
const noContractState = await client.getAllContractState(noContract);
expect(noContractState).toEqual([]);
// query by one key
const model = await client.queryContractRaw(contractAddress, expectedKey);
expect(model).not.toBeNull();
expect(model).toEqual(data.val);
// missing key is null
const missing = await client.queryContractRaw(contractAddress, fromHex("cafe0dad"));
expect(missing).toBeNull();
// bad address is null
const noContractModel = await client.queryContractRaw(noContract, expectedKey);
expect(noContractModel).toBeNull();
});
});
});

View File

@ -271,7 +271,7 @@ export class RestClient {
// Returns the data at the key if present (unknown decoded json),
// or null if no data at this (contract address, key) pair
public async getContractKey(address: string, key: Uint8Array): Promise<unknown | null> {
public async queryContractRaw(address: string, key: Uint8Array): Promise<unknown | null> {
const hexKey = toHex(key);
const path = `/wasm/contract/${address}/raw/${hexKey}?encoding=hex`;
const responseData = await this.get(path);
@ -281,7 +281,7 @@ export class RestClient {
// Makes a "smart query" on the contract, returns response verbatim (json.RawMessage)
// Throws error if no such contract or invalid query format
public async queryContract(address: string, query: object): Promise<unknown> {
public async queryContractSmart(address: string, query: object): Promise<unknown> {
const encoded = toHex(toUtf8(JSON.stringify(query)));
const path = `/wasm/contract/${address}/smart/${encoded}?encoding=hex`;
const responseData = (await this.get(path)) as WasmResponse;

View File

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