Проверенный контрактЧастичноеGPL-3.0
ChainlinkOracleFeed
Адрес0x7c19b0cf11b396dfd4b9e8a7274e22b8948a6fa4
- Баланс
0 ETH
- Транзакций
0
- Компилятор
solc 0.8.30
- Тот же код
ещё 6 контрактов
Подробности
- Компилятор
- solc 0.8.30+commit.73712a01
- Язык
- solidity
- Оптимизация
- Да, 1 500 прогонов
- Версия EVM
- osaka
- Через IR
- да
- Лицензия
- GPL-3.0
- Совпадение
- Частичноекод создания: да, код в сети: да
- Проверен на Sourcify
- Создан в блоке
- 26 297 114
- Транзакция создания
- 0x1a3a6fe7ca…8df16fe880
2 файла
- src/oracle/ChainlinkOracleFeed.solглавный5,26 КБ
- src/oracle/IOracleFeed.sol1,89 КБ
src/oracle/ChainlinkOracleFeed.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.30;
import {IOracleFeed} from "./IOracleFeed.sol";
/**
* @title AggregatorV3Interface
* @notice Interface for Chainlink V3 price aggregators
* @dev This interface provides access to Chainlink's latest price data
* including price, timestamp, and round information.
*/
interface AggregatorV3Interface {
/**
* @notice Gets the latest round data from the aggregator
* @return roundId The round ID
* @return answer The price answer
* @return startedAt Timestamp when the round started
* @return updatedAt Timestamp when the round was updated
* @return answeredInRound The round in which the answer was computed
*/
function latestRoundData()
external
view
returns (uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound);
/**
* @notice Gets the number of decimals used by the aggregator
* @return Number of decimals for the price data
*/
function decimals() external view returns (uint8);
}
/**
* @title ChainlinkOracleFeed
* @notice Oracle feed implementation for Chainlink price feeds
* @dev This contract integrates with Chainlink's AggregatorV3 interface to provide
* price data for collateral assets in the minting system.
* @dev Implements comprehensive price validation including staleness checks and
* decimal normalization to 18 decimals.
* @dev Security features include timestamp validation and price sanity checks.
* @dev This oracle feed can also be used with Redstone push based oracles.
*
* Key Features:
* - Chainlink AggregatorV3 integration
* - Automatic decimal normalization to 18 decimals
* - Staleness threshold enforcement (1 day and 1 minute)
* - Price validation and error handling
* - Event emission for price updates
*
* @custom:security This contract follows security best practices including:
* - Staleness threshold enforcement
* - Price validation and sanitization
* - Decimal precision handling
* - Comprehensive error handling
*/
contract ChainlinkOracleFeed is IOracleFeed {
/*//////////////////////////////////////////////////////////////
STORAGE
//////////////////////////////////////////////////////////////*/
/// @notice Chainlink aggregator contract for price data
AggregatorV3Interface public immutable oracle;
/// @notice Maximum allowed staleness for oracle prices
uint256 public immutable oracleStalenessThreshold;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
/**
* @notice Initializes the ChainlinkOracleFeed contract
* @param _oracle Address of the Chainlink aggregator contract
* @dev Reverts if oracle address is zero
* @dev Sets the oracle interface for price data access
*/
constructor(address _oracle, uint256 _oracleStalenessThreshold) {
if (_oracle == address(0)) revert InvalidOracleAddress();
oracle = AggregatorV3Interface(_oracle);
oracleStalenessThreshold = _oracleStalenessThreshold;
}
/*//////////////////////////////////////////////////////////////
ORACLE FUNCTIONS
//////////////////////////////////////////////////////////////*/
/**
* @notice Gets the latest validated price from Chainlink
* @dev Performs comprehensive validation including:
* - Price validity (positive values)
* - Timestamp validation (must be current or in the past)
* - Staleness checks (within threshold)
* - Decimal normalization to 18 decimals
* @return price The validated price in 18 decimals
* @return updatedAt Timestamp when the price was last updated
* @dev Reverts with InvalidOraclePrice if validation fails
* @dev Emits OraclePrice event with the validated price
* @dev Handles different decimal precisions automatically
*/
function getPrice() external returns (uint256, uint256) {
(, int256 answer,, uint256 updatedAt,) = oracle.latestRoundData();
// Validate price is positive
if (answer <= 0) revert InvalidOraclePrice();
// Check if price timestamp is in the future (should not be possible with Chainlink)
// Allow updatedAt == block.timestamp as Chainlink sets timestamps to block.timestamp when mined
if (updatedAt > block.timestamp) revert InvalidOraclePrice();
// Check if price is too old
uint256 timeDiff = block.timestamp - updatedAt;
if (timeDiff > oracleStalenessThreshold) revert InvalidOraclePrice();
// Convert to 18 decimals
uint8 oracleDecimals = oracle.decimals();
uint256 price = uint256(answer);
if (oracleDecimals < 18) {
// Scale up if oracle has fewer decimals
price = price * (10 ** (18 - oracleDecimals));
} else if (oracleDecimals > 18) {
// Scale down if oracle has more decimals
price = price / (10 ** (oracleDecimals - 18));
}
emit OraclePrice(price);
return (price, updatedAt);
}
}
src/oracle/IOracleFeed.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.30;
/**
* @title IOracleFeed
* @notice Interface for oracle price feeds in the on-chain minting system
* @dev This interface defines the standard API for oracle implementations that provide
* price data for collateral assets in the minting system.
* @dev Implementations should ensure price accuracy, staleness checks, and proper error handling.
* @dev The interface is designed to be compatible with various oracle providers including
* Chainlink, Pyth, and custom implementations.
*
* Key Requirements:
* - Price validation and staleness checks
* - Timestamp validation
* - Proper error handling for invalid prices
* - Event emission for price updates
*/
interface IOracleFeed {
/**
* @notice Error thrown when oracle price is invalid
* @dev Used when price is zero, negative, or otherwise unusable
*/
error InvalidOraclePrice();
/**
* @notice Error thrown when oracle address is invalid
* @dev Used when oracle contract address is zero or invalid
*/
error InvalidOracleAddress();
/**
* @notice Emitted when a new oracle price is received
* @param price The validated price from the oracle
*/
event OraclePrice(uint256 price);
/**
* @notice Gets the latest price and timestamp from the oracle
* @dev This function should perform all necessary validation including:
* - Price validity checks
* - Staleness validation
* - Timestamp verification
* @return price The validated price (18 decimals)
* @return updatedAt Timestamp when the price was last updated
* @dev Reverts with InvalidOraclePrice if price validation fails
* @dev Should emit OraclePrice event with the validated price
*/
function getPrice() external returns (uint256 price, uint256 updatedAt);
}
Настройки компилятораПоказать
{
"viaIR": true,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"libraries": {},
"optimizer": {
"runs": 1500,
"enabled": true
},
"evmVersion": "osaka",
"remappings": [
"ds-test/=lib/forge-std/lib/ds-test/src/",
"forge-std/=lib/forge-std/src/",
"@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@layerzerolabs/oft-evm-upgradeable/=lib/devtools/packages/oft-evm-upgradeable/",
"@layerzerolabs/oft-evm/=lib/devtools/packages/oft-evm/",
"@layerzerolabs/oapp-evm-upgradeable/=lib/devtools/packages/oapp-evm-upgradeable/",
"@layerzerolabs/oapp-evm/=lib/devtools/packages/oapp-evm/",
"@layerzerolabs/lz-evm-protocol-v2/=lib/layerzero-v2/packages/layerzero-v2/evm/protocol/",
"@layerzerolabs/lz-evm-messagelib-v2/=lib/layerzero-v2/packages/layerzero-v2/evm/messagelib/",
"solidity-bytes-utils/contracts/=test/fuzzing/utils/",
"@layerzerolabs/lz-evm-v1-0.7/contracts/interfaces/=test/fuzzing/utils/",
"@perimetersec/fuzzlib/=lib/fuzzlib/",
"solady/=lib/solady/src/",
"@layerzerolabs/test-devtools-evm-foundry/=lib/devtools/packages/test-devtools-evm-foundry/",
"devtools/=lib/devtools/packages/toolbox-foundry/src/",
"erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
"fuzzlib/=lib/fuzzlib/src/",
"halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
"layerzero-v2/=lib/layerzero-v2/",
"lz-evm-v1-0.7/=lib/lz-evm-v1-0.7/contracts/",
"openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
"openzeppelin-contracts/=lib/openzeppelin-contracts/"
]
}Исходный код и метаданные: Sourcify.
Исходный код принадлежит авторам контракта.