BLOG — Developer Tutorials

421 days ago

How to Automate Your Smart Contract Executions Using Gelato

If you've ever attempted to create a smart contract, then 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);
    router.swapExactTokensForETH(
           amountIn, 
           amountOutMin, 
           path, 
           to,     
           deadline
    );
    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()
    external
    view
    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 =                                                        
       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 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!

About Gelato

Gelato is a Web3 Cloud Platform empowering developers to create automated, gasless, and off-chain-aware Layer 2 chains and smart contracts. Over 400 web3 projects rely on Gelato for years to facilitate millions of transactions in DeFi, NFTs, and gaming.

  • Gelato RaaS: Deploy your own tailor-made ZK or OP L2 chains in a single click with native Account Abstraction and all Gelato middleware baked in.

  • Web3 Functions: Connect your smart contracts to off-chain data & computation by running decentralized cloud functions.

  • Automate: Automate your smart contracts by executing transactions automatically in a reliable, developer-friendly & decentralized manner.

  • Relay: Give your users access to reliable, robust, and scalable gasless transactions via a simple-to-use API.

  • Account Abstraction SDK: Gelato has partnered with Safe, to build a fully-fledged Account Abstraction SDK, combining Gelato's industry's best gasless transaction capabilities, with the industry's most secure smart contract wallet.

Subscribe to our newsletter and turn on your Twitter notifications to get the most recent updates about the Gelato ecosystem! If you are interested in being part of the Gelato team and building the future of the Internet browse the open positions and apply here.