Gelato
  • Introduction
    • Gelato, The Web3 Cloud Platform
  • Smart Wallets
    • Introduction
      • Understanding EIP-7702
      • Understanding ERC-4337
      • ERC-4337 vs EIP-7702
    • Templates & Examples
    • How-To Guides
      • Create a Sponsor API Key
      • Sponsor gas for your users
      • Allow users to pay gas with erc20
      • Allow users to pay gas with native
      • Create Dynamic's Environment Id
      • Use Dynamic/Privy signers with React SDK
      • Estimate Gas for your transactions
    • React SDK
    • Demo
    • Supported Networks
  • 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 ERC-4337
      • Introduction to Gelato Bundler
      • Templates & Examples
      • Quick Start
      • 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
  • 1. Understand the Role of a Checker
  • 2. Solidity Function Example
  • 3. Making your Checker Reusable
  • 4. Advanced: Checking Multiple Functions
  • 5. Incorporating Feedback with Logs
  • 6. Limit the Gas Price of your execution
  1. Web3 Services
  2. Web3 Functions
  3. Quick Start

Writing Solidity Functions

1. Understand the Role of a Checker

A Checker acts as a bridge between conditions and smart contract executions. Its purpose? To check conditions and determine whether a task should be executed by Gelato. Every Checker returns two main things:

  • canExec (Boolean): Indicates if Gelato should execute the task.

  • execData (Bytes): Contains the data that executors will use during execution.

Solidity functions must adhere to the block gas limit for checker calls; exceeding it will cause the call to fail.

2. Solidity Function Example

Before we delve into complexities, let's understand the structure of a simple Checker:

contract CounterChecker{
    ICounter public immutable counter;

    constructor(ICounter _counter) {
        counter = _counter;
    }

    function checker()
        external
        view
        returns (bool canExec, bytes memory execPayload)
    {
        uint256 lastExecuted = counter.lastExecuted();

        canExec = (block.timestamp - lastExecuted) > 180;

        execPayload = abi.encodeCall(ICounter.increaseCount, (1));
    }
}

In the above, the checker checks the state of a counter and prompts Gelato to execute if 3 minutes (180 seconds) have elapsed since its last execution.

3. Making your Checker Reusable

Avoid hardcoding addresses. Instead, allow the passing of arguments to your checker. This lets you reuse the checker for multiple tasks:

function checker(address _counter)
    external
    view
    returns (bool canExec, bytes memory execPayload)
{
    uint256 lastExecuted = ICounter(_counter).lastExecuted();

    canExec = (block.timestamp - lastExecuted) > 180;

    execPayload = abi.encodeCall(ICounter.increaseCount, (1));
}

4. Advanced: Checking Multiple Functions

Suppose you're automating tasks across different pools. Instead of creating multiple tasks, iterate through your list of pools within a single checker:

function checker()
    external
    view
    returns (bool canExec, bytes memory execPayload)
{
    uint256 delay = harvester.delay();

    for (uint256 i = 0; i < vaults.length(); i++) {
        IVault vault = IVault(getVault(i));

        canExec = block.timestamp >= vault.lastDistribution().add(delay);

        execPayload = abi.encodeWithSelector(
            IHarvester.harvestVault.selector,
            address(vault)
        );

        if (canExec) return(true, execPayload);
    }

    return(false, bytes("No vaults to harvest"));
}

5. Incorporating Feedback with Logs

With the Gelato Web3 Functions UI, you can use custom return messages to pinpoint where your checker might be "stuck":

function checker()
    external
    view
    returns (bool canExec, bytes memory execPayload)
{
    uint256 lastExecuted = counter.lastExecuted();

    if(block.timestamp - lastExecuted < 180) return(false, bytes("Time not elapsed"));

    execPayload = abi.encodeCall(ICounter.increaseCount, (1));
    return(true, execPayload);
}

6. Limit the Gas Price of your execution

On networks such as Ethereum, gas will get expensive at certain times. If what you are automating is not time-sensitive and don't mind having your transaction mined at a later point, you can limit the gas price used in your execution in your checker.

function checker()
	external
	view
	returns (bool canExec, bytes memory execPayload)
{
	// condition here
	
	if(tx.gasprice > 80 gwei) return (false, bytes("Gas price too high"));
}

This way, Gelato will not execute your transaction if the gas price is higher than 80 GWEI.

PreviousTest, Deploy & Run Typescript functionsNextTest, Deploy & Run Solidity Functions

Last updated 3 months ago