croncat-tasks 1.0.4

CronCat tasks smart contract
Documentation
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
use crate::ContractError::InvalidZeroValue;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
    to_binary, Attribute, Binary, Deps, DepsMut, Env, MessageInfo, Order, Response, StdResult,
    Uint64,
};
use croncat_sdk_core::internal_messages::agents::AgentOnTaskCreated;
use croncat_sdk_core::internal_messages::manager::{ManagerCreateTaskBalance, ManagerRemoveTask};
use croncat_sdk_core::internal_messages::tasks::{TasksRemoveTaskByManager, TasksRescheduleTask};
use croncat_sdk_core::types::{DEFAULT_PAGINATION_FROM_INDEX, DEFAULT_PAGINATION_LIMIT};
use croncat_sdk_tasks::msg::UpdateConfigMsg;
use croncat_sdk_tasks::types::{
    Config, CurrentTaskInfoResponse, Interval, SlotHashesResponse, SlotIdsResponse,
    SlotTasksTotalResponse, SlotType, Task, TaskExecutionInfo, TaskInfo, TaskRequest, TaskResponse,
};
use cw2::set_contract_version;
use cw20::Cw20CoinVerified;
use cw_storage_plus::Bound;

use crate::error::ContractError;
use crate::helpers::{
    check_if_sender_is_manager, get_agents_addr, get_manager_addr, remove_task, validate_boundary,
    validate_msg_calculate_usage, validate_queries, validate_transforms,
};
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{
    tasks_map, BLOCK_SLOTS, CONFIG, EVENTED_TASKS_LOOKUP, LAST_TASK_CREATION, PAUSED, TASKS_TOTAL,
    TIME_SLOTS,
};

const CONTRACT_NAME: &str = "crate:croncat-tasks";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

// Default value based on non-wasm operations, wasm ops seem impossible to predict
// TODO: this values based of pre-split, need to recalculate GAS_BASE_FEE
pub(crate) const GAS_BASE_FEE: u64 = 300_000;
pub(crate) const GAS_ACTION_FEE: u64 = 130_000;
pub(crate) const GAS_QUERY_FEE: u64 = 130_000; // Load query module(~61_000) and query after that(~65_000+)
pub(crate) const GAS_LIMIT: u64 = 3_000_000; // 10M is default for juno, but let's make sure we have space for block inclusivity guarantees
pub(crate) const SLOT_GRANULARITY_TIME: u64 = 10_000_000_000; // 10 seconds

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
    deps: DepsMut,
    _env: Env,
    info: MessageInfo,
    msg: InstantiateMsg,
) -> Result<Response, ContractError> {
    let InstantiateMsg {
        chain_name,
        version,
        pause_admin,
        croncat_manager_key,
        croncat_agents_key,
        slot_granularity_time,
        gas_base_fee,
        gas_action_fee,
        gas_limit,
        gas_query_fee,
    } = msg;

    validate_non_zero_value(slot_granularity_time, "slot_granularity_time")?;

    let contract_version = version.unwrap_or_else(|| CONTRACT_VERSION.to_string());
    set_contract_version(deps.storage, CONTRACT_NAME, &contract_version)?;

    let owner_addr = info.sender.clone();

    // Validate pause_admin
    // MUST: only be contract address
    // MUST: not be same address as factory owner (DAO)
    // Any factory action should be done by the owner_addr
    // NOTE: different networks have diff bech32 prefix lengths. Capturing min/max here
    if !(61usize..=74usize).contains(&pause_admin.to_string().len()) {
        return Err(ContractError::InvalidPauseAdmin {});
    }

    let config = Config {
        chain_name,
        version: contract_version,
        owner_addr,
        pause_admin,
        croncat_factory_addr: info.sender,
        croncat_manager_key,
        croncat_agents_key,
        slot_granularity_time: slot_granularity_time.unwrap_or(SLOT_GRANULARITY_TIME),
        gas_base_fee: gas_base_fee.unwrap_or(GAS_BASE_FEE),
        gas_action_fee: gas_action_fee.unwrap_or(GAS_ACTION_FEE),
        gas_query_fee: gas_query_fee.unwrap_or(GAS_QUERY_FEE),
        gas_limit: gas_limit.unwrap_or(GAS_LIMIT),
    };

    // Ensure the new gas limit will work
    validate_gas_limit(&config)?;

    // Save initializing states
    CONFIG.save(deps.storage, &config)?;
    PAUSED.save(deps.storage, &false)?;
    TASKS_TOTAL.save(deps.storage, &0)?;
    Ok(Response::new().add_attribute("action", "instantiate"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    msg: ExecuteMsg,
) -> Result<Response, ContractError> {
    match msg {
        ExecuteMsg::UpdateConfig(msg) => execute_update_config(deps, info, msg),
        ExecuteMsg::CreateTask { task } => execute_create_task(deps, env, info, *task),
        ExecuteMsg::RemoveTask { task_hash } => execute_remove_task(deps, info, task_hash),
        // Methods for other contracts
        ExecuteMsg::RemoveTaskByManager(remove_task_msg) => {
            execute_remove_task_by_manager(deps, info, remove_task_msg)
        }
        ExecuteMsg::RescheduleTask(reschedule_msg) => {
            execute_reschedule_task(deps, env, info, reschedule_msg)
        }
        ExecuteMsg::PauseContract {} => execute_pause(deps, info),
        ExecuteMsg::UnpauseContract {} => execute_unpause(deps, info),
    }
}

fn execute_update_config(
    deps: DepsMut,
    info: MessageInfo,
    msg: UpdateConfigMsg,
) -> Result<Response, ContractError> {
    let config = CONFIG.load(deps.storage)?;

    if info.sender != config.owner_addr {
        return Err(ContractError::Unauthorized {});
    }

    // Destruct so we won't forget to update if if new fields added
    let UpdateConfigMsg {
        croncat_factory_addr,
        croncat_manager_key,
        croncat_agents_key,
        slot_granularity_time,
        gas_base_fee,
        gas_action_fee,
        gas_query_fee,
        gas_limit,
    } = msg;

    let new_config = Config {
        owner_addr: config.owner_addr,
        pause_admin: config.pause_admin,
        croncat_factory_addr: croncat_factory_addr
            .map(|human| deps.api.addr_validate(&human))
            .transpose()?
            .unwrap_or(config.croncat_factory_addr),
        chain_name: config.chain_name,
        version: config.version,
        croncat_manager_key: croncat_manager_key.unwrap_or(config.croncat_manager_key),
        croncat_agents_key: croncat_agents_key.unwrap_or(config.croncat_agents_key),
        slot_granularity_time: slot_granularity_time.unwrap_or(config.slot_granularity_time),
        gas_base_fee: gas_base_fee.unwrap_or(config.gas_base_fee),
        gas_action_fee: gas_action_fee.unwrap_or(config.gas_action_fee),
        gas_query_fee: gas_query_fee.unwrap_or(config.gas_query_fee),
        gas_limit: gas_limit.unwrap_or(config.gas_limit),
    };

    // Ensure the new gas limit will work
    validate_gas_limit(&new_config)?;
    validate_non_zero_value(slot_granularity_time, "slot_granularity_time")?;

    CONFIG.save(deps.storage, &new_config)?;
    Ok(Response::new().add_attribute("action", "update_config"))
}

fn execute_reschedule_task(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    reschedule_msg: TasksRescheduleTask,
) -> Result<Response, ContractError> {
    let task_hash = reschedule_msg.task_hash;
    let config = CONFIG.load(deps.storage)?;
    check_if_sender_is_manager(&deps.querier, &config, &info.sender)?;

    let mut task_to_remove = None;
    let mut res_attributes: Vec<Attribute> = vec![];

    // Check default map
    let (next_id, slot_kind) = if let Some(task) = tasks_map().may_load(deps.storage, &task_hash)? {
        let (next_id, slot_kind) =
            task.interval
                .next(&env, &task.boundary, config.slot_granularity_time);

        res_attributes.push(Attribute::new(
            "task_hash",
            task.to_hash(&config.chain_name),
        ));
        res_attributes.push(Attribute::new("task_version", task.version.to_owned()));

        // NOTE: If task is evented, we dont want to "schedule" inside slots
        // but we also dont want to remove unless it was Interval::Once
        if next_id != 0 && !task.is_evented() && task.interval != Interval::Once {
            res_attributes.push(Attribute::new("action", "reschedule_task"));
            // Get previous task hashes in slot, add as needed
            let update_vec_data = |d: Option<Vec<Vec<u8>>>| -> StdResult<Vec<Vec<u8>>> {
                match d {
                    // has some data, simply push new hash
                    Some(data) => {
                        let mut s = data;
                        s.push(task_hash);
                        Ok(s)
                    }
                    // No data, push new vec & hash
                    None => Ok(vec![task_hash]),
                }
            };
            // Based on slot kind, put into block or cron slots
            match slot_kind {
                SlotType::Block => {
                    BLOCK_SLOTS.update(deps.storage, next_id, update_vec_data)?;
                    // Don't forget to pop finished task
                    let mut block_slot: Vec<(u64, Vec<Vec<u8>>)> = BLOCK_SLOTS
                        .range(
                            deps.storage,
                            None,
                            Some(Bound::inclusive(env.block.height)),
                            Order::Ascending,
                        )
                        .take(1)
                        .collect::<StdResult<_>>()?;
                    let mut slot = block_slot.pop().unwrap();
                    slot.1.pop();
                    if slot.1.is_empty() {
                        BLOCK_SLOTS.remove(deps.storage, slot.0)
                    } else {
                        BLOCK_SLOTS.save(deps.storage, slot.0, &slot.1)?;
                    }
                }
                SlotType::Cron => {
                    TIME_SLOTS.update(deps.storage, next_id, update_vec_data)?;
                    // Don't forget to pop finished task
                    let mut time_slot: Vec<(u64, Vec<Vec<u8>>)> = TIME_SLOTS
                        .range(
                            deps.storage,
                            None,
                            Some(Bound::inclusive(env.block.time.nanos())),
                            Order::Ascending,
                        )
                        .take(1)
                        .collect::<StdResult<_>>()?;
                    let mut slot = time_slot.pop().unwrap();
                    slot.1.pop();
                    if slot.1.is_empty() {
                        TIME_SLOTS.remove(deps.storage, slot.0)
                    } else {
                        TIME_SLOTS.save(deps.storage, slot.0, &slot.1)?;
                    }
                }
            }
        } else if !task.is_evented() {
            remove_task(
                deps.storage,
                &task_hash,
                task.boundary.is_block(),
                task.is_evented(),
            )?;
            task_to_remove = Some(ManagerRemoveTask {
                sender: task.owner_addr,
                task_hash,
            });
            res_attributes.push(Attribute::new("action", "remove_task"));
        } else if task.is_evented() {
            // Seems like overkill but super nice to know we didn't remove an evented task
            res_attributes.push(Attribute::new("action", "continue_task"));
        }
        (next_id, slot_kind)
    } else {
        return Err(ContractError::NoTaskFound {});
    };

    Ok(Response::new()
        .add_attributes(res_attributes)
        .add_attribute("slot_id", next_id.to_string())
        .add_attribute("slot_kind", slot_kind.to_string())
        .set_data(to_binary(&task_to_remove)?))
}

fn execute_remove_task_by_manager(
    deps: DepsMut,
    info: MessageInfo,
    remove_task_msg: TasksRemoveTaskByManager,
) -> Result<Response, ContractError> {
    let task_hash = remove_task_msg.task_hash;
    let config = CONFIG.load(deps.storage)?;
    check_if_sender_is_manager(&deps.querier, &config, &info.sender)?;

    if let Some(task) = tasks_map().may_load(deps.storage, &task_hash)? {
        remove_task(
            deps.storage,
            &task_hash,
            task.boundary.is_block(),
            task.is_evented(),
        )?;
    } else {
        return Err(ContractError::NoTaskFound {});
    }

    Ok(Response::new().add_attribute("action", "remove_task_by_manager"))
}

fn execute_remove_task(
    deps: DepsMut,
    info: MessageInfo,
    task_hash: String,
) -> Result<Response, ContractError> {
    if PAUSED.load(deps.storage)? {
        return Err(ContractError::ContractPaused);
    }
    let config = CONFIG.load(deps.storage)?;
    let hash = task_hash.as_bytes();
    if let Some(task) = tasks_map().may_load(deps.storage, hash)? {
        if task.owner_addr != info.sender {
            return Err(ContractError::Unauthorized {});
        }
        remove_task(
            deps.storage,
            hash,
            task.boundary.is_block(),
            task.is_evented(),
        )?;
    } else {
        return Err(ContractError::NoTaskFound {});
    }
    let manager_addr = get_manager_addr(&deps.querier, &config)?;
    let remove_task_msg = ManagerRemoveTask {
        sender: info.sender,
        task_hash: task_hash.into_bytes(),
    }
    .into_cosmos_msg(manager_addr)?;
    Ok(Response::new()
        .add_attribute("action", "remove_task")
        .add_message(remove_task_msg))
}

fn execute_create_task(
    deps: DepsMut,
    env: Env,
    info: MessageInfo,
    task: TaskRequest,
) -> Result<Response, ContractError> {
    if PAUSED.load(deps.storage)? {
        return Err(ContractError::ContractPaused);
    }
    let config = CONFIG.load(deps.storage)?;
    let owner_addr = info.sender;

    // Validate boundary and interval
    let boundary = validate_boundary(&env.block, task.boundary.clone(), &task.interval)?;

    let amount_for_one_task = validate_msg_calculate_usage(
        deps.as_ref(),
        &task,
        &env.contract.address,
        &owner_addr,
        &config,
    )?;
    if amount_for_one_task.gas > config.gas_limit {
        return Err(ContractError::InvalidGas {});
    }
    let cw20 = task
        .cw20
        .map(|human| {
            StdResult::Ok(Cw20CoinVerified {
                address: deps.api.addr_validate(&human.address)?,
                amount: human.amount,
            })
        })
        .transpose()?;

    let item = Task {
        owner_addr: owner_addr.clone(),
        interval: task.interval,
        boundary,
        stop_on_fail: task.stop_on_fail,
        amount_for_one_task: amount_for_one_task.clone(),
        actions: task.actions,
        // NOTE: See process_queries in manager contract for details on limitations of malformed queries
        queries: task.queries.unwrap_or_default(),
        transforms: task.transforms.unwrap_or_default(),
        version: config.version.clone(),
    };
    if !item.interval.is_valid() {
        return Err(ContractError::InvalidInterval {});
    }
    if !validate_queries(&deps.as_ref(), &item.queries) {
        return Err(ContractError::InvalidQueries {});
    }
    if !validate_transforms(&item) {
        return Err(ContractError::InvalidTransform {});
    }

    let hash_prefix = &config.chain_name;
    let hash = item.to_hash(hash_prefix);

    let (next_id, slot_kind) =
        item.interval
            .next(&env, &item.boundary, config.slot_granularity_time);
    if next_id == 0 {
        return Err(ContractError::TaskEnded {});
    }

    let recurring = item.recurring();
    let hash_vec = hash.clone().into_bytes();
    let mut attributes: Vec<Attribute> = vec![];

    // Update query totals and map
    TASKS_TOTAL.update(deps.storage, |amt| -> StdResult<_> { Ok(amt + 1) })?;
    tasks_map().update(deps.storage, &hash_vec, |old| match old {
        Some(_) => Err(ContractError::TaskExists {}),
        None => Ok(item.clone()),
    })?;

    // Get previous task hashes in slot, add as needed
    let update_vec_data = |d: Option<Vec<Vec<u8>>>| -> StdResult<Vec<Vec<u8>>> {
        match d {
            // has some data, simply push new hash
            Some(data) => {
                let mut s = data;
                s.push(hash_vec.clone());
                Ok(s)
            }
            // No data, push new vec & hash
            None => Ok(vec![hash_vec.clone()]),
        }
    };

    if item.is_evented() {
        EVENTED_TASKS_LOOKUP.update(deps.storage, next_id, update_vec_data)?;
        attributes.push(Attribute::new("evented_id", next_id.to_string()));
    } else {
        // Only scheduled tasks get put into slots
        match slot_kind {
            SlotType::Block => {
                BLOCK_SLOTS.update(deps.storage, next_id, update_vec_data)?;
            }
            SlotType::Cron => {
                TIME_SLOTS.update(deps.storage, next_id, update_vec_data)?;
            }
        }
        attributes.push(Attribute::new("slot_id", next_id.to_string()));
        attributes.push(Attribute::new("slot_kind", slot_kind.to_string()));
    }

    // Save the current timestamp as the last time a task was created
    LAST_TASK_CREATION.save(deps.storage, &env.block.time)?;

    let manager_addr = get_manager_addr(&deps.querier, &config)?;
    let manager_create_task_balance_msg = ManagerCreateTaskBalance {
        sender: owner_addr,
        task_hash: hash_vec,
        recurring,
        cw20,
        amount_for_one_task,
    }
    .into_cosmos_msg(manager_addr, info.funds)?;

    let agent_addr = get_agents_addr(&deps.querier, &config)?;
    let agent_new_task_msg = AgentOnTaskCreated {}.into_cosmos_msg(agent_addr)?;
    let response_data = TaskExecutionInfo {
        block_height: env.block.height,
        tx_info: env.transaction,
        task_hash: hash.clone(),
        owner_addr: item.owner_addr,
        amount_for_one_task: item.amount_for_one_task,
        version: item.version.clone(),
    };
    Ok(Response::new()
        .set_data(to_binary(&response_data)?)
        .add_attribute("action", "create_task")
        .add_attributes(attributes)
        .add_attribute("task_hash", hash)
        .add_attribute("task_version", item.version)
        .add_message(manager_create_task_balance_msg)
        .add_message(agent_new_task_msg))
}

pub fn execute_pause(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
    if PAUSED.load(deps.storage)? {
        return Err(ContractError::ContractPaused);
    }
    let config = CONFIG.load(deps.storage)?;
    if info.sender != config.pause_admin {
        return Err(ContractError::Unauthorized {});
    }
    PAUSED.save(deps.storage, &true)?;
    Ok(Response::new().add_attribute("action", "pause_contract"))
}

pub fn execute_unpause(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> {
    if !PAUSED.load(deps.storage)? {
        return Err(ContractError::ContractUnpaused);
    }
    let config = CONFIG.load(deps.storage)?;
    if info.sender != config.owner_addr {
        return Err(ContractError::Unauthorized {});
    }
    PAUSED.save(deps.storage, &false)?;
    Ok(Response::new().add_attribute("action", "unpause_contract"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
    match msg {
        QueryMsg::Config {} => to_binary(&CONFIG.load(deps.storage)?),
        QueryMsg::Paused {} => to_binary(&PAUSED.load(deps.storage)?),
        QueryMsg::TasksTotal {} => to_binary(&cosmwasm_std::Uint64::from(query_tasks_total(deps)?)),
        QueryMsg::CurrentTaskInfo {} => to_binary(&query_current_task_info(deps, env)?),
        QueryMsg::CurrentTask {} => to_binary(&query_current_task(deps, env)?),
        QueryMsg::Tasks { from_index, limit } => to_binary(&query_tasks(deps, from_index, limit)?),
        QueryMsg::EventedIds { from_index, limit } => {
            to_binary(&query_evented_ids(deps, from_index, limit)?)
        }
        QueryMsg::EventedHashes {
            id,
            from_index,
            limit,
        } => to_binary(&query_evented_hashes(deps, id, from_index, limit)?),
        QueryMsg::EventedTasks {
            start,
            from_index,
            limit,
        } => to_binary(&query_evented_tasks(deps, env, start, from_index, limit)?),
        QueryMsg::TasksByOwner {
            owner_addr,
            from_index,
            limit,
        } => to_binary(&query_tasks_by_owner(deps, owner_addr, from_index, limit)?),
        QueryMsg::Task { task_hash } => to_binary(&query_task(deps, task_hash)?),
        QueryMsg::TaskHash { task } => to_binary(&query_task_hash(deps, *task)?),
        QueryMsg::SlotHashes { slot } => to_binary(&query_slot_hashes(deps, slot)?),
        QueryMsg::SlotIds { from_index, limit } => {
            to_binary(&query_slot_ids(deps, from_index, limit)?)
        }
        QueryMsg::SlotTasksTotal { offset } => {
            to_binary(&query_slot_tasks_total(deps, env, offset)?)
        }
    }
}

fn query_tasks_total(deps: Deps) -> StdResult<u64> {
    TASKS_TOTAL.load(deps.storage)
}

// returns the total task count & last task creation timestamp for agent nomination checks
fn query_current_task_info(deps: Deps, _env: Env) -> StdResult<CurrentTaskInfoResponse> {
    Ok(CurrentTaskInfoResponse {
        total: Uint64::from(query_tasks_total(deps).unwrap()),
        last_created_task: LAST_TASK_CREATION.load(deps.storage)?,
    })
}

// Offset can be defined for forward looking task amounts, but will default to current block - 1
// NOTE: Subtracts 1 block so that during the current finalized block we can see the upcoming work
// and react accordingly - current task will always be 1 block ahead for signing to occur accurately.
fn query_slot_tasks_total(
    deps: Deps,
    env: Env,
    offset: Option<u64>,
) -> StdResult<SlotTasksTotalResponse> {
    if let Some(off) = offset {
        let config = CONFIG.load(deps.storage)?;
        let block_tasks = BLOCK_SLOTS
            .may_load(deps.storage, env.block.height + off)?
            .unwrap_or_default()
            .len() as u64;
        let evented_tasks = EVENTED_TASKS_LOOKUP
            .may_load(deps.storage, env.block.height + off)?
            .unwrap_or_default()
            .len() as u64;

        let current_block_ts = env.block.time.nanos();
        let current_block_slot =
            current_block_ts.saturating_sub(current_block_ts % config.slot_granularity_time);
        let cron_tasks = TIME_SLOTS
            .may_load(
                deps.storage,
                current_block_slot + config.slot_granularity_time * off,
            )?
            .unwrap_or_default()
            .len() as u64;
        Ok(SlotTasksTotalResponse {
            block_tasks,
            cron_tasks,
            evented_tasks,
        })
    } else {
        // NOTE: Using addition here since the range uses "max", which is still subtracting current block
        let block_height = env.block.height.saturating_add(1);
        // Follows the same logic as block, needs to be far enough in past to cover ready tasks
        let block_time = env.block.time.plus_seconds(6).nanos();
        let block_slots: Vec<(u64, Vec<Vec<u8>>)> = BLOCK_SLOTS
            .range(
                deps.storage,
                None,
                Some(Bound::inclusive(block_height)),
                Order::Ascending,
            )
            .collect::<StdResult<_>>()?;

        let block_tasks = block_slots
            .iter()
            .fold(0, |acc, (_, hashes)| acc + hashes.len()) as u64;

        let evented_task_list: Vec<(u64, Vec<Vec<u8>>)> = EVENTED_TASKS_LOOKUP
            .range(
                deps.storage,
                None,
                Some(Bound::inclusive(block_height)),
                Order::Ascending,
            )
            .collect::<StdResult<_>>()?;

        let evented_tasks = evented_task_list
            .iter()
            .fold(0, |acc, (_, hashes)| acc + hashes.len()) as u64;

        let time_slot: Vec<(u64, Vec<Vec<u8>>)> = TIME_SLOTS
            .range(
                deps.storage,
                None,
                Some(Bound::inclusive(block_time)),
                Order::Ascending,
            )
            .collect::<StdResult<_>>()?;

        let cron_tasks = time_slot
            .iter()
            .fold(0, |acc, (_, hashes)| acc + hashes.len()) as u64;
        Ok(SlotTasksTotalResponse {
            block_tasks,
            cron_tasks,
            evented_tasks,
        })
    }
}

/// Get the slot with lowest height/timestamp
/// NOTE: This prioritizes blocks over timestamps
/// Why blocks over timestamps? Time-based tasks have a wider granularity than blocks.
/// For example, the default configuration for time granularity is 10 seconds. This means
/// on average a time-based task could occur on 1 of 2 blocks. This configuration is aimed
/// at a larger window of time because timestamp guarantees are virtually impossible, without
/// protocol level mechanisms. Since this is the case, timestamps should be considered more
/// flexible in execution windows. Future versions of this contract can ensure better
/// execution guarantees, based on whether the timestamp is nearing the end of its block-span
/// window (timestamp end is closer to 2nd block than 1st).
fn query_current_task(deps: Deps, env: Env) -> StdResult<TaskResponse> {
    let config = CONFIG.load(deps.storage)?;
    let mut block_slot: Vec<(u64, Vec<Vec<u8>>)> = BLOCK_SLOTS
        .range(
            deps.storage,
            None,
            // NOTE: Remove 1 block so that during the current finalized block we can see the upcoming work
            // and react accordingly - current task will always be 1 block ahead for signing to occur accurately.
            Some(Bound::inclusive(env.block.height.saturating_add(1))),
            Order::Ascending,
        )
        .take(1)
        .collect::<StdResult<_>>()?;
    if !block_slot.is_empty() {
        let task_hash = block_slot.pop().unwrap().1.pop().unwrap();
        let task = tasks_map().load(deps.storage, &task_hash)?;
        Ok(task.into_response(&config.chain_name))
    } else {
        let mut time_slot: Vec<(u64, Vec<Vec<u8>>)> = TIME_SLOTS
            .range(
                deps.storage,
                None,
                Some(Bound::inclusive(env.block.time.plus_nanos(6).nanos())),
                Order::Ascending,
            )
            .take(1)
            .collect::<StdResult<_>>()?;
        if !time_slot.is_empty() {
            let task_hash = time_slot.pop().unwrap().1.pop().unwrap();
            let task = tasks_map().load(deps.storage, &task_hash)?;
            Ok(task.into_response(&config.chain_name))
        } else {
            Ok(TaskResponse { task: None })
        }
    }
}

fn query_tasks(
    deps: Deps,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<Vec<TaskInfo>> {
    let config = CONFIG.load(deps.storage)?;

    let from_index = from_index.unwrap_or_default();
    let limit = limit.unwrap_or(100);

    tasks_map()
        .range(deps.storage, None, None, Order::Ascending)
        .skip(from_index as usize)
        .take(limit as usize)
        .map(|task_res| {
            task_res.map(|(_, task)| task.into_response(&config.chain_name).task.unwrap())
        })
        .collect()
}

fn query_evented_tasks(
    deps: Deps,
    env: Env,
    start: Option<u64>,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<Vec<TaskInfo>> {
    let config = CONFIG.load(deps.storage)?;
    let from_index = from_index.unwrap_or(DEFAULT_PAGINATION_FROM_INDEX);
    let limit = limit.unwrap_or(DEFAULT_PAGINATION_LIMIT);
    let tm = tasks_map();

    let mut evented_hashes: Vec<Vec<u8>> = Vec::new();
    let mut all_tasks: Vec<TaskInfo> = Vec::new();

    // Check if start was supplied, otherwise get the next ids for block and time
    if let Some(i) = start {
        evented_hashes = EVENTED_TASKS_LOOKUP
            .may_load(deps.storage, i)?
            .unwrap_or_default();
    } else {
        let block_evented: Vec<(u64, _)> = EVENTED_TASKS_LOOKUP
            .range(
                deps.storage,
                Some(Bound::inclusive(env.block.height)),
                None,
                Order::Ascending,
            )
            .skip(from_index as usize)
            .take(limit as usize)
            .collect::<StdResult<Vec<(u64, _)>>>()?;

        if !block_evented.is_empty() {
            for item in block_evented {
                evented_hashes = [evented_hashes, item.1].concat();
            }
        }

        let time_evented: Vec<(u64, _)> = EVENTED_TASKS_LOOKUP
            .range(
                deps.storage,
                Some(Bound::inclusive(env.block.time.nanos())),
                None,
                Order::Ascending,
            )
            .take(1)
            .collect::<StdResult<Vec<(u64, _)>>>()?;

        if !time_evented.is_empty() {
            for item in time_evented {
                evented_hashes = [evented_hashes, item.1].concat();
            }
        }
    }

    // Loop and get all associated tasks by hash
    for t in evented_hashes {
        if let Some(task) = tm.may_load(deps.storage, &t)? {
            all_tasks.push(task.into_response(&config.chain_name).task.unwrap())
        }
    }

    Ok(all_tasks)
}

fn query_evented_ids(
    deps: Deps,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<Vec<u64>> {
    let from_index = from_index.unwrap_or(DEFAULT_PAGINATION_FROM_INDEX);
    let limit = limit.unwrap_or(DEFAULT_PAGINATION_LIMIT);

    let evented_ids = EVENTED_TASKS_LOOKUP
        .keys(deps.storage, None, None, Order::Ascending)
        .skip(from_index as usize)
        .take(limit as usize)
        .collect::<StdResult<_>>()?;

    Ok(evented_ids)
}

fn query_evented_hashes(
    deps: Deps,
    id: Option<u64>,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<Vec<String>> {
    let mut evented_hashes: Vec<Vec<u8>> = Vec::new();
    let from_index = from_index.unwrap_or(DEFAULT_PAGINATION_FROM_INDEX);
    let limit = limit.unwrap_or(DEFAULT_PAGINATION_LIMIT);

    // Check if slot was supplied, otherwise get the next slots for block and time
    if let Some(i) = id {
        evented_hashes = EVENTED_TASKS_LOOKUP
            .may_load(deps.storage, i)?
            .unwrap_or_default();
    } else {
        let evented: Vec<(u64, _)> = EVENTED_TASKS_LOOKUP
            .range(deps.storage, None, None, Order::Ascending)
            .skip(from_index as usize)
            .take(limit as usize)
            .collect::<StdResult<Vec<(u64, _)>>>()?;

        if !evented.is_empty() {
            for item in evented {
                evented_hashes = [evented_hashes, item.1].concat();
            }
        }
    }

    // Generate strings for all hashes
    let evented_task_hashes: Vec<_> = evented_hashes
        .iter()
        .map(|t| String::from_utf8(t.to_vec()).unwrap_or_else(|_| "".to_string()))
        .collect();

    Ok(evented_task_hashes)
}

fn query_tasks_by_owner(
    deps: Deps,
    owner_addr: String,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<Vec<TaskInfo>> {
    let owner_addr = deps.api.addr_validate(&owner_addr)?;
    let config = CONFIG.load(deps.storage)?;

    let from_index = from_index.unwrap_or_default();
    let limit = limit.unwrap_or(100);

    tasks_map()
        .idx
        .owner
        .prefix(owner_addr)
        .range(deps.storage, None, None, Order::Ascending)
        .skip(from_index as usize)
        .take(limit as usize)
        .map(|task_res| {
            task_res.map(|(_, task)| task.into_response(&config.chain_name).task.unwrap())
        })
        .collect()
}

fn query_task(deps: Deps, task_hash: String) -> StdResult<TaskResponse> {
    let config = CONFIG.load(deps.storage)?;

    if let Some(task) = tasks_map().may_load(deps.storage, task_hash.as_bytes())? {
        Ok(task.into_response(&config.chain_name))
    } else {
        Ok(TaskResponse { task: None })
    }
}

fn query_task_hash(deps: Deps, task: Task) -> StdResult<String> {
    let config = CONFIG.load(deps.storage)?;
    Ok(task.to_hash(&config.chain_name))
}

fn query_slot_hashes(deps: Deps, slot: Option<u64>) -> StdResult<SlotHashesResponse> {
    let mut block_id: u64 = 0;
    let mut block_hashes: Vec<Vec<u8>> = Vec::new();
    let mut time_id: u64 = 0;
    let mut time_hashes: Vec<Vec<u8>> = Vec::new();

    // Check if slot was supplied, otherwise get the next slots for block and time
    if let Some(id) = slot {
        block_hashes = BLOCK_SLOTS.may_load(deps.storage, id)?.unwrap_or_default();
        if !block_hashes.is_empty() {
            block_id = id;
        }
        time_hashes = TIME_SLOTS.may_load(deps.storage, id)?.unwrap_or_default();
        if !time_hashes.is_empty() {
            time_id = id;
        }
    } else {
        let time: Vec<(u64, _)> = TIME_SLOTS
            .range(deps.storage, None, None, Order::Ascending)
            .take(1)
            .collect::<StdResult<Vec<(u64, _)>>>()?;

        if !time.is_empty() {
            let slot = time[0].clone();
            time_id = slot.0;
            time_hashes = slot.1;
        }

        let block: Vec<(u64, _)> = BLOCK_SLOTS
            .range(deps.storage, None, None, Order::Ascending)
            .take(1)
            .collect::<StdResult<Vec<(u64, _)>>>()?;

        if !block.is_empty() {
            let slot = block[0].clone();
            block_id = slot.0;
            block_hashes = slot.1;
        }
    }

    // Generate strings for all hashes
    let block_task_hash: Vec<_> = block_hashes
        .iter()
        .map(|b| String::from_utf8(b.to_vec()).unwrap_or_else(|_| "".to_string()))
        .collect();
    let time_task_hash: Vec<_> = time_hashes
        .iter()
        .map(|t| String::from_utf8(t.to_vec()).unwrap_or_else(|_| "".to_string()))
        .collect();

    Ok(SlotHashesResponse {
        block_id,
        block_task_hash,
        time_id,
        time_task_hash,
    })
}

fn query_slot_ids(
    deps: Deps,
    from_index: Option<u64>,
    limit: Option<u64>,
) -> StdResult<SlotIdsResponse> {
    let from_index = from_index.unwrap_or_default();
    let limit = limit.unwrap_or(100);

    let time_ids = TIME_SLOTS
        .keys(deps.storage, None, None, Order::Ascending)
        .skip(from_index as usize)
        .take(limit as usize)
        .collect::<StdResult<_>>()?;
    let block_ids = BLOCK_SLOTS
        .keys(deps.storage, None, None, Order::Ascending)
        .skip(from_index as usize)
        .take(limit as usize)
        .collect::<StdResult<_>>()?;

    Ok(SlotIdsResponse {
        time_ids,
        block_ids,
    })
}

fn validate_non_zero_value(opt_num: Option<u64>, field_name: &str) -> Result<(), ContractError> {
    if let Some(num) = opt_num {
        if num == 0u64 {
            Err(InvalidZeroValue {
                field: field_name.to_string(),
            })
        } else {
            Ok(())
        }
    } else {
        Ok(())
    }
}

/// Before changing the configuration's gas_limit, ensure it's a reasonable
/// value, meaning higher than the sum of its needed parts
fn validate_gas_limit(config: &Config) -> Result<(), ContractError> {
    if config.gas_limit > config.gas_base_fee + config.gas_action_fee + config.gas_query_fee {
        Ok(())
    } else {
        // Must be greater than those params
        Err(ContractError::InvalidGas {})
    }
}