1. Home
  2. Docs
  3. Chainlink General
  4. Making Keeper Compatible Contracts

Making Keeper Compatible Contracts

Making Keeper Compatible Contracts

️Open Beta

Chainlink Keepers is in beta. We’re continuing to test the network with users and improve the experience based on your needs. Please reach out to keeper@chain.link with any feedback.

Lets walk through how to make your contract keeper-compatible. While this will get you up and running quickly and showcase how easy it is to use, we highly recommend taking the time to fully understand and become a proficient user of Chainlink Keepers. We want you to take full advantage of the automation infrastructure we’ve built.

  1. The Interface
  2. Example Contract

KeeperCompatibleInterface

Functions

NameDescription
checkUpkeepChecks if the contract requires work to be done.
performUpkeepPerforms the work on the contract, if instructed by checkUpkeep().

checkUpkeep

The Keeper node runs this method as an eth_call in order to determine if your contract requires some work to be done. If the off-chain simulation of your checkUpkeep confirms your predefined conditions are met, the Keeper will broadcast a transaction to the blockchain executing the performUpkeep method described below.

️ NOTE

Since checkUpkeep is only ever performed off-chain in simulation, for most cases it is best to treat this as a view function and not modify any state.

  function checkUpkeep(
    bytes calldata checkData
  )
    external
    returns (
        bool upkeepNeeded,
        bytes memory performData
    );

Solidity (Ethereum)Copy

Parameters

  • checkData: Data passed to the contract when checking for Upkeep. Specified in the Upkeep registration so it is always the same for a registered Upkeep.

Return Values

  • upkeepNeeded: Indicates whether the Keeper should call performUpkeep or not.
  • performData: Bytes that the Keeper should call performUpkeep with, if Upkeep is needed. If you would like to encode data to decode later, try abi.encode.

performUpkeep

When your checkUpkeep returns upkeepNeeded == true, the Keeper node broadcasts a transaction to the blockchain to execute your contract code with performData as an input.

️ Important note

Ensure your performUpkeep is idempotent. Your performUpkeep should change state such that checkUpkeep will not return true for the same subset of work once said work is complete. Otherwise the Upkeep will remain eligible and result in multiple performances by the Keeper Network on the exactly same subset of work.

  function performUpkeep(
    bytes calldata performData
  ) external;

Solidity (Ethereum)Copy

Parameters

  • performData: Data which was passed back from the checkData simulation. If it is encoded, it can easily be decoded into other types by calling abi.decode. This data should always be validated against the contract’s current state.

Example Contract

The example below represents a simple counter contract. Each time performUpkeep is called, it increments its counter by one.Deploy this contract using Remix ↗What is Remix?

pragma solidity ^0.6.7;

import "@chainlink/contracts/src/v0.6/interfaces/AggregatorV3Interface.sol";
import "@chainlink/contracts/src/v0.6/interfaces/KeeperCompatibleInterface.sol";

contract Counter is KeeperCompatibleInterface {
    /**
    * Public counter variable
    */
    uint public counter;


    /**
    * Use an interval in seconds and a timestamp to slow execution of Upkeep
    */
    uint public immutable interval;
    uint public lastTimeStamp;

    
    constructor(uint updateInterval) public {
      interval = updateInterval;
      lastTimeStamp = block.timestamp;

      counter = 0;
    }


    function checkUpkeep(bytes calldata checkData) external override returns (bool upkeepNeeded, bytes memory performData) {
        upkeepNeeded = (block.timestamp - lastTimeStamp) > interval;

        // We don't use the checkData in this example
        // checkData was defined when the Upkeep was registered
        performData = checkData;
    }

    function performUpkeep(bytes calldata performData) external override {
        lastTimeStamp = block.timestamp;
        counter = counter + 1;

        // We don't use the performData in this example
        // performData is generated by the Keeper's call to your `checkUpkeep` function
        performData;
        
    }
    
}
    

Solidity (Ethereum)Copy

️ WARNING

DO NOT attempt to send LINK to your contract like you may be used to with VRF. With Chainlink Keepers, contracts are funded via the registry rather than within your contract

Once deployed, your contract doesn’t automatically start to receive Upkeep after deployment, it must be registered. See Register for Upkeep for more details.

https://docs.chain.link/docs/chainlink-keepers/compatible-contracts/

Was this article helpful to you? Yes No

How can we help?