From 479768beb303f0efcbc951cba4f4fa3a9be8dec7 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 9 Dec 2020 12:38:55 +0000 Subject: [PATCH] cosmwasm-stargate: Add wasm queries --- .../cosmwasm-stargate/src/queries/index.ts | 1 + .../cosmwasm-stargate/src/queries/wasm.ts | 125 ++++++++++++++++++ .../types/queries/index.d.ts | 1 + .../cosmwasm-stargate/types/queries/wasm.d.ts | 64 +++++++++ 4 files changed, 191 insertions(+) create mode 100644 packages/cosmwasm-stargate/src/queries/index.ts create mode 100644 packages/cosmwasm-stargate/src/queries/wasm.ts create mode 100644 packages/cosmwasm-stargate/types/queries/index.d.ts create mode 100644 packages/cosmwasm-stargate/types/queries/wasm.d.ts diff --git a/packages/cosmwasm-stargate/src/queries/index.ts b/packages/cosmwasm-stargate/src/queries/index.ts new file mode 100644 index 00000000..c0692523 --- /dev/null +++ b/packages/cosmwasm-stargate/src/queries/index.ts @@ -0,0 +1 @@ +export { setupWasmExtension, WasmExtension } from "./wasm"; diff --git a/packages/cosmwasm-stargate/src/queries/wasm.ts b/packages/cosmwasm-stargate/src/queries/wasm.ts new file mode 100644 index 00000000..40dc6728 --- /dev/null +++ b/packages/cosmwasm-stargate/src/queries/wasm.ts @@ -0,0 +1,125 @@ +/* eslint-disable @typescript-eslint/naming-convention */ +import { toAscii } from "@cosmjs/encoding"; +import { QueryClient } from "@cosmjs/stargate"; +import Long from "long"; + +import { cosmwasm } from "../codec"; + +type IQueryAllContractStateResponse = cosmwasm.wasm.v1beta1.IQueryAllContractStateResponse; +type IQueryCodesResponse = cosmwasm.wasm.v1beta1.IQueryCodesResponse; +type IQueryCodeResponse = cosmwasm.wasm.v1beta1.IQueryCodeResponse; +type IQueryContractHistoryResponse = cosmwasm.wasm.v1beta1.IQueryContractHistoryResponse; +type IQueryContractInfoResponse = cosmwasm.wasm.v1beta1.IQueryContractInfoResponse; +type IQueryContractsByCodeResponse = cosmwasm.wasm.v1beta1.IQueryContractsByCodeResponse; +type IQueryRawContractStateResponse = cosmwasm.wasm.v1beta1.IQueryRawContractStateResponse; +type IQuerySmartContractStateResponse = cosmwasm.wasm.v1beta1.IQuerySmartContractStateResponse; + +const { Query } = cosmwasm.wasm.v1beta1; + +export interface WasmExtension { + readonly unverified: { + readonly wasm: { + readonly listCodeInfo: (paginationKey?: Uint8Array) => Promise; + /** + * Downloads the original wasm bytecode by code ID. + * + * Throws an error if no code with this id + */ + readonly getCode: (id: number) => Promise; + readonly listContractsByCodeId: ( + id: number, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns null when contract was not found at this address. + */ + readonly getContractInfo: (address: string) => Promise; + /** + * Returns null when contract history was not found for this address. + */ + readonly getContractCodeHistory: ( + address: string, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns all contract state. + * This is an empty array if no such contract, or contract has no data. + */ + readonly getAllContractState: ( + address: string, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns the data at the key if present (unknown decoded json), + * or null if no data at this (contract address, key) pair + */ + readonly queryContractRaw: ( + address: string, + key: Uint8Array, + ) => Promise; + /** + * Makes a smart query on the contract and parses the response as JSON. + * Throws error if no such contract exists, the query format is invalid or the response is invalid. + */ + readonly queryContractSmart: ( + address: string, + query: Record, + ) => Promise; + }; + }; +} + +export function setupWasmExtension(base: QueryClient): WasmExtension { + const queryService = Query.create((method: any, requestData, callback) => { + const path = `/cosmwasm.wasm.v1beta1.Query/${method.name}`; + base + .queryUnverified(path, requestData) + .then((response) => callback(null, response)) + .catch((error) => callback(error)); + }); + return { + unverified: { + wasm: { + listCodeInfo: async (paginationKey?: Uint8Array) => { + const request = paginationKey ? { pagination: { key: paginationKey } } : {}; + return queryService.codes(request); + }, + getCode: async (id: number) => { + const request = { codeId: Long.fromNumber(id) }; + return queryService.code(request); + }, + listContractsByCodeId: async (id: number, paginationKey?: Uint8Array) => { + const pagination = paginationKey ? { pagination: { key: paginationKey } } : {}; + const request = { ...pagination, codeId: Long.fromNumber(id) }; + return queryService.contractsByCode(request); + }, + getContractInfo: async (address: string) => { + const request = { address: address }; + return queryService.contractInfo(request); + }, + + getContractCodeHistory: async (address: string, paginationKey?: Uint8Array) => { + const pagination = paginationKey ? { pagination: { key: paginationKey } } : {}; + const request = { ...pagination, address: address }; + return queryService.contractHistory(request); + }, + + getAllContractState: async (address: string, paginationKey?: Uint8Array) => { + const pagination = paginationKey ? { pagination: { key: paginationKey } } : {}; + const request = { ...pagination, address: address }; + return queryService.allContractState(request); + }, + + queryContractRaw: async (address: string, key: Uint8Array) => { + const request = { address: address, queryData: key }; + return queryService.rawContractState(request); + }, + + queryContractSmart: async (address: string, query: Record) => { + const request = { address: address, queryData: toAscii(JSON.stringify(query)) }; + return queryService.smartContractState(request); + }, + }, + }, + }; +} diff --git a/packages/cosmwasm-stargate/types/queries/index.d.ts b/packages/cosmwasm-stargate/types/queries/index.d.ts new file mode 100644 index 00000000..c0692523 --- /dev/null +++ b/packages/cosmwasm-stargate/types/queries/index.d.ts @@ -0,0 +1 @@ +export { setupWasmExtension, WasmExtension } from "./wasm"; diff --git a/packages/cosmwasm-stargate/types/queries/wasm.d.ts b/packages/cosmwasm-stargate/types/queries/wasm.d.ts new file mode 100644 index 00000000..3b6c1249 --- /dev/null +++ b/packages/cosmwasm-stargate/types/queries/wasm.d.ts @@ -0,0 +1,64 @@ +import { QueryClient } from "@cosmjs/stargate"; +import { cosmwasm } from "../codec"; +declare type IQueryAllContractStateResponse = cosmwasm.wasm.v1beta1.IQueryAllContractStateResponse; +declare type IQueryCodesResponse = cosmwasm.wasm.v1beta1.IQueryCodesResponse; +declare type IQueryCodeResponse = cosmwasm.wasm.v1beta1.IQueryCodeResponse; +declare type IQueryContractHistoryResponse = cosmwasm.wasm.v1beta1.IQueryContractHistoryResponse; +declare type IQueryContractInfoResponse = cosmwasm.wasm.v1beta1.IQueryContractInfoResponse; +declare type IQueryContractsByCodeResponse = cosmwasm.wasm.v1beta1.IQueryContractsByCodeResponse; +declare type IQueryRawContractStateResponse = cosmwasm.wasm.v1beta1.IQueryRawContractStateResponse; +declare type IQuerySmartContractStateResponse = cosmwasm.wasm.v1beta1.IQuerySmartContractStateResponse; +export interface WasmExtension { + readonly unverified: { + readonly wasm: { + readonly listCodeInfo: (paginationKey?: Uint8Array) => Promise; + /** + * Downloads the original wasm bytecode by code ID. + * + * Throws an error if no code with this id + */ + readonly getCode: (id: number) => Promise; + readonly listContractsByCodeId: ( + id: number, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns null when contract was not found at this address. + */ + readonly getContractInfo: (address: string) => Promise; + /** + * Returns null when contract history was not found for this address. + */ + readonly getContractCodeHistory: ( + address: string, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns all contract state. + * This is an empty array if no such contract, or contract has no data. + */ + readonly getAllContractState: ( + address: string, + paginationKey?: Uint8Array, + ) => Promise; + /** + * Returns the data at the key if present (unknown decoded json), + * or null if no data at this (contract address, key) pair + */ + readonly queryContractRaw: ( + address: string, + key: Uint8Array, + ) => Promise; + /** + * Makes a smart query on the contract and parses the response as JSON. + * Throws error if no such contract exists, the query format is invalid or the response is invalid. + */ + readonly queryContractSmart: ( + address: string, + query: Record, + ) => Promise; + }; + }; +} +export declare function setupWasmExtension(base: QueryClient): WasmExtension; +export {};