Skip to content

Commit

Permalink
update template to counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Buckram123 committed Dec 4, 2023
1 parent 722eb70 commit 012604a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examples/test-local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down
17 changes: 16 additions & 1 deletion src/handlers/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/handlers/instantiate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
10 changes: 8 additions & 2 deletions src/handlers/query.rs
Original file line number Diff line number Diff line change
@@ -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<Binary> {
match msg {
AppQueryMsg::Config {} => to_json_binary(&query_config(deps)?),
AppQueryMsg::Count {} => to_json_binary(&query_count(deps)?),
}
.map_err(Into::into)
}
Expand All @@ -14,3 +15,8 @@ fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let _config = CONFIG.load(deps.storage)?;
Ok(ConfigResponse {})
}

fn query_count(deps: Deps) -> StdResult<CountResponse> {
let count = COUNT.load(deps.storage)?;
Ok(CountResponse { count })
}
19 changes: 18 additions & 1 deletion src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {},
}

Expand All @@ -25,10 +35,17 @@ pub enum AppExecuteMsg {
pub enum AppQueryMsg {
#[returns(ConfigResponse)]
Config {},
#[returns(CountResponse)]
Count {},
}

#[cosmwasm_schema::cw_serde]
pub enum AppMigrateMsg {}

#[cosmwasm_schema::cw_serde]
pub struct ConfigResponse {}

#[cosmwasm_schema::cw_serde]
pub struct CountResponse {
pub count: i32,
}
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ use cw_storage_plus::Item;
pub struct Config {}

pub const CONFIG: Item<Config> = Item::new("config");
pub const COUNT: Item<i32> = Item::new("count");
49 changes: 45 additions & 4 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mock>, Abstract<Mock>, AppInterface<Mock>)> {
fn setup(
count: i32,
) -> anyhow::Result<(AbstractAccount<Mock>, Abstract<Mock>, AppInterface<Mock>)> {
// Create a sender
let sender = Addr::unchecked(OWNER);
// Create the mock
Expand All @@ -39,17 +43,54 @@ fn setup() -> anyhow::Result<(AbstractAccount<Mock>, Abstract<Mock>, 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))
}

#[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(())
}

0 comments on commit 012604a

Please sign in to comment.