This guide explains how to integrate the JarJar Scheduler into your Sui Move smart contract. The JarJar Scheduler allows you to schedule task executions at specific future timestamps.
Table of Contents
Overview
JarJar Scheduler Contract
Integration Steps
Example Implementation
Best Practices
Overview
The JarJar Scheduler is a decentralized task scheduling system on the Sui blockchain. It allows you to emit events for scheduled tasks, which validators can then execute at the specified time.
The core function you'll interact with is emit_scheduled_task_event. Here's its signature:
Parameters:
execution_path: A string describing which function to call in which module (e.g., "module::function").
execution_time: The timestamp (in milliseconds) when the smart contract should be executed.
amount: The amount of Binks to be given back to the validator upon execution.
params_id: The address of a shared object containing information for the function executed by the scheduler.
anti_spam_fee: A Binks coin used to prevent system spam (must be > 1 BINKS).
Integration Steps
Import the JarJar Scheduler module in your contract:
Create a Params struct to hold the task information:
Implement a function to register scheduled tasks:
Implement the function to be executed by the scheduler:
(Optional) Implement a cancellation function:
Example Implementation
Here's an example of how to implement the JarJar Scheduler in your contract:
Best Practices
Always include sufficient fee to incentivize validators to execute your task.
Use the Clock object to verify the execution time in your task_to_execute function.
Implement proper access control for task cancellation.
Handle potential errors and edge cases in your scheduled tasks.
Consider the gas costs of your scheduled tasks to ensure they can be executed successfully.
Use meaningful and unique execution_path strings to avoid conflicts with other contracts.
By following this guide, you can successfully integrate the JarJar Scheduler into your Sui Move smart contract, enabling decentralized, time-based task execution on the Sui blockchain.
public fun emit_scheduled_task_event(
execution_path: String,
execution_time: u64,
amount: u64,
params_id: address,
anti_spam_fee: Coin<BINKS>
)
use jarjar_scheduler::jarjar_scheduler;
public struct Params has key {
id: UID,
// Add other fields as needed
fee: Balance<BINKS>,
execution_time: u64,
}
public fun register_scheduled_task(
execution_path: String,
execution_time: u64,
fee: Coin<BINKS>,
anti_spam_fee: Coin<BINKS>,
ctx: &mut TxContext
) {
// Create Params object
// Call jarjar_scheduler::emit_scheduled_task_event
// Share the Params object
}
public fun task_to_execute(params: Params, clock: &Clock, ctx: &mut TxContext) {
// Verify execution time
// Perform task logic
// Clean up and transfer fee to executor
}
public fun cancel_task(params: Params, ctx: &mut TxContext) {
// Verify sender
// Clean up and return fee
}
module test_scheduler::test_scheduler {
use jarjar_scheduler::jarjar_scheduler;
use std::string::String;
use sui::coin::{Coin, Self};
use sui::balance::{Self, Balance};
use sui::clock::Clock;
use binks::binks::BINKS;
public struct Params has key {
id: UID,
text: String,
sender: address,
fee: Balance<BINKS>,
execution_time: u64,
}
public fun register_scheduled_task(
execution_path: String,
execution_time: u64,
fee: Coin<BINKS>,
anti_spam_fee: Coin<BINKS>,
ctx: &mut TxContext
) {
let amount = coin::value(&fee);
let params = Params {
id: object::new(ctx),
text: (b"String").to_string(),
sender: ctx.sender(),
fee: coin::into_balance(fee),
execution_time,
};
let params_address = object::uid_to_address(¶ms.id);
jarjar_scheduler::emit_scheduled_task_event(execution_path, execution_time, amount, params_address, anti_spam_fee);
transfer::share_object(params);
}
public fun task_to_execute(params: Params, clock: &Clock, ctx: &mut TxContext) {
let current_time = clock.timestamp_ms();
assert!(params.execution_time < current_time, EExecutionTimeTooEarly);
let Params {id, fee, text, execution_time, sender} = params;
// Task logic here
object::delete(id);
let coin = coin::from_balance(fee, ctx);
transfer::public_transfer(coin, ctx.sender())
}
public fun cancel_task(params: Params, ctx: &mut TxContext) {
assert!(params.sender == ctx.sender());
let Params {id, fee, text:_, execution_time:_, sender:_} = params;
let coin = coin::from_balance(fee, ctx);
object::delete(id);
transfer::public_transfer(coin, ctx.sender())
}
}