1#[cfg(not(feature = "library"))]
2use cosmwasm_std::entry_point;
3use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
4use cw2::set_contract_version;
5
6use crate::error::ContractError;
7use crate::msg::{ExecuteMsg, GetCountResponse, InstantiateMsg, QueryMsg};
8use crate::state::{State, STATE};
9
10const CONTRACT_NAME: &str = "crates.io:over-time-test";
12const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
13
14#[cfg_attr(not(feature = "library"), entry_point)]
15pub fn instantiate(
16 deps: DepsMut,
17 _env: Env,
18 info: MessageInfo,
19 msg: InstantiateMsg,
20) -> Result<Response, ContractError> {
21 let state = State {
22 count: msg.count,
23 owner: info.sender.clone(),
24 };
25 set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
26 STATE.save(deps.storage, &state)?;
27
28 Ok(Response::new()
29 .add_attribute("method", "instantiate")
30 .add_attribute("owner", info.sender)
31 .add_attribute("count", msg.count.to_string()))
32}
33
34#[cfg_attr(not(feature = "library"), entry_point)]
35pub fn execute(
36 deps: DepsMut,
37 _env: Env,
38 info: MessageInfo,
39 msg: ExecuteMsg,
40) -> Result<Response, ContractError> {
41 match msg {
42 ExecuteMsg::Increment {} => execute::increment(deps),
43 ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
44 }
45}
46
47pub mod execute {
48 use super::*;
49
50 pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
51 STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
52 state.count += 1;
53 Ok(state)
54 })?;
55
56 Ok(Response::new().add_attribute("action", "increment"))
57 }
58
59 pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
60 STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
61 if info.sender != state.owner {
62 return Err(ContractError::Unauthorized {});
63 }
64 state.count = count;
65 Ok(state)
66 })?;
67 Ok(Response::new().add_attribute("action", "reset"))
68 }
69}
70
71#[cfg_attr(not(feature = "library"), entry_point)]
72pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
73 match msg {
74 QueryMsg::GetCount {} => to_binary(&query::count(deps)?),
75 }
76}
77
78pub mod query {
79 use super::*;
80
81 pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
82 let state = STATE.load(deps.storage)?;
83 Ok(GetCountResponse { count: state.count })
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
91 use cosmwasm_std::{coins, from_binary};
92
93 #[test]
94 fn proper_initialization() {
95 let mut deps = mock_dependencies();
96
97 let msg = InstantiateMsg { count: 17 };
98 let info = mock_info("creator", &coins(1000, "earth"));
99
100 let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
102 assert_eq!(0, res.messages.len());
103
104 let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
106 let value: GetCountResponse = from_binary(&res).unwrap();
107 assert_eq!(17, value.count);
108 }
109
110 #[test]
111 fn increment() {
112 let mut deps = mock_dependencies();
113
114 let msg = InstantiateMsg { count: 17 };
115 let info = mock_info("creator", &coins(2, "token"));
116 let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
117
118 let info = mock_info("anyone", &coins(2, "token"));
120 let msg = ExecuteMsg::Increment {};
121 let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
122
123 let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
125 let value: GetCountResponse = from_binary(&res).unwrap();
126 assert_eq!(18, value.count);
127 }
128
129 #[test]
130 fn reset() {
131 let mut deps = mock_dependencies();
132
133 let msg = InstantiateMsg { count: 17 };
134 let info = mock_info("creator", &coins(2, "token"));
135 let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
136
137 let unauth_info = mock_info("anyone", &coins(2, "token"));
139 let msg = ExecuteMsg::Reset { count: 5 };
140 let res = execute(deps.as_mut(), mock_env(), unauth_info, msg);
141 match res {
142 Err(ContractError::Unauthorized {}) => {}
143 _ => panic!("Must return unauthorized error"),
144 }
145
146 let auth_info = mock_info("creator", &coins(2, "token"));
148 let msg = ExecuteMsg::Reset { count: 5 };
149 let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap();
150
151 let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
153 let value: GetCountResponse = from_binary(&res).unwrap();
154 assert_eq!(5, value.count);
155 }
156}