Last active
May 13, 2021 15:47
-
-
Save alexroan/ccde03a3b0c34ed92c0302d5eb90990e to your computer and use it in GitHub Desktop.
Solidity Tech Talk - Bad Code
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/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
import "https://github.com/smartcontractkit/chainlink/blob/develop/evm-contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
contract BadStalenessChecker is Ownable { | |
// Details about a price feed. | |
struct PriceFeedDetails { | |
AggregatorV3Interface aggregator; | |
uint stalenessThreshold; | |
} | |
// The name of this contract | |
string public name; | |
// The target price feed | |
PriceFeedDetails public priceFeed; | |
// The proposed price feed | |
PriceFeedDetails public proposedPriceFeed; | |
// Events | |
event PriceFeedProposed(AggregatorV3Interface indexed proposed); | |
event PriceFeedConfirmed(AggregatorV3Interface indexed previous, AggregatorV3Interface indexed confirmed); | |
// Constructor | |
constructor( | |
string memory contractName | |
) Ownable() { | |
name = contractName; | |
} | |
// Propose a new price feed to check for staleness | |
function proposeNewPriceFeed( | |
AggregatorV3Interface newAggregator, | |
uint newThreshold | |
) | |
external | |
onlyOwner() | |
{ | |
priceFeed = PriceFeedDetails({ | |
aggregator: newAggregator, | |
stalenessThreshold: newThreshold | |
}); | |
emit PriceFeedProposed(newAggregator); | |
} | |
// Confirm a proposed price feed | |
function confirmPriceFeed( | |
AggregatorV3Interface newAggregator | |
) | |
external | |
onlyOwner() | |
{ | |
emit PriceFeedConfirmed(priceFeed.aggregator, newAggregator); | |
priceFeed = proposedPriceFeed; | |
} | |
// Return true if the price feed is stale | |
function checkIfStale() | |
external | |
view | |
returns ( | |
bool | |
) | |
{ | |
(,,,uint timeStamp,) = priceFeed.aggregator.latestRoundData(); | |
return (block.timestamp - timeStamp) > priceFeed.stalenessThreshold; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment