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

BCI-1511: only contract schema changes #359

Merged
merged 5 commits into from
Aug 29, 2023
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ integration-tests/logs
integration-tests/smoke/logs
integration-tests/soak/logs
tmp-manifest-*
artifacts
/artifacts
.local-mock-server

3 changes: 2 additions & 1 deletion contracts/access-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ backtraces = ["cosmwasm-std/backtraces"]
library = []

[dependencies]
cosmwasm-schema = { version = "1.1.5" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm why the need for this? The crate's readme specifically states it's a dev dependency

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://book.cosmwasm.com/basics/good-practices.html#json-schema -- just copied over here. It also didn't work as a dev dependency as I wasn't able to use it in schema.rs

cosmwasm-std = { version = "1.1.5" }
cosmwasm-storage = { version = "1.1.5" }
cw-storage-plus = "0.16.0"
Expand All @@ -32,4 +33,4 @@ thiserror = { version = "1.0.26" }
owned = { version = "1.0", path = "../../crates/owned" }

[dev-dependencies]
cosmwasm-schema = { version = "1.1.5" }

11 changes: 6 additions & 5 deletions contracts/access-controller/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use cosmwasm_schema::write_api;

use access_controller::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}
135 changes: 135 additions & 0 deletions contracts/access-controller/schema/access-controller.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"contract_name": "access-controller",
"contract_version": "1.0.0",
"idl_version": "1.0.0",
"instantiate": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object"
},
"execute": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"oneOf": [
{
"type": "string",
"enum": [
"accept_ownership"
]
},
{
"type": "object",
"required": [
"add_access"
],
"properties": {
"add_access": {
"type": "object",
"required": [
"address"
],
"properties": {
"address": {
"type": "string"
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"remove_access"
],
"properties": {
"remove_access": {
"type": "object",
"required": [
"address"
],
"properties": {
"address": {
"type": "string"
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"transfer_ownership"
],
"properties": {
"transfer_ownership": {
"type": "object",
"required": [
"to"
],
"properties": {
"to": {
"type": "string"
}
}
}
},
"additionalProperties": false
}
]
},
"query": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"oneOf": [
{
"type": "object",
"required": [
"has_access"
],
"properties": {
"has_access": {
"type": "object",
"required": [
"address"
],
"properties": {
"address": {
"type": "string"
}
}
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"owner"
],
"properties": {
"owner": {
"type": "object"
}
},
"additionalProperties": false
}
]
},
"migrate": null,
"sudo": null,
"responses": {
"has_access": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Boolean",
"type": "boolean"
},
"owner": {
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Addr",
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
"type": "string"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "QueryMsg",
"oneOf": [
{
"type": "string",
"enum": [
"owner"
]
},
{
"type": "object",
"required": [
Expand All @@ -27,6 +21,18 @@
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"owner"
],
"properties": {
"owner": {
"type": "object"
}
},
"additionalProperties": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Boolean",
"type": "boolean"
}
6 changes: 6 additions & 0 deletions contracts/access-controller/schema/raw/response_to_owner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Addr",
"description": "A human readable address.\n\nIn Cosmos, this is typically bech32 encoded. But for multi-chain smart contracts no assumptions should be made other than being UTF-8 encoded and of reasonable length.\n\nThis type represents a validated address. It can be created in the following ways 1. Use `Addr::unchecked(input)` 2. Use `let checked: Addr = deps.api.addr_validate(input)?` 3. Use `let checked: Addr = deps.api.addr_humanize(canonical_addr)?` 4. Deserialize from JSON. This must only be done from JSON that was validated before such as a contract's state. `Addr` must not be used in messages sent by the user because this would result in unvalidated instances.\n\nThis type is immutable. If you really need to mutate it (Really? Are you sure?), create a mutable copy using `let mut mutable = Addr::to_string()` and operate on that `String` instance.",
"type": "string"
}
2 changes: 1 addition & 1 deletion contracts/access-controller/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn execute(
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::HasAccess { address } => to_binary(&query_has_access(deps, address)?),
QueryMsg::Owner => Ok(to_binary(&OWNER.query_owner(deps)?)?),
QueryMsg::Owner {} => Ok(to_binary(&OWNER.query_owner(deps)?)?),
}
}

Expand Down
8 changes: 6 additions & 2 deletions contracts/access-controller/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use cosmwasm_schema::QueryResponses;
use cosmwasm_std::Addr;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

Expand All @@ -14,9 +16,11 @@ pub enum ExecuteMsg {
AcceptOwnership,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, QueryResponses)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
#[returns(bool)]
HasAccess { address: String },
Owner,
#[returns(Addr)]
Owner {},
}
3 changes: 2 additions & 1 deletion contracts/deviation-flagging-validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ thiserror = { version = "1.0.24" }
cw-storage-plus = "0.16.0"
owned = { path = "../../crates/owned" }
flags = { path = "../flags", default-features = false, features = ["library"] }
cosmwasm-schema = { version = "1.1.5" }


[dev-dependencies]
cosmwasm-schema = { version = "1.1.5" }

34 changes: 27 additions & 7 deletions contracts/deviation-flagging-validator/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
use std::env::current_dir;
use std::fs::create_dir_all;
use std::fs::{create_dir_all, remove_dir_all, rename};

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use cosmwasm_schema::{export_schema, remove_schemas, schema_for, write_api};

use deviation_flagging_validator::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use deviation_flagging_validator::state::State;

fn main() {
// clean directory
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
remove_dir_all(&out_dir).unwrap();
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(State), &out_dir);
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}

// put main schema under main folder for codegen.js (else it will error)
let mut main_dir = out_dir.clone();
main_dir.push("main");
create_dir_all(&main_dir).unwrap();

let mut main_file = out_dir.clone();
main_file.push("deviation-flagging-validator.json");

let mut new_location = main_dir.clone();
new_location.push("deviation-flagging-validator.json");
rename(&main_file, &new_location).unwrap();

// put other hand-exported schemas in seperate folder
let mut other_dir = out_dir.clone();
other_dir.push("other");
create_dir_all(&other_dir).unwrap();
export_schema(&schema_for!(State), &other_dir);
}
Loading
Loading