9个版本 (稳定版)

1.1.1 2023年9月25日
1.1.0 2023年6月13日
1.0.3 2023年5月30日
0.1.6-rc.12023年4月21日

#47 in 魔法豆

Download history 69/week @ 2024-04-20 422/week @ 2024-04-27 117/week @ 2024-05-04 55/week @ 2024-05-11 136/week @ 2024-05-18 64/week @ 2024-05-25 90/week @ 2024-06-01 492/week @ 2024-06-08 101/week @ 2024-06-15 131/week @ 2024-06-22 230/week @ 2024-06-29 226/week @ 2024-07-06 171/week @ 2024-07-13 166/week @ 2024-07-20 174/week @ 2024-07-27 38/week @ 2024-08-03

每月下载量613次

MIT/Apache

265KB
5.5K SLoC

CronCat集成工具

此仓库包含为使用CronCat任务添加自动化到智能合约的项目提供的辅助方法。

Crate: https://crates.io/crates/croncat-integration-utils Rust文档: https://docs.rs/croncat-integration-utils/latest/croncat_integration_utils

使用此crate的示例: https://github.com/CronCats/cw-purrbox

任务创建

  • get_latest_croncat_contract — 提供CronCat工厂地址和合约名称(例如"tasks"),将返回最新合约的地址,或抛出自定义错误。
  • create_croncat_task_submessage — 这可能是一个dApps经常使用的函数。DApps能够创建一个TaskRequest对象(导出为CronCatTaskRequest),提供工厂地址和可选回复参数。它将返回一个包含可以添加到合约响应中的子消息的结果,例如Response::new().add_submessage(s)(
  • create_croncat_task_message — 与上面的子消息相同,但作为一个消息,用于集成已决定不需要回复的dApps。
  • create_croncat_task_cosmos_msg — 这个函数在之前的调用中作为辅助函数使用,但也可以导出给开发者使用,因为这样做只会带来帮助。它返回创建CronCat任务的Wasm执行消息的结果。
  • croncat_basic_validation — 如果我们愿意,这个函数将来可以扩展,它只包含一个验证,即确保资金已被附加。由于错误处理的性质,感觉我们必须尝试进行基本验证,以减少集成者从DevX的混淆。
pub struct CronCatTaskSubmessageParams {
    /// Defaults to [REPLY_CRONCAT_TASK_CREATION](crate::REPLY_CRONCAT_TASK_CREATION)
    pub reply_id: Option<u64>,
    /// Defaults to [Always](SubMessageReplyType::Always)
    pub reply_type: Option<SubMessageReplyType>,
}

REPLY_CRONCAT_TASK_CREATION 被设置为从单词 "croncat" 得出的大而唯一的数字,以减少集成dApps时的冲突。

还有一个简单的枚举用于回复类型,可以根据需要指定。

pub enum SubMessageReplyType {
    Always,
    OnError,
    OnSuccess,
}

处理任务创建后的回复

当您使用带有回复的子消息创建任务时,您可以在您的合约的回复入口点中利用两个函数。

  • reply_handle_croncat_task_creation — 这个函数接受 msg: Reply 参数并将其解析为更有用的 CronCatTaskExecutionInfo,这仅仅是 croncat-sdk-tasksTaskExecutionInfo。它返回一个元组,包含回复入口点的逻辑和二进制消息数据。后者对于在回复的Response中包含很有用,例如通过使用 Reply::new().set_data(

处理来自任务的调用

这里引入的方法

  • handle_incoming_task — 这将检查任务的版本,并通过工厂查询确认发送者是受制裁的管理合约。它还检查执行是否是同步的,通过确认状态中在发送调用它的消息之前管理器保存。最后,它检查任务的拥有者是接收合约本身,或者是在可选参数中提供的 expected_owner

在这种情况下,您的合约被调用,在预期CronCat将调用的方法中。我们想了解是否确实是CronCat在执行您的任务,并检查任务的所有者是接收合约。如果您希望修改这些限制,您可以使用此结构体来提供额外的参数进行修改。

pub struct HandleIncomingTaskParams {
    /// Disables the check ensuring that the block height and transaction index are the same
    /// If you expect an IBC delay or something asynchronous, disable by setting to true.
    pub disable_sync_check: bool,
    /// By default, we check that the contract receiving the task invocation
    /// must be the owner of the that task. Put another way: someone else's
    /// task isn't invoking our method. You can disable this by setting to true.
    pub disable_owner_check: bool,
    /// If the owner check is enabled, you may specify an alternate expected owner.
    /// Perhaps the task owner isn't this contract, but you know the address.
    /// By default, the validation logic in `handle_incoming_task` checks against the current contract.
    /// If disable_owner_check is true, this value is irrelevant.
    pub expected_owner: Option<Addr>,
}

依赖项

~6–13MB
~169K SLoC