How to Automate Your Smart Contract Executions Using Gelato🍦
Tags: blockchain, automation, defi, tooling
Published: 2021-08-10 13:09:43

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](http://gelato.network/) 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.

```solidity
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;
  }
```
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.

```solidity
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`.

**To get more information on how to write a resolver contract, take a look at the [docs](https://docs.gelato.network/tutorial#step-2-write-and-deploy-a-resolver-smart-contract)**

#👷‍♂️ 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](https://app.gelato.network/dashboard) webpage
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ox3ebiavqpjeyohj05rm.png)
 
Here you can see your current tasks and balance.

###2. Click on Submit Task
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86gk9ujx873w2q11pbl4.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ed2vwaegmw4h9nxjrs2.png)

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](https://docs.gelato.network/tutorial#step-3-create-a-task).**

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ptybr2y5ndt84qs6at41.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2u32wyer6v0ioj8i5jea.png)

On your [dashboard](https://app.gelato.network/dashboard), you can see all the tasks you have submitted.

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft8ji4a8fzlvcygosjzg.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/73xi618dv3r5v7hektbu.png)

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

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ie1smxpsioyofw6104c.png)

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](https://docs.gelato.network/contract-addresses)!