1use crate::{contracts, AGENT, ALICE, BOB, CHARLIZE, DENOM, PAUSE_ADMIN, VERSION, VERY_RICH};
3use cosmwasm_std::{coins, to_binary, Addr, BlockInfo, Coin};
4use croncat_sdk_agents::msg::InstantiateMsg as AgentsInstantiateMsg;
5use croncat_sdk_factory::msg::{ContractMetadataResponse, ModuleInstantiateInfo, VersionKind};
6use croncat_sdk_manager::msg::ManagerInstantiateMsg;
7use croncat_sdk_tasks::msg::TasksInstantiateMsg;
8use cw_multi_test::{App, AppBuilder, Executor};
9
10pub struct CronCatTestEnv {
11 pub app: App,
12 pub factory: Addr,
13 pub manager: Addr,
14 pub tasks: Addr,
15 pub agents: Addr,
16}
17
18pub fn set_up_croncat_contracts(app: Option<App>) -> CronCatTestEnv {
19 set_up_croncat_contracts_custom(app, Some(DENOM), None, None, None)
20}
21
22pub fn set_up_croncat_contracts_custom(
23 app: Option<App>,
24 denom: Option<&str>,
25 manager_instantiate_msg: Option<ManagerInstantiateMsg>,
26 agents_instantiate_msg: Option<AgentsInstantiateMsg>,
27 tasks_instantiate_msg: Option<TasksInstantiateMsg>,
28) -> CronCatTestEnv {
29 let denom = denom.unwrap_or(DENOM);
30 let mut app = app.unwrap_or_else(|| default_app(denom));
31
32 let factory_addr = init_factory(&mut app);
33
34 let manager_instantiate_msg: ManagerInstantiateMsg =
35 manager_instantiate_msg.unwrap_or_else(default_manager_instantiate_message);
36 let manager_addr = init_manager(
37 &mut app,
38 &manager_instantiate_msg,
39 &factory_addr,
40 &coins(1, denom),
41 );
42
43 let agents_instantiate_msg: AgentsInstantiateMsg =
44 agents_instantiate_msg.unwrap_or_else(default_agents_instantiate_message);
45 let agents_addr = init_agents(&mut app, &agents_instantiate_msg, &factory_addr, &[]);
46
47 let tasks_instantiate_msg: TasksInstantiateMsg =
48 tasks_instantiate_msg.unwrap_or_else(default_tasks_instantiate_msg);
49 let tasks_addr = init_tasks(&mut app, &tasks_instantiate_msg, &factory_addr);
50
51 CronCatTestEnv {
52 app,
53 factory: factory_addr,
54 manager: manager_addr,
55 tasks: tasks_addr,
56 agents: agents_addr,
57 }
58}
59
60pub fn default_app(denom: &str) -> App {
61 AppBuilder::new().build(|router, _, storage| {
62 let accounts: Vec<(u128, String)> = vec![
63 (6_000_000, ALICE.to_string()),
64 (6_000_000, AGENT.to_string()),
65 (600_000, BOB.to_string()),
66 (666_000, CHARLIZE.to_string()),
67 (u128::MAX.saturating_sub(1000), VERY_RICH.to_string()),
68 ];
69 for (amt, address) in accounts {
70 router
71 .bank
72 .init_balance(storage, &Addr::unchecked(address), coins(amt, denom))
73 .unwrap();
74 }
75 })
76}
77
78pub fn init_agents(
79 app: &mut App,
80 msg: &AgentsInstantiateMsg,
81 factory_addr: &Addr,
82 funds: &[Coin],
83) -> Addr {
84 let code_id = app.store_code(contracts::croncat_agents_contract());
85
86 let module_instantiate_info = ModuleInstantiateInfo {
87 code_id,
88 version: [0, 1],
89 commit_id: "commit1".to_owned(),
90 checksum: "checksum2".to_owned(),
91 changelog_url: None,
92 schema: None,
93 msg: to_binary(msg).unwrap(),
94 contract_name: "agents".to_owned(),
95 };
96
97 app.execute_contract(
98 Addr::unchecked(ALICE),
99 factory_addr.to_owned(),
100 &croncat_sdk_factory::msg::FactoryExecuteMsg::Deploy {
101 kind: VersionKind::Manager,
102 module_instantiate_info,
103 },
104 funds,
105 )
106 .unwrap();
107
108 let metadata: ContractMetadataResponse = app
109 .wrap()
110 .query_wasm_smart(
111 factory_addr,
112 &croncat_sdk_factory::msg::FactoryQueryMsg::LatestContract {
113 contract_name: "agents".to_owned(),
114 },
115 )
116 .unwrap();
117 metadata.metadata.unwrap().contract_addr
118}
119
120pub fn init_factory(app: &mut App) -> Addr {
121 let code_id = app.store_code(contracts::croncat_factory_contract());
122 app.instantiate_contract(
123 code_id,
124 Addr::unchecked(ALICE),
125 &croncat_sdk_factory::msg::FactoryInstantiateMsg { owner_addr: None },
126 &[],
127 "croncat_factory",
128 None,
129 )
130 .unwrap()
131}
132
133pub fn init_manager(
134 app: &mut App,
135 msg: &ManagerInstantiateMsg,
136 factory_addr: &Addr,
137 funds: &[Coin],
138) -> Addr {
139 let code_id = app.store_code(contracts::croncat_manager_contract());
140
141 let module_instantiate_info = ModuleInstantiateInfo {
142 code_id,
143 version: [0, 1],
144 commit_id: "commit1".to_owned(),
145 checksum: "checksum2".to_owned(),
146 changelog_url: None,
147 schema: None,
148 msg: to_binary(msg).unwrap(),
149 contract_name: "manager".to_owned(),
150 };
151
152 app.execute_contract(
153 Addr::unchecked(ALICE),
154 factory_addr.to_owned(),
155 &croncat_sdk_factory::msg::FactoryExecuteMsg::Deploy {
156 kind: VersionKind::Manager,
157 module_instantiate_info,
158 },
159 funds,
160 )
161 .unwrap();
162
163 let metadata: ContractMetadataResponse = app
164 .wrap()
165 .query_wasm_smart(
166 factory_addr,
167 &croncat_sdk_factory::msg::FactoryQueryMsg::LatestContract {
168 contract_name: "manager".to_owned(),
169 },
170 )
171 .unwrap();
172 metadata.metadata.unwrap().contract_addr
173}
174
175pub fn default_tasks_instantiate_msg() -> TasksInstantiateMsg {
176 TasksInstantiateMsg {
177 chain_name: "atom".to_owned(),
178 version: Some(VERSION.to_string()),
179 pause_admin: Addr::unchecked(PAUSE_ADMIN),
180 croncat_manager_key: ("manager".to_owned(), [0, 1]),
181 croncat_agents_key: ("agents".to_owned(), [0, 1]),
182 slot_granularity_time: None,
183 gas_base_fee: None,
184 gas_action_fee: None,
185 gas_query_fee: None,
186 gas_limit: None,
187 }
188}
189
190pub fn default_manager_instantiate_message() -> ManagerInstantiateMsg {
191 ManagerInstantiateMsg {
192 version: Some(VERSION.to_owned()),
193 croncat_tasks_key: ("tasks".to_owned(), [0, 1]),
194 croncat_agents_key: ("agents".to_owned(), [0, 1]),
195 pause_admin: Addr::unchecked(PAUSE_ADMIN),
196 gas_price: None,
197 treasury_addr: None,
198 cw20_whitelist: None,
199 }
200}
201
202pub fn default_agents_instantiate_message() -> AgentsInstantiateMsg {
203 AgentsInstantiateMsg {
204 version: Some(VERSION.to_string()),
205 croncat_manager_key: ("manager".to_string(), [0, 1]),
206 croncat_tasks_key: ("tasks".to_owned(), [0, 1]),
207 agent_nomination_duration: None,
208 min_tasks_per_agent: None,
209 min_coins_for_agent_registration: None,
210 agents_eject_threshold: None,
211 min_active_agent_count: None,
212 public_registration: false,
213 pause_admin: Addr::unchecked(PAUSE_ADMIN),
214 allowed_agents: Some(vec![AGENT.to_string()]),
215 }
216}
217
218pub fn init_tasks(app: &mut App, msg: &TasksInstantiateMsg, factory_addr: &Addr) -> Addr {
219 let code_id = app.store_code(contracts::croncat_tasks_contract());
220 let module_instantiate_info = ModuleInstantiateInfo {
221 code_id,
222 version: [0, 1],
223 commit_id: "commit1".to_owned(),
224 checksum: "checksum2".to_owned(),
225 changelog_url: None,
226 schema: None,
227 msg: to_binary(msg).unwrap(),
228 contract_name: "tasks".to_owned(),
229 };
230 app.execute_contract(
231 Addr::unchecked(ALICE),
232 factory_addr.to_owned(),
233 &croncat_sdk_factory::msg::FactoryExecuteMsg::Deploy {
234 kind: VersionKind::Tasks,
235 module_instantiate_info,
236 },
237 &[],
238 )
239 .unwrap();
240
241 let metadata: ContractMetadataResponse = app
242 .wrap()
243 .query_wasm_smart(
244 factory_addr,
245 &croncat_sdk_factory::msg::FactoryQueryMsg::LatestContract {
246 contract_name: "tasks".to_owned(),
247 },
248 )
249 .unwrap();
250
251 metadata.metadata.unwrap().contract_addr
252}
253
254pub fn add_seconds_to_block(block: &mut BlockInfo, seconds: u64) {
255 block.time = block.time.plus_seconds(seconds);
256}
257
258pub fn increment_block_height(block: &mut BlockInfo, inc_value: Option<u64>) {
259 block.height += inc_value.unwrap_or(1);
260}