Add CosmWasmClient.queryContractSmart

This commit is contained in:
Simon Warta 2020-02-13 23:06:37 +01:00
parent 62d6bd7bd9
commit 91a3144e15
3 changed files with 77 additions and 1 deletions

View File

@ -10,7 +10,7 @@ import cosmoshub from "./testdata/cosmoshub.json";
import { getRandomizedHackatom, makeRandomAddress } from "./testutils.spec";
import { Coin, CosmosSdkTx, MsgSend, StdFee } from "./types";
const { fromUtf8, toAscii } = Encoding;
const { fromAscii, fromUtf8, toAscii } = Encoding;
const httpUrl = "http://localhost:1317";
@ -423,4 +423,51 @@ describe("CosmWasmClient", () => {
);
});
});
describe("queryContractSmart", () => {
let contract: HackatomInstance | undefined;
beforeAll(async () => {
if (cosmosEnabled()) {
pendingWithoutCosmos();
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
const client = CosmWasmClient.makeWritable(httpUrl, faucet.address, signBytes => pen.sign(signBytes));
const codeId = await client.upload(getRandomizedHackatom());
const initMsg = { verifier: makeRandomAddress(), beneficiary: makeRandomAddress() };
const contractAddress = await client.instantiate(codeId, initMsg);
contract = { initMsg: initMsg, address: contractAddress };
}
});
it("works", async () => {
pendingWithoutCosmos();
assert(contract);
const client = CosmWasmClient.makeReadOnly(httpUrl);
const verifier = await client.queryContractSmart(contract.address, { verifier: {} });
expect(fromAscii(verifier)).toEqual(contract.initMsg.verifier);
});
it("errors for malformed query message", async () => {
pendingWithoutCosmos();
assert(contract);
const client = CosmWasmClient.makeReadOnly(httpUrl);
await client.queryContractSmart(contract.address, { broken: {} }).then(
() => fail("must not succeed"),
error => expect(error).toMatch(/Error parsing QueryMsg/i),
);
});
it("errors for non-existent contract", async () => {
pendingWithoutCosmos();
const nonExistentAddress = makeRandomAddress();
const client = CosmWasmClient.makeReadOnly(httpUrl);
await client.queryContractSmart(nonExistentAddress, { verifier: {} }).then(
() => fail("must not succeed"),
error => expect(error).toMatch(`No contract found at address "${nonExistentAddress}"`),
);
});
});
});

View File

@ -307,4 +307,26 @@ export class CosmWasmClient {
return this.restClient.queryContractRaw(address, key);
}
/**
* Makes a "smart query" on the contract, returns raw data
*
* Promise is rejected when contract does not exist.
* Promise is rejected for invalid query format.
*/
public async queryContractSmart(address: string, queryMsg: object): Promise<Uint8Array> {
try {
return await this.restClient.queryContractSmart(address, queryMsg);
} catch (error) {
if (error instanceof Error) {
if (error.message === "not found: contract") {
throw new Error(`No contract found at address "${address}"`);
} else {
throw error;
}
} else {
throw error;
}
}
}
}

View File

@ -69,4 +69,11 @@ export declare class CosmWasmClient {
* Promise is rejected when contract does not exist.
*/
queryContractRaw(address: string, key: Uint8Array): Promise<Uint8Array | null>;
/**
* Makes a "smart query" on the contract, returns raw data
*
* Promise is rejected when contract does not exist.
* Promise is rejected for invalid query format.
*/
queryContractSmart(address: string, queryMsg: object): Promise<Uint8Array>;
}