OracleReader
The OracleReader contract aggregates price feeds from Chainlink oracles to provide accurate token valuations for collateral calculations, health factor monitoring, and liquidation decisions.
Overview
- Pattern: UUPS Upgradeable Proxy
- Solidity Version: 0.8.24
- Dependencies: OpenZeppelin Contracts v5, Chainlink v0.8
Key Functions
getPrice
function getPrice(address token) external view returns (uint256 price, uint8 decimals)
Returns the latest price for a token in USD.
Parameters:
token-- ERC-20 token address
Returns:
price-- Token price in USD (scaled bydecimals)decimals-- Number of decimal places in the price
getHealthFactor
function getHealthFactor(address user) external view returns (uint256)
Calculates the health factor for a user by comparing their weighted collateral value against their outstanding credit balance.
Returns: Health factor scaled to 18 decimals (1e18 = 1.0)
getCollateralValue
function getCollateralValue(address user) external view returns (uint256)
Returns the total USD value of a user's deposits weighted by collateral factors.
Price Feed Configuration
| Token | Chainlink Feed | Heartbeat |
|---|---|---|
| ETH | ETH/USD | 1 hour |
| USDC | USDC/USD | 24 hours |
| USDT | USDT/USD | 24 hours |
| DAI | DAI/USD | 1 hour |
| wstETH | wstETH/USD | 24 hours |
Staleness Checks
The contract enforces staleness thresholds on all price feeds:
require(
block.timestamp - updatedAt <= stalePriceThreshold,
"Price feed is stale"
);
If a price feed is stale, the transaction will revert rather than use outdated data.
Deviation Checks
Large price deviations between consecutive updates trigger additional validation:
if (deviation > maxDeviation) {
// Fall back to TWAP or revert
}
Adding Price Feeds
function setPriceFeed(
address token,
address feed,
uint256 staleThreshold
) external onlyOwner
New price feeds are configured by the contract owner. Each feed must be a valid Chainlink AggregatorV3Interface.
Security Considerations
- Staleness checks prevent using outdated prices
- Deviation checks catch oracle manipulation attempts
- Multiple independent price sources reduce single-point-of-failure risk
- Emergency price override available for owner in extreme market conditions