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 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 } ] } 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 + } + ] + } + } +} 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/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/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 e98b9d3b14..435d008e45 100644 --- a/packages/std/src/query/wasm.rs +++ b/packages/std/src/query/wasm.rs @@ -21,4 +21,20 @@ 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 }, +} + +#[non_exhaustive] +#[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, + /// set if this contract has bound an IBC port + pub ibc_port: Option, }