How to Create a Task

Task creation is as easy as creating and deploying a new smart contract. Task authors should follow the interface at ITask.cairo create a task that defines their automation.

Get our help

The Yagi team is happy to help in brainstorming different ideas for tasks that could be helpful to your protocol as well as task development.

Just ask on Discord or Twitter.

Example task

This is an example task that maintains a counter to be called at least every minute. Note: there is nothing currently preventing the counter to be called more frequently.

## SPDX-License-Identifier: AGPL-3.0-or-later

%lang starknet

from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.starknet.common.syscalls import get_block_timestamp
from starkware.cairo.common.math_cmp import is_le

## @title Example counter task.
## @description Incrementable counter.
## @author Peteris <github.com/Pet3ris>

#############################################
##                 STORAGE                 ##
#############################################

@storage_var
func __counter() -> (counter: felt):
end

@storage_var
func __lastExecuted() -> (lastExecuted: felt):
end

#############################################
##                 GETTERS                 ##
#############################################

@view
func counter{
        syscall_ptr : felt*,
        pedersen_ptr : HashBuiltin*,
        range_check_ptr
    }() -> (counter: felt):
    let (counter) = __counter.read()
    return (counter)
end

@view
func lastExecuted{
        syscall_ptr : felt*,
        pedersen_ptr : HashBuiltin*,
        range_check_ptr
    }() -> (lastExecuted: felt):
    let (lastExecuted) = __lastExecuted.read()
    return (lastExecuted)
end

#############################################
##                  TASK                   ##
#############################################

@view
func probeTask{
        syscall_ptr : felt*,
        pedersen_ptr : HashBuiltin*,
        range_check_ptr
    }() -> (taskReady: felt):
    alloc_locals

    let (lastExecuted) = __lastExecuted.read()
    let (block_timestamp) = get_block_timestamp()
    let deadline = lastExecuted + 60
    let (taskReady) = is_le(deadline, block_timestamp)

    return (taskReady=taskReady)
end

@external
func executeTask{
        syscall_ptr : felt*,
        pedersen_ptr : HashBuiltin*,
        range_check_ptr
    }() -> ():
    # One could call `probeTask` here; it depends
    # entirely on the application.

    let (counter) = __counter.read()
    let new_counter = counter + 1
    let (block_timestamp) = get_block_timestamp()
    __lastExecuted.write(block_timestamp)
    __counter.write(new_counter)
    return ()
end

Last updated