From 57e324b88de08eb6c4bafeb9d41960b120191bc1 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Mon, 29 May 2023 22:16:57 +0200 Subject: [PATCH 1/5] feat: fake-vault --- Cargo.lock | 13 +++ contracts/fake-vault/.cargo/config | 6 ++ contracts/fake-vault/Cargo.toml | 44 +++++++++ contracts/fake-vault/README.md | 3 + contracts/fake-vault/examples/schema.rs | 30 ++++++ contracts/fake-vault/schema/execute_msg.json | 32 +++++++ .../fake-vault/schema/instantiate_msg.json | 21 ++++ contracts/fake-vault/schema/query_msg.json | 92 ++++++++++++++++++ contracts/fake-vault/src/contract.rs | 95 +++++++++++++++++++ contracts/fake-vault/src/lib.rs | 3 + contracts/fake-vault/src/msg.rs | 33 +++++++ contracts/fake-vault/src/store.rs | 15 +++ 12 files changed, 387 insertions(+) create mode 100644 contracts/fake-vault/.cargo/config create mode 100644 contracts/fake-vault/Cargo.toml create mode 100644 contracts/fake-vault/README.md create mode 100644 contracts/fake-vault/examples/schema.rs create mode 100644 contracts/fake-vault/schema/execute_msg.json create mode 100644 contracts/fake-vault/schema/instantiate_msg.json create mode 100644 contracts/fake-vault/schema/query_msg.json create mode 100644 contracts/fake-vault/src/contract.rs create mode 100644 contracts/fake-vault/src/lib.rs create mode 100644 contracts/fake-vault/src/msg.rs create mode 100644 contracts/fake-vault/src/store.rs diff --git a/Cargo.lock b/Cargo.lock index c659f4f..b003826 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -428,6 +428,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "fake-vault" +version = "0.1.0" +dependencies = [ + "cosmwasm-schema", + "cosmwasm-std", + "cw-storage-plus", + "cw2", + "neutron-sdk", + "schemars", + "serde", +] + [[package]] name = "ff" version = "0.12.1" diff --git a/contracts/fake-vault/.cargo/config b/contracts/fake-vault/.cargo/config new file mode 100644 index 0000000..7c11532 --- /dev/null +++ b/contracts/fake-vault/.cargo/config @@ -0,0 +1,6 @@ +[alias] +wasm = "build --release --target wasm32-unknown-unknown" +wasm-debug = "build --target wasm32-unknown-unknown" +unit-test = "test --lib --features backtraces" +integration-test = "test --test integration" +schema = "run --example schema" diff --git a/contracts/fake-vault/Cargo.toml b/contracts/fake-vault/Cargo.toml new file mode 100644 index 0000000..d0288a8 --- /dev/null +++ b/contracts/fake-vault/Cargo.toml @@ -0,0 +1,44 @@ +[package] +name = "fake-vault" +version = "0.1.0" +edition = "2021" +authors = ["Sergey Ratiashvili "] + + +exclude = [ + # Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication. + "contract.wasm", + "hash.txt", +] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[profile.release] +opt-level = 3 +debug = false +rpath = false +lto = true +debug-assertions = false +codegen-units = 1 +panic = 'abort' +incremental = false +overflow-checks = true + +[features] +# for quicker tests, cargo test --lib +# for more explicit tests, cargo test --features=backtraces +backtraces = ["cosmwasm-std/backtraces"] +library = [] + +[dependencies] +cosmwasm-std = "1.2.5" +cw2 = "1.0.1" +schemars = "0.8.10" +cw-storage-plus = "1.0.1" +serde = { version = "1.0.103", default-features = false, features = ["derive"] } +neutron-sdk = "0.5.0" +[dev-dependencies] +cosmwasm-schema = { version = "1.0.0", default-features = false } diff --git a/contracts/fake-vault/README.md b/contracts/fake-vault/README.md new file mode 100644 index 0000000..aa085dd --- /dev/null +++ b/contracts/fake-vault/README.md @@ -0,0 +1,3 @@ +# Reflect + +This contract is used for tests in the main neutron repository. \ No newline at end of file diff --git a/contracts/fake-vault/examples/schema.rs b/contracts/fake-vault/examples/schema.rs new file mode 100644 index 0000000..26a1a35 --- /dev/null +++ b/contracts/fake-vault/examples/schema.rs @@ -0,0 +1,30 @@ +// Copyright 2022 Neutron +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use std::env::current_dir; +use std::fs::create_dir_all; + +use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; +use fake_vault::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); +} diff --git a/contracts/fake-vault/schema/execute_msg.json b/contracts/fake-vault/schema/execute_msg.json new file mode 100644 index 0000000..5f0df72 --- /dev/null +++ b/contracts/fake-vault/schema/execute_msg.json @@ -0,0 +1,32 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "ExecuteMsg", + "oneOf": [ + { + "type": "object", + "required": [ + "set_power" + ], + "properties": { + "set_power": { + "type": "object", + "required": [ + "address", + "power" + ], + "properties": { + "address": { + "type": "string" + }, + "power": { + "type": "integer", + "format": "uint64", + "minimum": 0.0 + } + } + } + }, + "additionalProperties": false + } + ] +} diff --git a/contracts/fake-vault/schema/instantiate_msg.json b/contracts/fake-vault/schema/instantiate_msg.json new file mode 100644 index 0000000..12bfc9c --- /dev/null +++ b/contracts/fake-vault/schema/instantiate_msg.json @@ -0,0 +1,21 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "InstantiateMsg", + "type": "object", + "required": [ + "dao", + "description", + "info" + ], + "properties": { + "dao": { + "type": "string" + }, + "description": { + "type": "string" + }, + "info": { + "type": "string" + } + } +} diff --git a/contracts/fake-vault/schema/query_msg.json b/contracts/fake-vault/schema/query_msg.json new file mode 100644 index 0000000..151a5e2 --- /dev/null +++ b/contracts/fake-vault/schema/query_msg.json @@ -0,0 +1,92 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "QueryMsg", + "oneOf": [ + { + "type": "object", + "required": [ + "dao" + ], + "properties": { + "dao": { + "type": "object" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "description" + ], + "properties": { + "description": { + "type": "object" + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "voting_power_at_height" + ], + "properties": { + "voting_power_at_height": { + "type": "object", + "required": [ + "address" + ], + "properties": { + "address": { + "type": "string" + }, + "height": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0.0 + } + } + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "total_power_at_height" + ], + "properties": { + "total_power_at_height": { + "type": "object", + "properties": { + "height": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0.0 + } + } + } + }, + "additionalProperties": false + }, + { + "type": "object", + "required": [ + "info" + ], + "properties": { + "info": { + "type": "object" + } + }, + "additionalProperties": false + } + ] +} diff --git a/contracts/fake-vault/src/contract.rs b/contracts/fake-vault/src/contract.rs new file mode 100644 index 0000000..0349987 --- /dev/null +++ b/contracts/fake-vault/src/contract.rs @@ -0,0 +1,95 @@ +use crate::{ + msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}, + store::{Config, CONFIG, USERS}, +}; +use cosmwasm_std::{ + entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, +}; +use cw2::set_contract_version; + +const CONTRACT_NAME: &str = concat!("crates.io:neutron-contracts__", env!("CARGO_PKG_NAME")); +const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); + +#[entry_point] +pub fn instantiate( + deps: DepsMut, + _env: Env, + _info: MessageInfo, + msg: InstantiateMsg, +) -> StdResult { + set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; + let config = Config { + dao: msg.dao, + description: msg.description, + info: msg.info, + }; + CONFIG.save(deps.storage, &config)?; + Ok(Response::default()) +} + +#[entry_point] +pub fn execute(deps: DepsMut, _env: Env, _: MessageInfo, msg: ExecuteMsg) -> StdResult { + match msg { + ExecuteMsg::SetPower { address, power } => set_power(deps, address, power), + } +} + +fn set_power(deps: DepsMut, address: String, power: u64) -> StdResult { + USERS.save(deps.storage, address, &power)?; + Ok(Response::default()) +} + +#[entry_point] +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { + match msg { + QueryMsg::Dao {} => to_binary(&query_dao(deps)?), + QueryMsg::Description {} => to_binary(&query_description(deps)?), + QueryMsg::Info {} => to_binary(&query_info(deps)?), + QueryMsg::VotingPowerAtHeight { height, address } => { + to_binary(&query_voting_power_at_height(deps, env, height, address)?) + } + QueryMsg::TotalPowerAtHeight { height } => { + to_binary(&query_total_power_at_height(deps, env, height)?) + } + } +} + +fn query_dao(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + Ok(config.dao) +} + +fn query_info(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + Ok(config.info) +} + +fn query_description(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + Ok(config.description) +} + +fn query_voting_power_at_height( + deps: Deps, + _env: Env, + _height: Option, + address: String, +) -> StdResult { + let power = USERS.may_load(deps.storage, address)?.unwrap_or_default(); + Ok(power) +} + +fn query_total_power_at_height(deps: Deps, _env: Env, _height: Option) -> StdResult { + let mut total_power: u64 = 0; + for user in USERS.range(deps.storage, None, None, Order::Ascending) { + let (_, power) = user?; + total_power += power; + } + Ok(total_power) +} + +#[entry_point] +pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult { + deps.api.debug("WASMDEBUG: migrate"); + Ok(Response::default()) +} diff --git a/contracts/fake-vault/src/lib.rs b/contracts/fake-vault/src/lib.rs new file mode 100644 index 0000000..27779bc --- /dev/null +++ b/contracts/fake-vault/src/lib.rs @@ -0,0 +1,3 @@ +pub mod contract; +pub mod msg; +pub mod store; diff --git a/contracts/fake-vault/src/msg.rs b/contracts/fake-vault/src/msg.rs new file mode 100644 index 0000000..ef36e2a --- /dev/null +++ b/contracts/fake-vault/src/msg.rs @@ -0,0 +1,33 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum QueryMsg { + Dao {}, + Description {}, + VotingPowerAtHeight { + height: Option, + address: String, + }, + TotalPowerAtHeight { + height: Option, + }, + Info {}, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[serde(rename_all = "snake_case")] +pub enum ExecuteMsg { + SetPower { address: String, power: u64 }, +} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct MigrateMsg {} + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct InstantiateMsg { + pub dao: String, + pub description: String, + pub info: String, +} diff --git a/contracts/fake-vault/src/store.rs b/contracts/fake-vault/src/store.rs new file mode 100644 index 0000000..c233857 --- /dev/null +++ b/contracts/fake-vault/src/store.rs @@ -0,0 +1,15 @@ +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +use cw_storage_plus::{Item, Map}; + +pub const USERS: Map = Map::new("users"); + +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct Config { + pub dao: String, + pub description: String, + pub info: String, +} + +pub const CONFIG: Item = Item::new("config"); From 112e0908989c5dfaa3448a347ad66085bbc854ee Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Mon, 29 May 2023 23:00:30 +0200 Subject: [PATCH 2/5] feat: add name query --- contracts/fake-vault/schema/instantiate_msg.json | 6 +++++- contracts/fake-vault/schema/query_msg.json | 12 ++++++++++++ contracts/fake-vault/src/contract.rs | 6 ++++++ contracts/fake-vault/src/msg.rs | 2 ++ contracts/fake-vault/src/store.rs | 1 + 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/contracts/fake-vault/schema/instantiate_msg.json b/contracts/fake-vault/schema/instantiate_msg.json index 12bfc9c..82a8440 100644 --- a/contracts/fake-vault/schema/instantiate_msg.json +++ b/contracts/fake-vault/schema/instantiate_msg.json @@ -5,7 +5,8 @@ "required": [ "dao", "description", - "info" + "info", + "name" ], "properties": { "dao": { @@ -16,6 +17,9 @@ }, "info": { "type": "string" + }, + "name": { + "type": "string" } } } diff --git a/contracts/fake-vault/schema/query_msg.json b/contracts/fake-vault/schema/query_msg.json index 151a5e2..9d9389f 100644 --- a/contracts/fake-vault/schema/query_msg.json +++ b/contracts/fake-vault/schema/query_msg.json @@ -26,6 +26,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "name" + ], + "properties": { + "name": { + "type": "object" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/contracts/fake-vault/src/contract.rs b/contracts/fake-vault/src/contract.rs index 0349987..819fc55 100644 --- a/contracts/fake-vault/src/contract.rs +++ b/contracts/fake-vault/src/contract.rs @@ -22,6 +22,7 @@ pub fn instantiate( dao: msg.dao, description: msg.description, info: msg.info, + name: msg.name, }; CONFIG.save(deps.storage, &config)?; Ok(Response::default()) @@ -45,6 +46,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { QueryMsg::Dao {} => to_binary(&query_dao(deps)?), QueryMsg::Description {} => to_binary(&query_description(deps)?), QueryMsg::Info {} => to_binary(&query_info(deps)?), + QueryMsg::Name {} => to_binary(&query_name(deps)?), QueryMsg::VotingPowerAtHeight { height, address } => { to_binary(&query_voting_power_at_height(deps, env, height, address)?) } @@ -68,6 +70,10 @@ fn query_description(deps: Deps) -> StdResult { let config = CONFIG.load(deps.storage)?; Ok(config.description) } +fn query_name(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + Ok(config.name) +} fn query_voting_power_at_height( deps: Deps, diff --git a/contracts/fake-vault/src/msg.rs b/contracts/fake-vault/src/msg.rs index ef36e2a..b785ef0 100644 --- a/contracts/fake-vault/src/msg.rs +++ b/contracts/fake-vault/src/msg.rs @@ -6,6 +6,7 @@ use serde::{Deserialize, Serialize}; pub enum QueryMsg { Dao {}, Description {}, + Name {}, VotingPowerAtHeight { height: Option, address: String, @@ -30,4 +31,5 @@ pub struct InstantiateMsg { pub dao: String, pub description: String, pub info: String, + pub name: String, } diff --git a/contracts/fake-vault/src/store.rs b/contracts/fake-vault/src/store.rs index c233857..a34183c 100644 --- a/contracts/fake-vault/src/store.rs +++ b/contracts/fake-vault/src/store.rs @@ -10,6 +10,7 @@ pub struct Config { pub dao: String, pub description: String, pub info: String, + pub name: String, } pub const CONFIG: Item = Item::new("config"); From bdc1c13c6334906378390662c7696c85897ca72e Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Tue, 30 May 2023 11:22:25 +0200 Subject: [PATCH 3/5] fix: default zero --- contracts/fake-vault/src/contract.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/fake-vault/src/contract.rs b/contracts/fake-vault/src/contract.rs index 819fc55..fcc7d3d 100644 --- a/contracts/fake-vault/src/contract.rs +++ b/contracts/fake-vault/src/contract.rs @@ -81,7 +81,7 @@ fn query_voting_power_at_height( _height: Option, address: String, ) -> StdResult { - let power = USERS.may_load(deps.storage, address)?.unwrap_or_default(); + let power = USERS.may_load(deps.storage, address)?.unwrap_or(0); Ok(power) } From 1561652503957bc9876e76832b8b4d15878e98e3 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Tue, 30 May 2023 14:26:41 +0200 Subject: [PATCH 4/5] feat: power response --- contracts/fake-vault/src/contract.rs | 25 ++++++++++++++++++------- contracts/fake-vault/src/msg.rs | 6 ++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/contracts/fake-vault/src/contract.rs b/contracts/fake-vault/src/contract.rs index fcc7d3d..55c79ae 100644 --- a/contracts/fake-vault/src/contract.rs +++ b/contracts/fake-vault/src/contract.rs @@ -1,9 +1,10 @@ use crate::{ - msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}, + msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, PowerResponse, QueryMsg}, store::{Config, CONFIG, USERS}, }; use cosmwasm_std::{ entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult, + Uint128, }; use cw2::set_contract_version; @@ -77,21 +78,31 @@ fn query_name(deps: Deps) -> StdResult { fn query_voting_power_at_height( deps: Deps, - _env: Env, - _height: Option, + env: Env, + height: Option, address: String, -) -> StdResult { +) -> StdResult { let power = USERS.may_load(deps.storage, address)?.unwrap_or(0); - Ok(power) + Ok(PowerResponse { + power: Uint128::from(power), + height: height.unwrap_or(env.block.height), + }) } -fn query_total_power_at_height(deps: Deps, _env: Env, _height: Option) -> StdResult { +fn query_total_power_at_height( + deps: Deps, + env: Env, + height: Option, +) -> StdResult { let mut total_power: u64 = 0; for user in USERS.range(deps.storage, None, None, Order::Ascending) { let (_, power) = user?; total_power += power; } - Ok(total_power) + Ok(PowerResponse { + power: Uint128::from(total_power), + height: height.unwrap_or(env.block.height), + }) } #[entry_point] diff --git a/contracts/fake-vault/src/msg.rs b/contracts/fake-vault/src/msg.rs index b785ef0..d930c6c 100644 --- a/contracts/fake-vault/src/msg.rs +++ b/contracts/fake-vault/src/msg.rs @@ -1,3 +1,4 @@ +use cosmwasm_std::Uint128; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -33,3 +34,8 @@ pub struct InstantiateMsg { pub info: String, pub name: String, } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +pub struct PowerResponse { + pub power: Uint128, + pub height: u64, +} From 83fe3591861283fd6d6b4d1a8e2f15485ef59b66 Mon Sep 17 00:00:00 2001 From: Sergey Ratiashvili Date: Tue, 30 May 2023 15:34:11 +0200 Subject: [PATCH 5/5] feat: add config query --- contracts/fake-vault/schema/query_msg.json | 12 ++++++++++++ contracts/fake-vault/src/contract.rs | 6 ++++++ contracts/fake-vault/src/msg.rs | 1 + 3 files changed, 19 insertions(+) diff --git a/contracts/fake-vault/schema/query_msg.json b/contracts/fake-vault/schema/query_msg.json index 9d9389f..02a0241 100644 --- a/contracts/fake-vault/schema/query_msg.json +++ b/contracts/fake-vault/schema/query_msg.json @@ -38,6 +38,18 @@ }, "additionalProperties": false }, + { + "type": "object", + "required": [ + "config" + ], + "properties": { + "config": { + "type": "object" + } + }, + "additionalProperties": false + }, { "type": "object", "required": [ diff --git a/contracts/fake-vault/src/contract.rs b/contracts/fake-vault/src/contract.rs index 55c79ae..4ef2542 100644 --- a/contracts/fake-vault/src/contract.rs +++ b/contracts/fake-vault/src/contract.rs @@ -48,6 +48,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { QueryMsg::Description {} => to_binary(&query_description(deps)?), QueryMsg::Info {} => to_binary(&query_info(deps)?), QueryMsg::Name {} => to_binary(&query_name(deps)?), + QueryMsg::Config {} => to_binary(&query_config(deps)?), QueryMsg::VotingPowerAtHeight { height, address } => { to_binary(&query_voting_power_at_height(deps, env, height, address)?) } @@ -76,6 +77,11 @@ fn query_name(deps: Deps) -> StdResult { Ok(config.name) } +fn query_config(deps: Deps) -> StdResult { + let config = CONFIG.load(deps.storage)?; + Ok(config) +} + fn query_voting_power_at_height( deps: Deps, env: Env, diff --git a/contracts/fake-vault/src/msg.rs b/contracts/fake-vault/src/msg.rs index d930c6c..9d7f3cc 100644 --- a/contracts/fake-vault/src/msg.rs +++ b/contracts/fake-vault/src/msg.rs @@ -8,6 +8,7 @@ pub enum QueryMsg { Dao {}, Description {}, Name {}, + Config {}, VotingPowerAtHeight { height: Option, address: String,