Making Keeper Compatible Contracts
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.
KeeperCompatibleInterface
Functions
Name | Description |
---|---|
checkUpkeep | Checks if the contract requires work to be done. |
performUpkeep | Performs 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.
️ Important Note
The check that is run is subject to the checkGasLimit
in the configuration of the registry.
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
);
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 callperformUpkeep
or not.performData
: Bytes that the Keeper should callperformUpkeep
with, if Upkeep is needed. If you would like to encode data to decode later, tryabi.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
The Upkeep that is performed is subject to the callGasLimit
in the configuration of the registry.
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;
Parameters
performData
: Data which was passed back from thecheckData
simulation. If it is encoded, it can easily be decoded into other types by callingabi.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.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/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) {
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. The checkData is defined when the Upkeep was registered.
}
function performUpkeep(bytes calldata /* performData */) external override {
lastTimeStamp = block.timestamp;
counter = counter + 1;
// We don't use the performData in this example. The performData is generated by the Keeper's call to your checkUpkeep function
}
}
️ 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.