1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
use crate::error::CoreError;
use crate::traits::Intervals;
use crate::types::{
    Action, AgentStatus, Boundary, BoundaryValidated, GasPrice, GenericBalance, Interval, Task,
    Transform,
};
use crate::types::{Agent, SlotType};
use cosmwasm_std::{Addr, Coin, Timestamp, Uint64};
use cw20::{Balance, Cw20Coin, Cw20CoinVerified};
use cw_rules_core::types::CroncatQuery;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

// NOTE: Which version is more practical?
// // Exporting a nice schema
// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
// pub enum Croncat {
//     Agent(Agent),
//     Task(Task),
//     ConfigResponse(GetConfigResponse),
//     BalancesResponse(GetBalancesResponse),
//     GetAgentIdsResponse(GetAgentIdsResponse),
//     GetAgentTasksResponse(GetAgentTasksResponse),
//     TaskRequest(TaskRequest),
//     TaskResponse(TaskResponse),
//     ValidateIntervalResponse(ValidateIntervalResponse),
//     GetAgentResponse(GetAgentResponse),
//     GetTasksResponse(GetTasksResponse),
//     GetTasksByOwnerResponse(GetTasksByOwnerResponse),
//     GetTaskResponse(GetTaskResponse),
//     GetTaskHashResponse(GetTaskHashResponse),
//     GetSlotHashesResponse(GetSlotHashesResponse),
//     GetSlotIdsResponse(GetSlotIdsResponse),
// }

// Exporting a nice schema
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "PascalCase")]
pub struct Croncat {
    pub(crate) agent: Option<Agent>,
    pub(crate) task: Option<Task>,
    pub(crate) config_response: Option<GetConfigResponse>,
    pub(crate) balance_response: Option<GetBalancesResponse>,
    pub(crate) get_agent_ids_response: Option<GetAgentIdsResponse>,
    pub(crate) get_agent_tasks_response: Option<AgentTaskResponse>,
    pub(crate) task_request: Option<TaskRequest>,
    pub(crate) task_response: Option<TaskResponse>,
    pub(crate) validate_interval_response: Option<bool>,
    pub(crate) get_agent_response: Option<Option<AgentResponse>>,
    pub(crate) get_tasks_response: Option<Vec<TaskResponse>>,
    pub(crate) get_tasks_by_owner_response: Option<Vec<TaskResponse>>,
    pub(crate) get_task_response: Option<Option<TaskResponse>>,
    pub(crate) get_task_hash_response: Option<String>,
    pub(crate) get_slot_hashes_response: Option<GetSlotHashesResponse>,
    pub(crate) get_slot_ids_response: Option<GetSlotIdsResponse>,
    pub(crate) get_wallet_balances_response: Option<GetWalletBalancesResponse>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {
    // TODO: Submit issue for AppBuilder tests not working for -- deps.querier.query_bonded_denom()?;
    pub denom: String,
    pub cw_rules_addr: String,
    pub owner_id: Option<String>,
    pub gas_base_fee: Option<Uint64>,
    pub gas_action_fee: Option<Uint64>,
    pub gas_query_fee: Option<Uint64>,
    pub gas_wasm_query_fee: Option<Uint64>,
    pub gas_price: Option<GasPrice>,
    pub agent_nomination_duration: Option<u16>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
    UpdateSettings {
        owner_id: Option<String>,
        slot_granularity_time: Option<u64>,
        paused: Option<bool>,
        agent_fee: Option<u64>,
        gas_base_fee: Option<Uint64>,
        gas_action_fee: Option<Uint64>,
        gas_query_fee: Option<Uint64>,
        gas_wasm_query_fee: Option<Uint64>,
        gas_price: Option<GasPrice>,
        proxy_callback_gas: Option<u32>,
        min_tasks_per_agent: Option<u64>,
        agents_eject_threshold: Option<u64>,
        // treasury_id: Option<String>,
    },
    MoveBalances {
        balances: Vec<Balance>,
        account_id: String,
    },

    RegisterAgent {
        payable_account_id: Option<String>,
    },
    UpdateAgent {
        payable_account_id: String,
    },
    CheckInAgent {},
    UnregisterAgent {
        from_behind: Option<bool>,
    },
    WithdrawReward {},

    CreateTask {
        task: TaskRequest,
    },
    RemoveTask {
        task_hash: String,
    },
    RefillTaskBalance {
        task_hash: String,
    },
    RefillTaskCw20Balance {
        task_hash: String,
        cw20_coins: Vec<Cw20Coin>,
    },
    ProxyCall {
        task_hash: Option<String>,
    },
    /// Receive cw20 token
    Receive(cw20::Cw20ReceiveMsg),
    WithdrawWalletBalance {
        cw20_amounts: Vec<Cw20Coin>,
    },
    Tick {},
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
    GetConfig {},
    GetBalances {},
    GetAgent {
        account_id: String,
    },
    GetAgentIds {},
    GetAgentTasks {
        account_id: String,
    },
    GetTasks {
        from_index: Option<u64>,
        limit: Option<u64>,
    },
    GetTasksWithQueries {
        from_index: Option<u64>,
        limit: Option<u64>,
    },
    GetTasksByOwner {
        owner_id: String,
    },
    GetTask {
        task_hash: String,
    },
    GetTaskHash {
        task: Box<Task>,
    },
    ValidateInterval {
        interval: Interval,
    },
    GetSlotHashes {
        slot: Option<u64>,
    },
    GetSlotIds {},
    GetWalletBalances {
        wallet: String,
    },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct AgentTaskResponse {
    pub num_block_tasks: Uint64,
    pub num_block_tasks_extra: Uint64,
    pub num_cron_tasks: Uint64,
    pub num_cron_tasks_extra: Uint64,
}

impl AgentTaskResponse {
    pub fn has_any_slot_tasks(&self, slot_kind: SlotType) -> bool {
        if self.num_of_slot_tasks(slot_kind) < 1u64 {
            return false;
        }
        true
    }
    pub fn num_of_slot_tasks(&self, slot_kind: SlotType) -> u64 {
        if slot_kind == SlotType::Block {
            return self.num_block_tasks.u64();
        }

        self.num_cron_tasks.u64()
    }
    pub fn has_any_slot_extra_tasks(&self, slot_kind: SlotType) -> bool {
        if self.num_of_slot_extra_tasks(slot_kind) < 1u64 {
            return false;
        }
        true
    }
    pub fn num_of_slot_extra_tasks(&self, slot_kind: SlotType) -> u64 {
        if slot_kind == SlotType::Block {
            return self.num_block_tasks_extra.u64();
        }

        self.num_cron_tasks_extra.u64()
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TaskRequest {
    pub interval: Interval,
    pub boundary: Option<Boundary>,
    pub stop_on_fail: bool,
    pub actions: Vec<Action>,
    pub queries: Option<Vec<CroncatQuery>>,
    pub transforms: Option<Vec<Transform>>,
    pub cw20_coins: Vec<Cw20Coin>,
}
pub struct TaskRequestBuilder {
    interval: Interval,
    boundary: Option<Boundary>,
    stop_on_fail: bool,
    actions: Option<Vec<Action>>,
    queries: Option<Option<Vec<CroncatQuery>>>,
    transforms: Option<Option<Vec<Transform>>>,
    cw20_coins: Option<Vec<Cw20Coin>>,
}
#[allow(dead_code)]
impl TaskRequestBuilder {
    pub fn new() -> Self {
        Self {
            interval: Interval::Once,
            boundary: None,
            stop_on_fail: false,
            actions: None,
            queries: None,
            transforms: None,
            cw20_coins: None,
        }
    }
    pub fn with_interval(&mut self, interval: Interval) -> &mut Self {
        self.interval = interval;
        self
    }
    pub fn once(&mut self) -> &mut Self {
        self.with_interval(Interval::Once)
    }
    pub fn block(&mut self, block_inerval: u64) -> &mut Self {
        self.with_interval(Interval::Block(block_inerval))
    }
    pub fn cron(&mut self, crontab: String) -> &mut Self {
        self.with_interval(Interval::Cron(crontab))
    }
    pub fn immediate(&mut self) -> &mut Self {
        self.with_interval(Interval::Immediate)
    }
    pub fn with_boundary(&mut self, boundary: Boundary) -> &mut Self {
        self.boundary = Some(boundary);
        self
    }
    pub fn with_time_boundary(&mut self, start: Timestamp, end: Timestamp) -> &mut Self {
        self.with_boundary(Boundary::Time {
            start: Some(start),
            end: Some(end),
        })
    }
    pub fn with_height_boundary(&mut self, start: u64, end: u64) -> &mut Self {
        self.with_boundary(Boundary::Height {
            start: Some(Uint64::new(start)),
            end: Some(Uint64::new(end)),
        })
    }
    pub fn should_stop_on_fail(&mut self, stop_on_fail: bool) -> &mut Self {
        self.stop_on_fail = stop_on_fail;
        self
    }

    pub fn with_action(&mut self, action: Action) -> &mut Self {
        self.actions = Some(vec![action]);
        self
    }
    pub fn with_actions(&mut self, actions: Vec<Action>) -> &mut Self {
        self.actions = Some(actions);
        self
    }
    pub fn with_query(&mut self, query: CroncatQuery) -> &mut Self {
        self.queries = Some(Some(vec![query]));
        self
    }
    pub fn with_queries(&mut self, queries: Vec<CroncatQuery>) -> &mut Self {
        self.queries = Some(Some(queries));
        self
    }
    pub fn with_transform(&mut self, transform: Transform) -> &mut Self {
        self.transforms = Some(Some(vec![transform]));
        self
    }
    fn with_transforms(&mut self, transforms: Vec<Transform>) -> &mut Self {
        self.transforms = Some(Some(transforms));
        self
    }
    fn with_cw20(&mut self, cw20: Cw20Coin) -> &mut Self {
        self.cw20_coins = Some(vec![cw20]);
        self
    }
    pub fn with_cw20s(&mut self, cw20s: Vec<Cw20Coin>) -> &mut Self {
        self.cw20_coins = Some(cw20s);
        self
    }
    pub fn build(&self) -> Result<TaskRequest, CoreError> {
        if !self.interval.is_valid() {
            return Err(CoreError::InvalidInterval {});
        }
        BoundaryValidated::validate_boundary(self.boundary, &self.interval)?;

        Ok(TaskRequest {
            interval: self.interval.clone(),
            boundary: self.boundary,
            stop_on_fail: self.stop_on_fail,
            actions: self.actions.clone().unwrap_or_default(),
            queries: self.queries.clone().unwrap_or_default(),
            transforms: self.transforms.clone().unwrap_or_default(),
            cw20_coins: self.cw20_coins.clone().unwrap_or_default(),
        })
    }
}
impl Default for TaskRequestBuilder {
    fn default() -> Self {
        Self::new()
    }
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TaskResponse {
    pub task_hash: String,

    pub owner_id: Addr,

    pub interval: Interval,
    pub boundary: Option<Boundary>,

    pub stop_on_fail: bool,
    pub total_deposit: Vec<Coin>,
    pub total_cw20_deposit: Vec<Cw20CoinVerified>,
    pub amount_for_one_task_native: Vec<Coin>,
    pub amount_for_one_task_cw20: Vec<Cw20CoinVerified>,

    pub actions: Vec<Action>,
    pub queries: Option<Vec<CroncatQuery>>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct TaskWithQueriesResponse {
    pub task_hash: String,
    pub interval: Interval,
    pub boundary: Option<Boundary>,
    pub queries: Option<Vec<CroncatQuery>>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct GetBalancesResponse {
    pub native_denom: String,
    pub available_balance: GenericBalance,
    pub staked_balance: GenericBalance,
    pub cw20_whitelist: Vec<Addr>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct GetWalletBalancesResponse {
    pub cw20_balances: Vec<Cw20CoinVerified>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct GetAgentIdsResponse {
    pub active: Vec<Addr>,
    pub pending: Vec<Addr>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct AgentResponse {
    // This field doesn't exist in the Agent struct and is the only one that differs
    pub status: AgentStatus,
    pub payable_account_id: Addr,
    pub balance: GenericBalance,
    pub total_tasks_executed: u64,
    pub last_executed_slot: u64,
    pub register_start: Timestamp,
}

impl From<Task> for TaskResponse {
    fn from(task: Task) -> Self {
        let boundary = match (task.boundary, &task.interval) {
            (
                BoundaryValidated {
                    start: None,
                    end: None,
                    is_block_boundary: None,
                },
                _,
            ) => None,
            (
                BoundaryValidated {
                    start,
                    end,
                    is_block_boundary: _,
                },
                Interval::Cron(_),
            ) => Some(Boundary::Time {
                start: start.map(Timestamp::from_nanos),
                end: end.map(Timestamp::from_nanos),
            }),
            (
                BoundaryValidated {
                    start,
                    end,
                    is_block_boundary: _,
                },
                _,
            ) => Some(Boundary::Height {
                start: start.map(Into::into),
                end: end.map(Into::into),
            }),
        };
        TaskResponse {
            task_hash: task.to_hash(),
            owner_id: task.owner_id,
            interval: task.interval,
            boundary,
            stop_on_fail: task.stop_on_fail,
            total_deposit: task.total_deposit.native,
            total_cw20_deposit: task.total_deposit.cw20,
            amount_for_one_task_native: task.amount_for_one_task.native,
            amount_for_one_task_cw20: task.amount_for_one_task.cw20,
            actions: task.actions,
            queries: task.queries,
        }
    }
}

impl From<Task> for TaskWithQueriesResponse {
    fn from(task: Task) -> Self {
        let boundary = match (task.boundary, &task.interval) {
            (
                BoundaryValidated {
                    start: None,
                    end: None,
                    is_block_boundary: None,
                },
                _,
            ) => None,
            (
                BoundaryValidated {
                    start,
                    end,
                    is_block_boundary: _,
                },
                Interval::Cron(_),
            ) => Some(Boundary::Time {
                start: start.map(Timestamp::from_nanos),
                end: end.map(Timestamp::from_nanos),
            }),
            (
                BoundaryValidated {
                    start,
                    end,
                    is_block_boundary: _,
                },
                _,
            ) => Some(Boundary::Height {
                start: start.map(Into::into),
                end: end.map(Into::into),
            }),
        };
        TaskWithQueriesResponse {
            task_hash: task.to_hash(),
            interval: task.interval,
            boundary,
            queries: task.queries,
        }
    }
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct GetSlotHashesResponse {
    pub block_id: u64,
    pub block_task_hash: Vec<String>,
    pub time_id: u64,
    pub time_task_hash: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, JsonSchema)]
pub struct GetSlotIdsResponse {
    pub time_ids: Vec<u64>,
    pub block_ids: Vec<u64>,
}

// cw_rules
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct QueryConstruct {
    pub queries: Vec<CroncatQuery>,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct GetConfigResponse {
    pub paused: bool,
    pub owner_id: Addr,
    // pub treasury_id: Option<Addr>,
    pub min_tasks_per_agent: u64,
    pub agents_eject_threshold: u64,
    pub agent_active_indices: Vec<(SlotType, u32, u32)>,
    pub agent_nomination_duration: u16,

    pub cw_rules_addr: Addr,

    pub agent_fee: u64,
    pub gas_price: GasPrice,
    pub gas_base_fee: u64,
    pub gas_action_fee: u64,
    pub proxy_callback_gas: u32,
    pub slot_granularity_time: u64,

    pub cw20_whitelist: Vec<Addr>,
    pub native_denom: String,
    pub available_balance: GenericBalance, // tasks + rewards balances
    pub staked_balance: GenericBalance, // surplus that is temporary staking (to be used in conjunction with external treasury)

    // The default amount of tasks to query
    pub limit: u64,
}