Skip to content

Instantly share code, notes, and snippets.

@ZakAyesh
Last active December 15, 2022 06:23
Show Gist options
  • Save ZakAyesh/5048ecc72f545a898179a4f85e2947fa to your computer and use it in GitHub Desktop.
Save ZakAyesh/5048ecc72f545a898179a4f85e2947fa to your computer and use it in GitHub Desktop.
An example showing how to perform multiple Keeper upkeeps on one contract by using checkdata and execution paths
// SPDX-License-Identifier: MIT
pragma solidity ^0.7.0;
// KeeperCompatible.sol imports the functions from both ./KeeperBase.sol and
// ./interfaces/KeeperCompatibleInterface.sol
import "@chainlink/contracts/src/v0.7/KeeperCompatible.sol";
contract MultipleCounter is KeeperCompatibleInterface {
/**
* Public counter variable
*/
uint public counter;
uint public counter2;
uint public counter3;
/**
* Use an interval in seconds and a timestamp to slow execution of Upkeep
*/
uint public immutable interval;
uint public immutable interval2;
uint public immutable interval3;
uint public lastTimeStamp;
uint public lastTimeStamp2;
uint public lastTimeStamp3;
constructor(uint updateInterval, uint updateInterval2, uint updateInterval3) {
interval = updateInterval;
interval2 = updateInterval2;
interval3 = updateInterval3;
lastTimeStamp = block.timestamp;
lastTimeStamp2 = block.timestamp;
lastTimeStamp3 = block.timestamp;
counter = 0;
counter2 = 0;
counter3 = 0;
}
function checkUpkeep(bytes calldata checkData ) external override returns (bool upkeepNeeded, bytes memory performData) {
if(keccak256(checkData) == keccak256(hex'01')) {
upkeepNeeded = (block.timestamp - lastTimeStamp) > interval;
performData = checkData;
}
if(keccak256(checkData) == keccak256(hex'02')) {
upkeepNeeded = (block.timestamp - lastTimeStamp2) > interval2;
performData = checkData;
}
if(keccak256(checkData) == keccak256(hex'03')) {
upkeepNeeded = (block.timestamp - lastTimeStamp3) > interval3;
performData = checkData;
}
}
function performUpkeep(bytes calldata performData ) external override {
if(keccak256(performData) == keccak256(hex'01')) {
lastTimeStamp = block.timestamp;
counter = counter + 1;
}
if(keccak256(performData) == keccak256(hex'02')) {
lastTimeStamp2 = block.timestamp;
counter2 = counter2 + 1;
}
if(keccak256(performData) == keccak256(hex'03')) {
lastTimeStamp3 = block.timestamp;
counter3 = counter3 + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment