From 90ac9f1387e380ff43ab181bb714b580cbd1162c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 5 Feb 2020 17:14:08 +0100 Subject: [PATCH] All contract queries except smart --- packages/sdk/src/restclient.spec.ts | 46 ++++++++++++++++++++++++----- packages/sdk/src/restclient.ts | 4 +-- packages/sdk/types/restclient.d.ts | 4 +-- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index dd34e7dc..47049f46 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -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(); }); }); }); diff --git a/packages/sdk/src/restclient.ts b/packages/sdk/src/restclient.ts index a2a71e27..22513647 100644 --- a/packages/sdk/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -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 { + public async queryContractRaw(address: string, key: Uint8Array): Promise { 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 { + public async queryContractSmart(address: string, query: object): Promise { const encoded = toHex(toUtf8(JSON.stringify(query))); const path = `/wasm/contract/${address}/smart/${encoded}?encoding=hex`; const responseData = (await this.get(path)) as WasmResponse; diff --git a/packages/sdk/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts index e9feca7c..d0c8f9d3 100644 --- a/packages/sdk/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -97,7 +97,7 @@ export declare class RestClient { listContractAddresses(): Promise; getContractInfo(address: string): Promise; getAllContractState(address: string): Promise; - getContractKey(address: string, key: Uint8Array): Promise; - queryContract(address: string, query: object): Promise; + queryContractRaw(address: string, key: Uint8Array): Promise; + queryContractSmart(address: string, query: object): Promise; } export {};