Skip to content

Instantly share code, notes, and snippets.

@alexroan
Created May 12, 2021 12:35
Show Gist options
  • Save alexroan/62201c6854aaffea21b4bc18203bef6d to your computer and use it in GitHub Desktop.
Save alexroan/62201c6854aaffea21b4bc18203bef6d to your computer and use it in GitHub Desktop.
Keeper Compatible Staleness Flagger
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
interface KeeperCompatibleInterface {
function checkUpkeep(bytes calldata checkData) external returns (bool upkeepNeeded, bytes memory performData);
function performUpkeep(bytes calldata performData) external;
}
contract Staleness is KeeperCompatibleInterface {
uint public immutable interval;
mapping(AggregatorV3Interface => bool) public staleFlag;
constructor(uint updateInterval) {
interval = updateInterval;
}
function checkUpkeep(bytes calldata checkData) external view override returns (bool upkeepNeeded, bytes memory performData) {
upkeepNeeded = checkIfStale(abi.decode(checkData, (AggregatorV3Interface)));
performData = checkData;
}
function performUpkeep(bytes calldata performData) external override {
raiseFlag(abi.decode(performData, (AggregatorV3Interface)));
}
function checkIfStale(AggregatorV3Interface priceFeed) public view returns (bool) {
(,,,uint timeStamp,) = priceFeed.latestRoundData();
return (block.timestamp - timeStamp) > interval;
}
function raiseFlag(AggregatorV3Interface priceFeed) public {
require(checkIfStale(priceFeed), "Not stale");
require(!staleFlag[priceFeed], "Already flagged");
staleFlag[priceFeed] = true;
}
function lowerFlag(AggregatorV3Interface priceFeed) public {
require(!checkIfStale(priceFeed), "Still stale");
require(staleFlag[priceFeed], "Not flagged");
staleFlag[priceFeed] = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment