Created
May 12, 2021 12:35
-
-
Save alexroan/62201c6854aaffea21b4bc18203bef6d to your computer and use it in GitHub Desktop.
Keeper Compatible Staleness Flagger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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