Traditional blockchains often require humans in the loop to execute transactions at the right time or under specific conditions. With Ritual’s scheduling system, we can create autonomous agents that live entirely on-chain and execute complex sets of actions based on the world around them.

In this tutorial, we will:

  1. Create a smart agent that autonomously executes transactions
  2. Use the IScheduler helper interface to manage agent actions
  3. Configure scheduling parameters to control when and how your agent operates

1

Import IScheduler for your agent

The IScheduler interface is Ritual’s convenience library that enables your agent to schedule and execute autonomous actions.

Your agent can use simple configuration parameters through functions like <IScheduler>.schedule() to set up its automated behaviors:

// Declare scheduler for the agent
IScheduler public scheduler;

// Initialize scheduler in constructor
constructor(..., address schedulerAddress) {
    // ...
    scheduler = IScheduler(schedulerAddress);
    // ...
}
2

Program your agent's recurring actions

The following code shows how to program your agent to execute actions at predetermined intervals.

Your agent can schedule any function call with the appropriate selector and arguments, making it versatile for both simple and complex automated tasks.

function scheduleAgentAction(
        bytes calldata args,
        uint32 gasLimit,
        uint48 gasPrice,
        uint32 frequency,
        uint32 numBlocks
    ) public payable {
        uint256 amount = uint256(gasLimit) *
            uint256(gasPrice) *
            uint256(numBlocks / frequency);

        require(msg.value >= amount, "Insufficient funds");

        uint32 deadline = uint32(block.number + numBlocks);

        scheduler.schedule{value: msg.value}(
            bytes4(abi.encodeCall(this.requestScheduledCompute, (args))),
            gasLimit, // max gas limit
            gasPrice, // max gas price
            uint32(block.number + numBlocks), // max block number
            frequency // frequency
        );
    }

With just these two steps, you can create an autonomous agent that executes scheduled transactions!


Your agent’s behavior can be customized through the <IScheduler>.schedule() parameters:

ParameterDescription
fnFunction to invoke + optional calldata
maxGasLimitMax gas limit, in wei, per call
maxGasPriceMax gas price, in wei, to pay per call
maxBlockNumberMax block number after which subsequent calls are invalidated
frequencyNumber of blocks between each subsequent call