From a04a97d6053748625c9e438a3c011fe8ddb362a8 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 9 Sep 2021 16:52:19 +0200 Subject: [PATCH 1/5] Add new WasmQuery variant for ContractInfo --- packages/std/src/mock.rs | 1 + packages/std/src/query/wasm.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/std/src/mock.rs b/packages/std/src/mock.rs index 9ea019351f..ca1f6cfc3d 100644 --- a/packages/std/src/mock.rs +++ b/packages/std/src/mock.rs @@ -486,6 +486,7 @@ impl NoWasmQuerier { let addr = match request { WasmQuery::Smart { contract_addr, .. } => contract_addr, WasmQuery::Raw { contract_addr, .. } => contract_addr, + WasmQuery::ContractInfo { contract_addr, .. } => contract_addr, } .clone(); SystemResult::Err(SystemError::NoSuchContract { addr }) diff --git a/packages/std/src/query/wasm.rs b/packages/std/src/query/wasm.rs index e98b9d3b14..b0cf51d0ce 100644 --- a/packages/std/src/query/wasm.rs +++ b/packages/std/src/query/wasm.rs @@ -21,4 +21,21 @@ pub enum WasmQuery { /// Key is the raw key used in the contracts Storage key: Binary, }, + /// returns a ContractInfoResponse with metadata on the contract from the runtime + ContractInfo { contract_addr: String }, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] +pub struct ContractInfoResponse { + pub code_id: u64, + /// address that instantiated this contract + pub creator: String, + /// admin who can run migrations (if any) + pub admin: Option, + /// if set, the contract is pinned to the cache, and thus uses less gas when called + pub pinned: bool, + /// block height this was created at + pub created_at: u64, + /// set if this contract has bound an IBC port + pub ibc_port: Option, } From ec74447861f3a9e40d8af5cf6887b203ff8ca80a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 9 Sep 2021 16:58:12 +0200 Subject: [PATCH 2/5] Update reflect schema --- contracts/reflect/schema/query_msg.json | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/contracts/reflect/schema/query_msg.json b/contracts/reflect/schema/query_msg.json index 7084cacd97..db7679087a 100644 --- a/contracts/reflect/schema/query_msg.json +++ b/contracts/reflect/schema/query_msg.json @@ -517,6 +517,27 @@ } }, "additionalProperties": false + }, + { + "description": "returns a ContractInfoResponse with metadata on the contract from the runtime", + "type": "object", + "required": [ + "contract_info" + ], + "properties": { + "contract_info": { + "type": "object", + "required": [ + "contract_addr" + ], + "properties": { + "contract_addr": { + "type": "string" + } + } + } + }, + "additionalProperties": false } ] } From bcde74ff494fbe54c6a67fd3726d3e542e52c88c Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 10 Sep 2021 10:32:04 +0200 Subject: [PATCH 3/5] Update with PR comments --- packages/std/src/lib.rs | 3 ++- packages/std/src/query/mod.rs | 2 +- packages/std/src/query/wasm.rs | 3 +-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/std/src/lib.rs b/packages/std/src/lib.rs index 4b702f9f70..0a9e1a0308 100644 --- a/packages/std/src/lib.rs +++ b/packages/std/src/lib.rs @@ -41,7 +41,8 @@ pub use crate::ibc::{ pub use crate::iterator::{Order, Pair}; pub use crate::math::{Decimal, Decimal256, Fraction, Uint128, Uint256, Uint512, Uint64}; pub use crate::query::{ - AllBalanceResponse, BalanceResponse, BankQuery, CustomQuery, QueryRequest, WasmQuery, + AllBalanceResponse, BalanceResponse, BankQuery, ContractInfoResponse, CustomQuery, + QueryRequest, WasmQuery, }; #[cfg(feature = "staking")] pub use crate::query::{ diff --git a/packages/std/src/query/mod.rs b/packages/std/src/query/mod.rs index f1e6afff4f..fb37ab7e8a 100644 --- a/packages/std/src/query/mod.rs +++ b/packages/std/src/query/mod.rs @@ -21,7 +21,7 @@ pub use staking::{ }; #[cfg(feature = "stargate")] pub use stargate::StargateResponse; -pub use wasm::WasmQuery; +pub use wasm::{ContractInfoResponse, WasmQuery}; #[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] diff --git a/packages/std/src/query/wasm.rs b/packages/std/src/query/wasm.rs index b0cf51d0ce..435d008e45 100644 --- a/packages/std/src/query/wasm.rs +++ b/packages/std/src/query/wasm.rs @@ -25,6 +25,7 @@ pub enum WasmQuery { ContractInfo { contract_addr: String }, } +#[non_exhaustive] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] pub struct ContractInfoResponse { pub code_id: u64, @@ -34,8 +35,6 @@ pub struct ContractInfoResponse { pub admin: Option, /// if set, the contract is pinned to the cache, and thus uses less gas when called pub pinned: bool, - /// block height this was created at - pub created_at: u64, /// set if this contract has bound an IBC port pub ibc_port: Option, } From 1fff005deec6485b0e9d517f55d12bddf88009fb Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 10 Sep 2021 10:32:56 +0200 Subject: [PATCH 4/5] Add QueryRequest to cosmwasm-std schema files --- packages/std/examples/schema.rs | 3 +- packages/std/schema/query_request.json | 187 +++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 packages/std/schema/query_request.json diff --git a/packages/std/examples/schema.rs b/packages/std/examples/schema.rs index 2d251a5c83..37b9ad0423 100644 --- a/packages/std/examples/schema.rs +++ b/packages/std/examples/schema.rs @@ -2,7 +2,7 @@ use std::env::current_dir; use std::fs::create_dir_all; use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for}; -use cosmwasm_std::{CosmosMsg, Timestamp}; +use cosmwasm_std::{CosmosMsg, Empty, QueryRequest, Timestamp}; fn main() { let mut out_dir = current_dir().unwrap(); @@ -12,4 +12,5 @@ fn main() { export_schema(&schema_for!(Timestamp), &out_dir); export_schema_with_title(&schema_for!(CosmosMsg), &out_dir, "CosmosMsg"); + export_schema_with_title(&schema_for!(QueryRequest), &out_dir, "QueryRequest"); } diff --git a/packages/std/schema/query_request.json b/packages/std/schema/query_request.json new file mode 100644 index 0000000000..66293cf45b --- /dev/null +++ b/packages/std/schema/query_request.json @@ -0,0 +1,187 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryRequest", + "anyOf": [ + { + "type": "object", + "required": [ + "bank" + ], + "properties": { + "bank": { + "$ref": "#/definitions/BankQuery" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "custom" + ], + "properties": { + "custom": { + "$ref": "#/definitions/Empty" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "wasm" + ], + "properties": { + "wasm": { + "$ref": "#/definitions/WasmQuery" + } + }, + "additionalProperties": false + } + ], + "definitions": { + "BankQuery": { + "anyOf": [ + { + "description": "This calls into the native bank module for one denomination Return value is BalanceResponse", + "type": "object", + "required": [ + "balance" + ], + "properties": { + "balance": { + "type": "object", + "required": [ + "address", + "denom" + ], + "properties": { + "address": { + "type": "string" + }, + "denom": { + "type": "string" + } + } + } + }, + "additionalProperties": false + }, + { + "description": "This calls into the native bank module for all denominations. Note that this may be much more expensive than Balance and should be avoided if possible. Return value is AllBalanceResponse.", + "type": "object", + "required": [ + "all_balances" + ], + "properties": { + "all_balances": { + "type": "object", + "required": [ + "address" + ], + "properties": { + "address": { + "type": "string" + } + } + } + }, + "additionalProperties": false + } + ] + }, + "Binary": { + "description": "Binary is a wrapper around Vec to add base64 de/serialization with serde. It also adds some helper methods to help encode inline.\n\nThis is only needed as serde-json-{core,wasm} has a horrible encoding for Vec", + "type": "string" + }, + "Empty": { + "description": "An empty struct that serves as a placeholder in different places, such as contracts that don't set a custom message.\n\nIt is designed to be expressable in correct JSON and JSON Schema but contains no meaningful data. Previously we used enums without cases, but those cannot represented as valid JSON Schema (https://github.com/CosmWasm/cosmwasm/issues/451)", + "type": "object" + }, + "WasmQuery": { + "anyOf": [ + { + "description": "this queries the public API of another contract at a known address (with known ABI) return value is whatever the contract returns (caller should know)", + "type": "object", + "required": [ + "smart" + ], + "properties": { + "smart": { + "type": "object", + "required": [ + "contract_addr", + "msg" + ], + "properties": { + "contract_addr": { + "type": "string" + }, + "msg": { + "description": "msg is the json-encoded QueryMsg struct", + "allOf": [ + { + "$ref": "#/definitions/Binary" + } + ] + } + } + } + }, + "additionalProperties": false + }, + { + "description": "this queries the raw kv-store of the contract. returns the raw, unparsed data stored at that key, which may be an empty vector if not present", + "type": "object", + "required": [ + "raw" + ], + "properties": { + "raw": { + "type": "object", + "required": [ + "contract_addr", + "key" + ], + "properties": { + "contract_addr": { + "type": "string" + }, + "key": { + "description": "Key is the raw key used in the contracts Storage", + "allOf": [ + { + "$ref": "#/definitions/Binary" + } + ] + } + } + } + }, + "additionalProperties": false + }, + { + "description": "returns a ContractInfoResponse with metadata on the contract from the runtime", + "type": "object", + "required": [ + "contract_info" + ], + "properties": { + "contract_info": { + "type": "object", + "required": [ + "contract_addr" + ], + "properties": { + "contract_addr": { + "type": "string" + } + } + } + }, + "additionalProperties": false + } + ] + } + } +} From a6cf59ffd388de28a3781433b6ff8444d5072a59 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Fri, 10 Sep 2021 10:36:03 +0200 Subject: [PATCH 5/5] [skip ci] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b9ff9a81..bf5c026031 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ and this project adheres to ## [Unreleased] +### Added + +- cosmwasm-std: Add new `WasmQuery::ContractInfo` variant to get metadata + about the contract, like code_id and admin. + ### Changed - cosmwasm-std: Make `iterator` a required feature if the `iterator` feature