How to Automate Your Smart Contract Executions Using Gelato

Gelato Team

Apr 24, 2023

If you've ever built a smart contract, you know that it can’t execute functions automatically on its own based on conditions — for example, selling ETH when the price spikes. To achieve this, you’d usually need to build and maintain your own bot to monitor conditions and trigger transactions, which requires time, servers, and ongoing supervision.

Fortunately, with Gelato, this isn’t your problem anymore!

Let’s Begin

Suppose you already have a deployed smart contract and want Gelato to call a function at a set interval. All you need to do is deploy a resolver contract that tells Gelato:

“Call this function every hour.”

Here’s an example of a function buy() that buys ETH through Uniswap V2. We want Gelato to call it once per hour:

uint256 public lastBought;

function buy(
    uint256 amountIn,
    uint256 amountOutMin,
    address[] calldata path,
    address to,
    uint256 deadline
) external {
    require(block.timestamp >= lastBought + 1 hours);
    IERC20(path[0]).approve(address(router), amountIn);
    router.swapExactTokensForETH(
       amountIn,
       amountOutMin,
       path,
       to,
       deadline
    );
    lastBought = block.timestamp;
}

Below is an example of a resolver contract. The checker() function returns true if it has been at least one hour since lastBought, prompting Gelato to call buy().

contract Resolver {
  address public immutable owner;
  ISwap public immutable swap;
  address WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
  address USDT = 0xdAC17F958D2ee523a2206206994597C13D831ec7;

  constructor(address _swap) public {
    owner = msg.sender;
    swap = ISwap(_swap);
  }

  function checker()
    external
    view
    returns (bool canExec, bytes memory execPayload)
  {
    address ;
    path[0] = USDT;
    path[1] = WETH;
    uint256 deadline = block.timestamp + 10 minutes;
    uint256 lastBought = swap.lastBought();

    if (lastBought >= 1 hours) {
       bytes4 selector =
         bytes4(keccak256(
           "buy(uint256,uint256,address[],address,uint256)"
         ));

       execPayload = abi.encodeWithSelector(
         selector,
         1000 ether,
         0,
         path,
         owner,
         deadline
       );
       canExec = true;
       return (canExec, execPayload);
    }
  }
}

Gelato calls checker() off-chain at every block. If canExec is true, Gelato executors will send a transaction with execPayload.

Read more about resolvers in the official guide here.

Task Creation

Once your resolver contract is deployed, the next step is to create a task. Gelato makes this simple via the Gelato Automate UI:

  1. Visit the Gelato Automate app.

  2. Fill in your execution address (the contract with the function you want to call).

  3. Select the function and choose “Dynamic inputs via Resolver.”

  4. Enter your resolver contract address and select the checker() function.

  5. Deposit funds into your Gelato balance to pay executors.

  6. Name your task and create it.

Once done, you’ll be redirected to your task page, where you can monitor all executions in real time.

All Done!

Gelato will now monitor your smart contract and automatically execute when your conditions are met.

Check out the full list of supported network contract addresses to get started quickly.

About Gelato

Gelato is a Web3 Cloud Platform empowering developers to build automated, gasless, and off-chain-aware smart contracts and Layer 2 chains. More than 400 projects trust Gelato to facilitate millions of transactions in DeFi, NFTs, and gaming.

  • Gelato RaaS: Deploy tailor-made ZK or OP L2 chains with Gelato middleware baked in.

  • Web3 Functions: Connect smart contracts to off-chain data with decentralized cloud functions.

  • Automate: Execute contracts automatically via reliable, decentralized infrastructure.

  • Relay: Enable gasless transactions for your users through a simple API.

  • Account Abstraction SDK: Built with Safe, combining Gelato’s gasless infra with the most secure smart contract wallet.

👉 Subscribe to our newsletter and turn on Twitter notifications for the latest Gelato updates.
👉 Interested in helping build the future of the internet? Explore open positions and apply here.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.