Skip to content

Commit

Permalink
feat: monitor heartbeat in chainlink (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Oct 2, 2022
1 parent 460615f commit 6198cea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/chainlink/ChainlinkOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ contract ChainlinkOracle is Ownable, IOracle {
/// @notice Mapping of token to token/usd chainlink price feed
mapping(address => AggregatorV3Interface) public feed;

/// @notice HeatBeat of chainlink feed
mapping(address => uint) public heartBeatOf;

/* -------------------------------------------------------------------------- */
/* EVENTS */
/* -------------------------------------------------------------------------- */
Expand All @@ -47,10 +50,13 @@ contract ChainlinkOracle is Ownable, IOracle {
/// @inheritdoc IOracle
/// @dev feed[token].latestRoundData should return price scaled by 8 decimals
function getPrice(address token) external view virtual returns (uint) {
(, int answer,,,) =
(, int answer,, uint updatedAt,) =
feed[token].latestRoundData();

if (answer < 0)
if (block.timestamp - updatedAt >= heartBeatOf[token])
revert Errors.StalePrice(token, address(feed[token]));

if (answer <= 0)
revert Errors.NegativePrice(token, address(feed[token]));

return (
Expand All @@ -63,10 +69,13 @@ contract ChainlinkOracle is Ownable, IOracle {
/* -------------------------------------------------------------------------- */

function getEthPrice() internal view returns (uint) {
(, int answer,,,) =
(, int answer,, uint updatedAt,) =
ethUsdPriceFeed.latestRoundData();

if (answer < 0)
if (block.timestamp - updatedAt >= 86400)
revert Errors.StalePrice(address(0), address(ethUsdPriceFeed));

if (answer <= 0)
revert Errors.NegativePrice(address(0), address(ethUsdPriceFeed));

return uint(answer);
Expand All @@ -78,10 +87,12 @@ contract ChainlinkOracle is Ownable, IOracle {

function setFeed(
address token,
AggregatorV3Interface _feed
AggregatorV3Interface _feed,
uint heartBeat
) external adminOnly {
if (_feed.decimals() != 8) revert Errors.IncorrectDecimals();
feed[token] = _feed;
heartBeatOf[token] = heartBeat;
emit UpdateFeed(token, address(_feed));
}
}
1 change: 1 addition & 0 deletions src/utils/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ interface Errors {
error IncorrectDecimals();
error L2SequencerUnavailable();
error InactivePriceFeed(address feed);
error StalePrice(address token, address feed);
error NegativePrice(address token, address feed);
}

0 comments on commit 6198cea

Please sign in to comment.