use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult}; use cw2::set_contract_version; use crate::counter::{error::*, execute, msg::*, query, state::*}; // version info for migration info pub const CONTRACT_NAME: &str = "crates.io:counter"; const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); // ANCHOR: interface_entry // ANCHOR: entry_point_line #[cfg_attr(feature = "export", entry_point)] // ANCHOR_END: entry_point_line pub fn instantiate( deps: DepsMut, _env: Env, info: MessageInfo, msg: InstantiateMsg, ) -> Result { let state = State { count: msg.count, owner: info.sender.clone(), cousin: None, }; set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; STATE.save(deps.storage, &state)?; Ok(Response::new() .add_attribute("method", "instantiate") .add_attribute("owner", info.sender) .add_attribute("count", msg.count.to_string())) } #[cfg_attr(feature = "export", entry_point)] pub fn execute( deps: DepsMut, _env: Env, info: MessageInfo, msg: ExecuteMsg, ) -> Result { match msg { ExecuteMsg::Increment {} => execute::increment(deps), ExecuteMsg::Reset { count } => execute::reset(deps, info, count), ExecuteMsg::SetCousin { cousin } => execute::set_cousin(deps, info, cousin), } } #[cfg_attr(feature = "export", entry_point)] pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult { match msg { QueryMsg::Count {} => to_json_binary(&query::count(deps)?), QueryMsg::CousinCount {} => to_json_binary(&query::cousin_count(deps)?), QueryMsg::RawCousinCount {} => to_json_binary(&query::raw_cousin_count(deps)?), } } // ANCHOR_END: interface_entry #[cfg(test)] mod tests { use super::*; use cosmwasm_std::{ coins, from_json, testing::{mock_dependencies, mock_env, mock_info}, }; #[test] fn proper_initialization() { let mut deps = mock_dependencies(); let msg = InstantiateMsg { count: 17 }; let info = mock_info("creator", &coins(1000, "earth")); // we can just call .unwrap() to assert this was a success let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); assert_eq!(0, res.messages.len()); // it worked, let's query the state let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); let value: GetCountResponse = from_json(res).unwrap(); assert_eq!(17, value.count); } #[test] fn increment() { let mut deps = mock_dependencies(); let msg = InstantiateMsg { count: 17 }; let info = mock_info("creator", &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); // beneficiary can release it let info = mock_info("anyone", &coins(2, "token")); let msg = ExecuteMsg::Increment {}; let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap(); // should increase counter by 1 let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); let value: GetCountResponse = from_json(res).unwrap(); assert_eq!(18, value.count); } #[test] fn reset() { let mut deps = mock_dependencies(); let msg = InstantiateMsg { count: 17 }; let info = mock_info("creator", &coins(2, "token")); let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap(); // beneficiary can release it let unauth_info = mock_info("anyone", &coins(2, "token")); let msg = ExecuteMsg::Reset { count: 5 }; let res = execute(deps.as_mut(), mock_env(), unauth_info, msg); match res { Err(ContractError::Unauthorized {}) => {} _ => panic!("Must return unauthorized error"), } // only the original creator can reset the counter let auth_info = mock_info("creator", &coins(2, "token")); let msg = ExecuteMsg::Reset { count: 5 }; let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap(); // should now be 5 let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap(); let value: GetCountResponse = from_json(res).unwrap(); assert_eq!(5, value.count); } }