ChainlinkOracle

Git Source

Inherits: IChainlinkOracle, IOracleMiddlewareErrors

This contract is used to get the price of an asset from Chainlink. It is used by the USDN protocol to get the price of the USDN underlying asset, and by the LiquidationRewardsManager to get the price of the gas.

State Variables

PRICE_TOO_OLD

The sentinel value returned instead of the price if the data from the oracle is too old.

int256 public constant PRICE_TOO_OLD = type(int256).min;

_priceFeed

The Chainlink price feed aggregator contract.

AggregatorV3Interface internal immutable _priceFeed;

_timeElapsedLimit

Tolerated elapsed time until we consider the data too old.

uint256 internal _timeElapsedLimit;

Functions

constructor

constructor(address chainlinkPriceFeed, uint256 timeElapsedLimit);

Parameters

NameTypeDescription
chainlinkPriceFeedaddressAddress of the price feed.
timeElapsedLimituint256Tolerated elapsed time before the data is considered invalid.

getChainlinkDecimals

Gets the number of decimals of the asset from Chainlink.

function getChainlinkDecimals() public view returns (uint256 decimals_);

Returns

NameTypeDescription
decimals_uint256The number of decimals of the asset.

getPriceFeed

Gets the Chainlink price feed aggregator contract address.

function getPriceFeed() public view returns (AggregatorV3Interface priceFeed_);

Returns

NameTypeDescription
priceFeed_AggregatorV3InterfaceThe address of the Chainlink price feed contract.

getChainlinkTimeElapsedLimit

Gets the duration after which the Chainlink data is considered stale or invalid.

function getChainlinkTimeElapsedLimit() external view returns (uint256 limit_);

Returns

NameTypeDescription
limit_uint256The price validity duration.

_getChainlinkLatestPrice

Gets the latest price of the asset from Chainlink.

If the price is too old, the returned price will be equal to PRICE_TOO_OLD.

function _getChainlinkLatestPrice() internal view virtual returns (ChainlinkPriceInfo memory price_);

Returns

NameTypeDescription
price_ChainlinkPriceInfoThe price of the asset.

_getFormattedChainlinkLatestPrice

Gets the latest price of the asset from Chainlink, formatted to the specified number of decimals.

function _getFormattedChainlinkLatestPrice(uint256 middlewareDecimals)
    internal
    view
    returns (ChainlinkPriceInfo memory formattedPrice_);

Parameters

NameTypeDescription
middlewareDecimalsuint256The number of decimals to format the price to.

Returns

NameTypeDescription
formattedPrice_ChainlinkPriceInfoThe formatted price of the asset.

_getFormattedChainlinkPrice

Gets the price of the asset at the specified round ID, formatted to the specified number of decimals.

function _getFormattedChainlinkPrice(uint256 middlewareDecimals, uint80 roundId)
    internal
    view
    returns (ChainlinkPriceInfo memory formattedPrice_);

Parameters

NameTypeDescription
middlewareDecimalsuint256The number of decimals to format the price to.
roundIduint80The targeted round ID.

Returns

NameTypeDescription
formattedPrice_ChainlinkPriceInfoThe formatted price of the asset.

_getChainlinkPrice

Gets the price of the asset at the specified round ID.

function _getChainlinkPrice(uint80 roundId) internal view virtual returns (ChainlinkPriceInfo memory price_);

Parameters

NameTypeDescription
roundIduint80The Chainlink roundId price.

Returns

NameTypeDescription
price_ChainlinkPriceInfoThe price of the asset.