BLOG — Developer Tutorials

35 days ago

How to Automate Your Smart Contract Executions Using Gelato

If you've ever attempted to create a smart contract, the you know that it's impossible to program it to execute a particular function on its own based on a specific condition, such as initiating a sell order when Ethereum's price skyrockets.

To accomplish this, you'd need to BYOB (build your own bot) to keep track of on-chain activities and then transmit transactions accordingly. But this requires a considerable amount of effort and developer resources, which could be better used to enhance your smart contract. You'd also have to operate your bot on a server and supervise it daily, which isn't the most fun experience.

Fortunately, if you're a Gelato user, none of the above is your concern!

Let’s Begin

Let’s say you already have a smart contract deployed and you want Gelato to call it at a certain time. All you need to do is to deploy a resolver contract which will tell Gelato.

“Hey call this function here at every hour”

Here’s an example of the function buy() which I want Gelato to call. This function buys ETH through Uniswap V2.

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);
    lastBought = block.timestamp;

Below is an example of a resolver contract, checker() returns true when it has been at least an hour past lastBought. This prompts Gelato to call the buy() function mentioned above.

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()
    returns (bool canExec, bytes memory execPayload)
    address[] memory path = new address[](2);
    path[0] = USDT;
    path[1] = WETH;
    uint256 deadline = block.timestamp + 10 minutes;
    uint256 lastBought = swap.lastBought();
    if (lastBought >= 1 hours) {
       bytes4 selector =                                                        
       execPayload = abi.encodeWithSelector(
         1000 ether,
     canExec = true;
     return (canExec, execPayload);

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

Click here for more information on how to write a resolver contract

Task Creation

Once you have your resolver contract deployed, you’re already halfway done! Now you would need to submit a task. Gelato makes submitting tasks super easy with the Gelato Automate UI.

  1. Visit the Gelato Automate webpage

  2. Fill in your execution address

  • Fill in the smart contract address containing the function you want Gelato to call.
  • Select the function to be automated
  • Select “Dynamic inputs via Resolver”
  1. Fill in the resolver address
  • Fill in the address of your resolver deployed previously.
  • Select the function gelato should call to check if the task should be executed.
  1. Deposit Funds
  • Choose pay with “Gelato Balance”
  • Deposit some funds which will be used to pay executors for executing your transactions.
  1. Name your task

  2. Create Task You will then be redirected to your task page.

Here you can find details of your task. All the Gelato executions will also appear here.

All Done!

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

Here are the contract addresses for each supported network, so you can jump right in and get started!