DEV Community

Cover image for How to Automate Your Smart Contract Executions Using Gelato🍦
Gelato Network
Gelato Network

Posted on • Updated on

How to Automate Your Smart Contract Executions Using Gelato🍦

If you have ever written a smart contract, you would know that it is impossible to have a smart contract execute a certain function on its own under a specified condition e.g. make a sell order when the price of Ethereum moons. Well to achieve that, you have to build your own bot which monitors on-chain activities and then have it send transactions. But building a bot that performs these functions for you requires a lot of work and developer resources that can instead be allocated to optimizing your smart contract or going out for a walk from your super shadowy basement 🙊 Apart from that, you have to run your bot on a server, monitor it daily… all that fun stuff.

So here is the solution — Continue to prioritize your focus on building awesome smart contracts without having to run the underlying infrastructure that leaves you susceptible to becoming the central point of failure. Let Gelato do the work for you.

🏃‍♂️ Starting Off

Let’s say you already have a smart contract deployed. And you want Gelato to call the smart contract 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 is an example of the function buy() which we 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;
  }
Enter fullscreen mode Exit fullscreen mode

Here is an example of a resolver contract. checker() returns true when it has been at least an hour past lastBought. This will prompt 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);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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

To get more information on how to write a resolver contract, take a look at the docs

👷‍♂️ Creating A Task

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

1. Visit the PokeMe webpage

image

Here you can see your current tasks and balance.

2. Click on Submit Task

image

There are two fields to be filled manually here.

  • Execution address — Address of the function you want Gelato to call.
  • Resolver address — Address of your resolver deployed previously.

image

When you input the execution addresses, a list of functions will appear for you to choose from.

3. Choose the function which you want Gelato to call

Make sure to have your contract verified on Etherscan / Polygonscan or you would have to do it manually.

image

The same thing applies to the resolver address.

  • Choose the function which Gelato will call off-chain to check if the bots should execute.
  • Submit your task!

image

On your dashboard, you can see all the tasks you have submitted.

image

Clicking into the task, you will be able to see more details and all the previous executions of the task.

🤑 Deposit Funds

Once you have submitted your task, the final step is to deposit some funds which will be used to pay executors for executing your transactions.

image

Deposit some ETH or MATIC depending on the network of your smart contracts:

image

Your balance will then appear on the dashboard.

🎊 All Done

You are all set up! Gelato will now monitor your smart contract and execute when your specified conditions are met!

Gelato PokeMe is available on Ethereum mainnet, Ropsten, and Polygon

Here are the contract addresses!

Top comments (0)