From 012604a54aebb0de4344be40d2682b9b40aed6f3 Mon Sep 17 00:00:00 2001 From: Buckram Date: Mon, 4 Dec 2023 22:16:51 +0200 Subject: [PATCH] update template to counter --- examples/test-local.rs | 2 +- src/handlers/execute.rs | 17 ++++++++++++- src/handlers/instantiate.rs | 5 ++-- src/handlers/query.rs | 10 ++++++-- src/msg.rs | 19 +++++++++++++- src/state.rs | 1 + tests/integration.rs | 49 ++++++++++++++++++++++++++++++++++--- 7 files changed, 92 insertions(+), 11 deletions(-) diff --git a/examples/test-local.rs b/examples/test-local.rs index f066ea1..1832593 100644 --- a/examples/test-local.rs +++ b/examples/test-local.rs @@ -59,7 +59,7 @@ fn main() -> anyhow::Result<()> { app.deploy(version)?; // Install app - account.install_app(app, &AppInstantiateMsg {}, None)?; + account.install_app(app, &AppInstantiateMsg { count: 0 }, None)?; assert_that!(account.manager.is_module_installed(APP_ID).unwrap()).is_true(); Ok(()) diff --git a/src/handlers/execute.rs b/src/handlers/execute.rs index c349444..a5e1c70 100644 --- a/src/handlers/execute.rs +++ b/src/handlers/execute.rs @@ -4,7 +4,7 @@ use cosmwasm_std::{DepsMut, Env, MessageInfo, Response}; use crate::contract::{App, AppResult}; use crate::msg::AppExecuteMsg; -use crate::state::CONFIG; +use crate::state::{CONFIG, COUNT}; pub fn execute_handler( deps: DepsMut, @@ -14,10 +14,25 @@ pub fn execute_handler( msg: AppExecuteMsg, ) -> AppResult { match msg { + AppExecuteMsg::Increment {} => increment(deps, app), + AppExecuteMsg::Reset { count } => reset(deps, info, count, app), AppExecuteMsg::UpdateConfig {} => update_config(deps, info, app), } } +fn increment(deps: DepsMut, app: App) -> AppResult { + COUNT.update(deps.storage, |count| AppResult::Ok(count + 1))?; + + Ok(app.tag_response(Response::default(), "increment")) +} + +fn reset(deps: DepsMut, info: MessageInfo, count: i32, app: App) -> AppResult { + app.admin.assert_admin(deps.as_ref(), &info.sender)?; + COUNT.save(deps.storage, &count)?; + + Ok(app.tag_response(Response::default(), "increment")) +} + /// Update the configuration of the app fn update_config(deps: DepsMut, msg_info: MessageInfo, app: App) -> AppResult { // Only the admin should be able to call this diff --git a/src/handlers/instantiate.rs b/src/handlers/instantiate.rs index db23e7f..070a27f 100644 --- a/src/handlers/instantiate.rs +++ b/src/handlers/instantiate.rs @@ -2,18 +2,19 @@ use cosmwasm_std::{DepsMut, Env, MessageInfo, Response}; use crate::contract::{App, AppResult}; use crate::msg::AppInstantiateMsg; -use crate::state::{Config, CONFIG}; +use crate::state::{Config, CONFIG, COUNT}; pub fn instantiate_handler( deps: DepsMut, _env: Env, _info: MessageInfo, _app: App, - _msg: AppInstantiateMsg, + msg: AppInstantiateMsg, ) -> AppResult { let config: Config = Config {}; CONFIG.save(deps.storage, &config)?; + COUNT.save(deps.storage, &msg.count)?; // Example instantiation that doesn't do anything Ok(Response::new()) diff --git a/src/handlers/query.rs b/src/handlers/query.rs index 4192196..3abe555 100644 --- a/src/handlers/query.rs +++ b/src/handlers/query.rs @@ -1,11 +1,12 @@ use crate::contract::{App, AppResult}; -use crate::msg::{AppQueryMsg, ConfigResponse}; -use crate::state::CONFIG; +use crate::msg::{AppQueryMsg, ConfigResponse, CountResponse}; +use crate::state::{CONFIG, COUNT}; use cosmwasm_std::{to_json_binary, Binary, Deps, Env, StdResult}; pub fn query_handler(deps: Deps, _env: Env, _app: &App, msg: AppQueryMsg) -> AppResult { match msg { AppQueryMsg::Config {} => to_json_binary(&query_config(deps)?), + AppQueryMsg::Count {} => to_json_binary(&query_count(deps)?), } .map_err(Into::into) } @@ -14,3 +15,8 @@ fn query_config(deps: Deps) -> StdResult { let _config = CONFIG.load(deps.storage)?; Ok(ConfigResponse {}) } + +fn query_count(deps: Deps) -> StdResult { + let count = COUNT.load(deps.storage)?; + Ok(CountResponse { count }) +} diff --git a/src/msg.rs b/src/msg.rs index db6fa97..06026c3 100644 --- a/src/msg.rs +++ b/src/msg.rs @@ -7,13 +7,23 @@ abstract_app::app_msg_types!(App, AppExecuteMsg, AppQueryMsg); /// App instantiate message #[cosmwasm_schema::cw_serde] -pub struct AppInstantiateMsg {} +pub struct AppInstantiateMsg { + /// Initial count + pub count: i32, +} /// App execute messages #[cosmwasm_schema::cw_serde] #[cfg_attr(feature = "interface", derive(cw_orch::ExecuteFns))] #[cfg_attr(feature = "interface", impl_into(ExecuteMsg))] pub enum AppExecuteMsg { + /// Increment count by 1 + Increment {}, + /// Admin method - reset count + Reset { + /// Count value after reset + count: i32, + }, UpdateConfig {}, } @@ -25,6 +35,8 @@ pub enum AppExecuteMsg { pub enum AppQueryMsg { #[returns(ConfigResponse)] Config {}, + #[returns(CountResponse)] + Count {}, } #[cosmwasm_schema::cw_serde] @@ -32,3 +44,8 @@ pub enum AppMigrateMsg {} #[cosmwasm_schema::cw_serde] pub struct ConfigResponse {} + +#[cosmwasm_schema::cw_serde] +pub struct CountResponse { + pub count: i32, +} diff --git a/src/state.rs b/src/state.rs index 95dbe0a..52b2a63 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,3 +4,4 @@ use cw_storage_plus::Item; pub struct Config {} pub const CONFIG: Item = Item::new("config"); +pub const COUNT: Item = Item::new("count"); diff --git a/tests/integration.rs b/tests/integration.rs index 3de62df..311bec1 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -3,16 +3,20 @@ use abstract_interface::{Abstract, AbstractAccount, AppDeployer, VCExecFns}; use abstract_testing::OWNER; use app::{ contract::{APP_ID, APP_VERSION}, - msg::{AppInstantiateMsg, ConfigResponse}, + error::AppError, + msg::{AppInstantiateMsg, ConfigResponse, CountResponse}, *, }; +use cw_controllers::AdminError; // Use prelude to get all the necessary imports use cw_orch::{anyhow, deploy::Deploy, prelude::*}; use cosmwasm_std::Addr; /// Set up the test environment with the contract installed -fn setup() -> anyhow::Result<(AbstractAccount, Abstract, AppInterface)> { +fn setup( + count: i32, +) -> anyhow::Result<(AbstractAccount, Abstract, AppInterface)> { // Create a sender let sender = Addr::unchecked(OWNER); // Create the mock @@ -39,7 +43,7 @@ fn setup() -> anyhow::Result<(AbstractAccount, Abstract, AppInterfac app.deploy(APP_VERSION.parse()?)?; - account.install_app(app.clone(), &AppInstantiateMsg {}, None)?; + account.install_app(app.clone(), &AppInstantiateMsg { count }, None)?; Ok((account, abstr_deployment, app)) } @@ -47,9 +51,46 @@ fn setup() -> anyhow::Result<(AbstractAccount, Abstract, AppInterfac #[test] fn successful_install() -> anyhow::Result<()> { // Set up the environment and contract - let (_account, _abstr, app) = setup()?; + let (_account, _abstr, app) = setup(0)?; let config = app.config()?; assert_eq!(config, ConfigResponse {}); Ok(()) } + +#[test] +fn successful_increment() -> anyhow::Result<()> { + // Set up the environment and contract + let (_account, _abstr, app) = setup(0)?; + + app.increment()?; + let count: CountResponse = app.count()?; + assert_eq!(count.count, 1); + Ok(()) +} + +#[test] +fn successful_reset() -> anyhow::Result<()> { + // Set up the environment and contract + let (account, _abstr, app) = setup(0)?; + + app.call_as(&account.manager.address()?).reset(42)?; + let count: CountResponse = app.count()?; + assert_eq!(count.count, 6); + Ok(()) +} + +#[test] +fn failed_reset() -> anyhow::Result<()> { + // Set up the environment and contract + let (_account, _abstr, app) = setup(0)?; + + let err: AppError = app + .call_as(&Addr::unchecked("NotAdmin")) + .reset(9) + .unwrap_err() + .downcast() + .unwrap(); + assert_eq!(err, AppError::Admin(AdminError::NotAdmin {})); + Ok(()) +}