Gelato
  • Introduction
    • Gelato, The Web3 Cloud Platform
  • Rollup As A Service
    • Introduction
    • Rollup Stacks
      • Arbitrum Orbit
        • Run a Full Orbit Node
      • OP Stack
        • Run OP Node
    • Deploy your Rollup
    • Customization
      • Data Availability
        • Celestia
        • Avail
        • Eigen DA
      • Custom Gas Token
      • Marketplace
        • Gelato Services
        • Data Indexers
        • Block Explorers
        • Oracles
        • Bridges
        • Account Abstraction
        • On & Off-ramp
        • Community
        • Identity & KYC
        • Others
      • Verifier Node Package
    • Public Testnet
  • RPC Nodes
    • Introduction
    • Compute Units
    • Using RPC Nodes
    • Supported Networks
    • Pricing and Plans
    • FAQ
  • Web3 Services
    • Web3 Functions
      • Understanding Web3 Functions
        • Trigger Types
        • Typescript Function
        • Solidity Function
        • Automated Transactions
      • Security Considerations
      • Template & Use Cases
      • Quick Start
        • Writing Typescript Functions
          • Event Trigger
          • Private Typescript Functions
          • Callbacks
        • Test, Deploy & Run Typescript functions
        • Writing Solidity Functions
        • Test, Deploy & Run Solidity Functions
        • Initiate an Automated Transaction
      • Create a Web3 Function Task
        • Using the UI
        • Using the Safe App
        • Using a Smart Contract
        • Using the Automate SDK
      • Analytics & Monitoring
      • Supported Networks
      • Subscription & Payments
      • Legacy Automate Migration Guide
    • Relay
      • What is Relaying?
      • Security Considerations
        • ERC-2771 Delegatecall Vulnerability
      • Templates
      • Quick Start
        • Sponsored Calls
        • Non-Sponsored Calls
      • ERC-2771 (recommended)
        • SponsoredCallERC2771
        • CallWithSyncFeeERC2771
          • Relay Context Contracts ERC2771
      • Non-ERC-2771
        • SponsoredCall
        • CallWithSyncFee
          • Relay Context Contracts
      • Relay API
      • Gelato's Fee Oracle
      • Tracking your Relay Request
      • Supported Networks
      • Subscriptions and Payments
        • 1Balance & Relay
        • SyncFee Payment Tokens
        • Relay Pricing
      • ERC2771 Migration Guide
    • VRF
      • Understanding VRF
      • How does Gelato VRF Work?
      • Security Considerations
      • Template
      • Quick Start
      • Create a VRF Task
        • Create a Fallback VRF
        • Migrating from Chainlink VRF
      • Supported Networks
      • Pricing & Rate Limits
    • Oracles
      • Understanding Gelato Oracles
      • Quick Start
      • Data Providers
        • Stork
        • Choas Labs
      • Migrating from Chainlink Oracles
      • Available Price Feeds
      • Supported Networks
      • Pricing & Rate Limits
    • Account Abstraction
      • Understanding ERC4337
      • Introduction to Gelato Bundler
      • Templates & Examples
      • Quick Start
      • Sponsored UserOps
        • Using 1Balance
        • Using Zerodev Paymaster
      • Non-Sponsored UserOps
        • Pay with Native
        • Pay with ERC20
      • Supported Networks
      • Bundler API Endpoints
        • eth_sendUserOperation
        • eth_estimateUserOperationGas
        • eth_getUserOperationByHash
        • eth_getUserOperationReceipt
        • eth_supportedEntryPoints
        • eth_maxPriorityFeePerGas
        • eth_chainId
    • 1Balance
      • 1Balance Alerts
      • Subscription Plans
      • Subscription Notifications
      • USDC Addresses
    • AI Agents
    • Teams
  • GELATO DAO
    • DAO & Token (GEL)
    • GEL Token Contracts
    • Governance Process
  • Social Media
Powered by GitBook
On this page
  • Using a Smart contract
  • Additional Info
  • _createTask()
  • ModuleData
  • _cancelTask()
  • onlyDedicatedMsgSender
  • _depositFunds1Balance()
  • Single Execution Task
  1. Web3 Services
  2. Web3 Functions
  3. Create a Web3 Function Task

Using a Smart Contract

PreviousUsing the Safe AppNextUsing the Automate SDK

Last updated 1 month ago

Using a Smart contract

You can create a task that uses Web3 Function from your smart contract as well.

If your project involves complex interactions and you need the task creation to be a part of an on-chain transaction, you would directly interact with a 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 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)
    }
    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);
    }

Additional Info

Tasks created via this route cannot be named

Smart Contracts can also create and cancel tasks.

Here are the functions exposed by AutomateTaskCreator which you can use when setting up your smart contract.

_createTask()

Interacts and creates a task on the Gelato Automate smart contract.

    function _createTask(
        address execAddress, 
        bytes memory execDataOrSelector, 
        ModuleData memory moduleData,
        address feeToken 
    ) internal returns (bytes32 taskId);
  • execAddress - Address of the contract which Gelato will call.

  • execDataOrSelector - Signature of function which Gelato will call / execution data (If Resolver Module is not used. More about modules below)

  • moduleData - Modules that are enabled for the task. (More about ModuleData below)

  • feeToken - Use address(0) if using Gelato 1balance. Use 0xeeeeee... for ETH or native tokens.

ModuleData

struct ModuleData {
        Module[] modules;
        bytes[] args;
}

Modules are conditions / specifications about your task. These are the current available Modules.

enum Module {
    RESOLVER,
    PROXY,
    SINGLE_EXEC,
    WEB3_FUNCTION,
    TRIGGER
}
  • RESOLVER - Define dynamic conditions and execution data.

  • TIME - Repeated execution at a specific time and interval. (in ms)

  • PROXY - Your function will be called by a dedicated msg.sender.

  • SINGLE_EXEC - Task is cancelled after one execution.

  • WEB3_FUNCTION - Define a Typescript function to get off-chain execution data.

  • TRIGGER - Define your execution trigger (Time interval, Event, every block, ...)

Each Module would require additional arguments which is an encoded data.

Including Module.Proxy in moduleData is mandatory, otherwise task creation will fail.

You can use these helper functions to get the arguments for each Module.

    function _resolverModuleArg(address _resolverAddress, bytes memory _resolverData)

    function _proxyModuleArg()
    
    function _singleExecModuleArg()
    
    function _timeTriggerModuleArg(uint128 _start, uint128 _interval)
    
    function _cronTriggerModuleArg(string memory _expression)
    
    function _blockTriggerModuleArg()
    
    function _eventTriggerModuleArg(
        address _address,
        bytes32[][] memory _topics,
        uint256 _blockConfirmations
    ) internal pure returns (bytes memory)

Crafting ModuleData will look like this if we want to create a task which utilise RESOLVER ,PROXY & SINGLE_EXEC Module.

        ModuleData memory moduleData = ModuleData({
            modules: new Module[](3),
            args: new bytes[](3)
        });

        moduleData.modules[0] = Module.RESOLVER;
        moduleData.modules[1] = Module.PROXY;
        moduleData.modules[2] = Module.SINGLE_EXEC

        moduleData.args[0] = _resolverModuleArg(
            address(this),
            abi.encodeCall(this.checker, ())
        );
        moduleData.args[1] = _proxyModuleArg();
        moduleData.args[2] = _singleExecModuleArg();

Module[] must follow the order RESOLVER, PROXY, SINGLE_EXEC, WEB3_FUNCTION, TRIGGER

_cancelTask()

Cancels a task owned by the smart contract.

    function _cancelTask(bytes32 _taskId) internal

onlyDedicatedMsgSender

Function modifier to restrict msg.sender to only task executions created by taskCreator (defined in constructor). Learn more about it at Security Considerations

    modifier onlyDedicatedMsgSender() {
        require(msg.sender == dedicatedMsgSender, "Only dedicated msg.sender");
        _;
    }

_depositFunds1Balance()

    function    function _depositFunds1Balance(
        uint256 _amount,
        address _token,
        address _sponsor
    )

Deposit funds into the Gelato 1balance contract.

The _depositFunds1Balance method is only available on Polygon

Single Execution Task

ModuleData memory moduleData = ModuleData({
    modules: new Module[](2),
    args: new bytes[](2)
});

moduleData.modules[0] = Module.PROXY;
moduleData.modules[1] = Module.SINGLE_EXEC;

moduleData.args[0] = _proxyModuleArg();
moduleData.args[1] = _singleExecModuleArg();

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

The full code can be found .

You can find a list of example smart contracts .

If you want to have Gelato call your function only once. If so, you can Include SingleExec module in ModuleData.modules . Check out the full code .

AutomateTaskCreator
here
here
here