Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new WasmQuery variant for ContractInfo #1089

Merged
merged 5 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions contracts/reflect/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion packages/std/examples/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<Empty>), &out_dir, "QueryRequest");
}
187 changes: 187 additions & 0 deletions packages/std/schema/query_request.json
Original file line number Diff line number Diff line change
@@ -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<u8> 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<u8>",
"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
}
]
}
}
}
3 changes: 2 additions & 1 deletion packages/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
1 change: 1 addition & 0 deletions packages/std/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
2 changes: 1 addition & 1 deletion packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/query/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
ethanfrey marked this conversation as resolved.
Show resolved Hide resolved
pub struct ContractInfoResponse {
ethanfrey marked this conversation as resolved.
Show resolved Hide resolved
pub code_id: u64,
/// address that instantiated this contract
pub creator: String,
/// admin who can run migrations (if any)
pub admin: Option<String>,
/// 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<String>,
}