Skip to content

Commit

Permalink
feat: create OracleLib to check for stale pricefeed data
Browse files Browse the repository at this point in the history
  • Loading branch information
dt6120 committed Nov 8, 2024
1 parent fc82fa4 commit deb2f16
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/DSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {AggregatorV3Interface} from "@chainlink/contracts/shared/interfaces/Aggr

import {IDSCEngine} from "./interfaces/IDSCEngine.sol";
import {DecentralizedStablecoin} from "./DecentralizedStablecoin.sol";
import {OracleLib} from "./libraries/OracleLib.sol";

/**
* @title DSCEngine
Expand All @@ -19,6 +20,8 @@ import {DecentralizedStablecoin} from "./DecentralizedStablecoin.sol";
*
*/
contract DSCEngine is IDSCEngine, ReentrancyGuard {
using OracleLib for AggregatorV3Interface;

uint256 private constant ADDITIONAL_PRICE_FEED_PRECISION = 1e10;
uint256 private constant PRECISION = 1e18;
uint256 private constant LIQUIDATION_THRESHOLD = 50;
Expand Down Expand Up @@ -251,13 +254,13 @@ contract DSCEngine is IDSCEngine, ReentrancyGuard {
*/
function getUsdValue(address token, uint256 amount) public view returns (uint256 usdValue) {
address priceFeed = s_tokenToPriceFeed[token];
(, int256 unitUsdPrice,,,) = AggregatorV3Interface(priceFeed).latestRoundData();
(, int256 unitUsdPrice,,,) = AggregatorV3Interface(priceFeed).staleCheckLatestRoundData();
usdValue = ((uint256(unitUsdPrice) * ADDITIONAL_PRICE_FEED_PRECISION) * amount) / PRECISION;
}

function getTokenAmountFromUsdValue(address token, uint256 amount) public view returns (uint256 tokenAmount) {
address priceFeed = s_tokenToPriceFeed[token];
(, int256 unitUsdPrice,,,) = AggregatorV3Interface(priceFeed).latestRoundData();
(, int256 unitUsdPrice,,,) = AggregatorV3Interface(priceFeed).staleCheckLatestRoundData();
tokenAmount = amount * PRECISION / (uint256(unitUsdPrice) * ADDITIONAL_PRICE_FEED_PRECISION);
}

Expand Down Expand Up @@ -374,6 +377,10 @@ contract DSCEngine is IDSCEngine, ReentrancyGuard {
adjustedCollateralUsdValue = collateralUsdValue * LIQUIDATION_THRESHOLD / LIQUIDATION_PRECISION;
}

function getAdjustedUsdValue(uint256 amount) external pure returns (uint256 adjustedUsdValue) {
adjustedUsdValue = amount * LIQUIDATION_THRESHOLD / LIQUIDATION_PRECISION;
}

function calculateHealthFactor(uint256 totalDscMinted, uint256 totalCollateralUsdValue)
external
pure
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IDSCEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface IDSCEngine {
function getMinHealthFactor() external pure returns (uint256);
function getPriceFeed(address token) external view returns (address);
function getUserCollateralDepositAmount(address user, address token) external view returns (uint256);
function getAdjustedUsdValue(uint256 amount) external pure returns (uint256);
function getDscMinted(address user) external view returns (uint256);
function getCollateralTokens() external view returns (address[] memory);
function getMaxMintableDsc(address user) external view returns (uint256);
Expand Down
22 changes: 22 additions & 0 deletions src/libraries/OracleLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

import {AggregatorV3Interface} from "@chainlink/contracts/shared/interfaces/AggregatorV3Interface.sol";

library OracleLib {
error OracleLib__StalePrice();

uint256 public constant TIMEOUT = 1 hours;

function staleCheckLatestRoundData(AggregatorV3Interface priceFeed)
public
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound)
{
(roundId, answer, startedAt, updatedAt, answeredInRound) = priceFeed.latestRoundData();

if (block.timestamp - updatedAt > TIMEOUT) {
revert OracleLib__StalePrice();
}
}
}

0 comments on commit deb2f16

Please sign in to comment.