Creating Web3 Function Tasks

Configuring what triggers your Web3 Function to run and the target smart contract to execute

Overview

A Web3 Function Task links your Web3 Function to your target smart contract and enables you to configure what should trigger your Web3 Function to execute, any arguments you want to pass to it and the target smart contract to execute.

Ways to submit Web3 Function Tasks

Creating a Web3 Function Task via the UI

In order to create a task that will automatically run your Web3 Function on your behalf, go to https://beta.app.gelato.network/ and click on the "Create Task" button.

You will be invited to select what should trigger your Web3 Function:

Time Interval Trigger Type

Select the time interval between Web3 Function runs that you would like to schedule. The minimum interval is currently 5 seconds. You can also set a timestamp (in UTC) in the future when you'd like executions to commence. By default runs start immediately after the task is created.

You can alternatively use any standard Cron expression to specify when you want your Web3 Function. Cron does not support sub-minute frequencies.

If your Web3 function triggers a transaction execution, further runs of your function will be paused until the transaction is confirmed on chain. When confirmed your Web3 Function runs will resume as configured.

Setting the Web3 Function to run

Make sure to have the IPFS CID of your Web3 Function at hand and paste it into the respective input. We fetch your Web3 Function and enable you to review the source code and enter any arguments that you have created:

Select the network on which your task should execute:

If you select a network that your wallet is not currently connected to you will be prompted to switch to the target network.

If your task uses secrets to store any private credentials, you can then configure the secret variables that will be exposed in your web3 function:

Secrets are optional, leave this part empty if your web3 function is not using any private credentials.

Finally, you will be prompted to enter a name for your task. Click on "Create Task" and confirm your name and task creation using your wallet:

You will then be redirected to the task page where you can view logs, past executions and more task-related information.

Using a Smart contract

You can create a task that uses Web3 Function from your smart contract as well. Learn more about creating tasks via smart contract.

Web3 Function secrets are not available for smart contract created tasks.

To create a Web3 Function task with your smart contract, you can inherit AutomateTaskCreator which has helper functions to easily create your task.

  • Pass the Module.PROXY & Module.WEB3_FUNCTION as modules in ModuleData

       ModuleData memory moduleData = ModuleData({
            modules: new Module[](2),
            args: new bytes[](2)
        });
        
        moduleData.modules[0] = Module.PROXY;
        moduleData.modules[1] = Module.WEB3_FUNCTION;
  • Use _web3FunctionModuleArg to encode arguments for WEB3_FUNCTION module.

    function _web3FunctionModuleArg(
        string memory _web3FunctionHash, // IPFS CID of deployed web3Function
        bytes calldata _web3FunctionArgsHex // Abi encoded web3 function arguments
    ) 

Here is how you can encode your Web3Function arguments to get web3FunctionArgsHex.

In this example, the Web3Function has 2 arguments, counterW3fAddress & count .

schema.json
{
  "web3FunctionVersion": "2.0.0",
  "runtime": "js-1.0",
  "memory": 128,
  "timeout": 30,
  "userArgs": {
    "counterW3fAddress": "string",
    "count", "number"
  }
}

In your contract, you would encode the arguments according to the sequence defined in schema.json .

    function _getWeb3FunctionArgsHex(
        address counterW3fAddress,
        uint256 count
    ) 
    internal 
    pure 
    returns (bytes memory web3FunctionArgsHex) {
        web3FunctionArgsHex = abi.encode(counterW3fAddress, count)
    }

The full code can be found here.

    function createTask(
        string memory _web3FunctionHash,
        bytes calldata _web3FunctionArgsHex
    ) external {
        require(taskId == bytes32(""), "Already started task");

        bytes memory execData = abi.encodeCall(this.increaseCount, (1));

        ModuleData memory moduleData = ModuleData({
            modules: new Module[](2),
            args: new bytes[](2)
        });
        moduleData.modules[0] = Module.PROXY;
        moduleData.modules[1] = Module.WEB3_FUNCTION;

        moduleData.args[0] = _proxyModuleArg();
        moduleData.args[1] = _web3FunctionModuleArg(
            _web3FunctionHash,
            _web3FunctionArgsHex
        );

        bytes32 id = _createTask(
            address(this),
            execData,
            moduleData,
            address(0)
        );

        taskId = id;
        emit CounterTaskCreated(id);
    }

Using Automate SDK

Use the automate-sdk to easily create a new task:

yarn install @gelatonetwork/automate-sdk@beta

Make sure to use the beta version of the automate-sdk to have access to web3 functions method

Import the sdk and create task, passing your web3 function CID & arguments:

import { AutomateSDK, Web3Function } from "@gelatonetwork/automate-sdk";

const automate = new AutomateSDK(chainId, deployer);

const { taskId, tx } = await automate.createBatchExecTask({
  name: "Web3Function - Eth Oracle",
  web3FunctionHash: cid,
  web3FunctionArgs: {
    oracle: oracle.address,
    currency: "ethereum",
  },
});
await tx.wait();

If your task utilizes secrets, you can set them after the task has been created.

const web3Function = new Web3Function(chainId, deployer);

const secrets = {
  API_KEY: "..." // Set your secret environment variables
}
await web3Function.secrets.set(secrts, taskId);

You can see a live example in our web3-functions-hardhat-template repo

Last updated

Change request #272: W3F context including gelato args