Created
May 12, 2021 12:12
-
-
Save alexroan/181c4a3d1ae76583b08f14bc01cc9ac3 to your computer and use it in GitHub Desktop.
Simple 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"; | |
contract Staleness { | |
uint public immutable interval; | |
mapping(AggregatorV3Interface => bool) public staleFlag; | |
constructor(uint updateInterval) { | |
interval = updateInterval; | |
} | |
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