OracleMiddlewareWithDataStreams

Git Source

Inherits: CommonOracleMiddleware, ChainlinkDataStreamsOracle, IOracleMiddlewareWithDataStreams

This contract is used to get the price of an asset from different oracles. It is used by the USDN protocol to get the price of the USDN underlying asset.

Functions

constructor

constructor(
    address pythContract,
    bytes32 pythFeedId,
    address chainlinkPriceFeed,
    uint256 chainlinkTimeElapsedLimit,
    address chainlinkProxyVerifierAddress,
    bytes32 chainlinkStreamId
)
    CommonOracleMiddleware(pythContract, pythFeedId, chainlinkPriceFeed, chainlinkTimeElapsedLimit)
    ChainlinkDataStreamsOracle(chainlinkProxyVerifierAddress, chainlinkStreamId);

Parameters

NameTypeDescription
pythContractaddressAddress of the Pyth contract.
pythFeedIdbytes32The Pyth price feed ID for the asset.
chainlinkPriceFeedaddressThe address of the Chainlink price feed.
chainlinkTimeElapsedLimituint256The duration after which a Chainlink price is considered stale.
chainlinkProxyVerifierAddressaddressThe address of the Chainlink proxy verifier contract.
chainlinkStreamIdbytes32The supported Chainlink data stream ID.

validationCost

Returns the cost of one price validation for the given action (in native token).

function validationCost(bytes calldata data, Types.ProtocolAction)
    public
    view
    override(CommonOracleMiddleware, IBaseOracleMiddleware)
    returns (uint256 result_);

Parameters

NameTypeDescription
databytesPrice data for which to get the fee.
<none>Types.ProtocolAction

Returns

NameTypeDescription
result_uint256cost_ The cost of one price validation (in native token).

setDataStreamsRecentPriceDelay

Sets the amount of time after which we do not consider a price as recent for Chainlink.

function setDataStreamsRecentPriceDelay(uint64 newDelay) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newDelayuint64The maximum age of a price to be considered recent.

_getLowLatencyPrice

Gets the price from the low-latency oracle.

function _getLowLatencyPrice(bytes calldata payload, uint128 actionTimestamp, PriceAdjustment dir, uint128 targetLimit)
    internal
    virtual
    override
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
payloadbytes
actionTimestampuint128The timestamp of the action corresponding to the price. If zero, then we must accept all prices younger than the recent price delay.
dirPriceAdjustmentThe direction for the low latency price adjustment.
targetLimituint128The most recent timestamp a price can have (can be zero if actionTimestamp is zero).

Returns

NameTypeDescription
price_PriceInfoThe price from the low-latency oracle, adjusted according to the price adjustment direction.

_getInitiateActionPrice

Gets the price for an initiate action of the protocol.

If the data parameter is not empty, validate the price with the low latency oracle. Else, get the on-chain price from ChainlinkOracle and compare its timestamp with the latest seen Pyth price (cached). If Pyth is more recent, we return it. Otherwise, we return the Chainlink price. For the latter, we don't have a price adjustment, so both neutralPrice and price are equal.

function _getInitiateActionPrice(bytes calldata data, PriceAdjustment dir)
    internal
    override
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesThe low latency data.
dirPriceAdjustmentThe direction to adjust the price (when using a low latency price).

Returns

NameTypeDescription
price_PriceInfoThe price to use for the user action.

_getLiquidationPrice

Gets the price from the low-latency oracle.

function _getLiquidationPrice(bytes calldata data) internal virtual override returns (PriceInfo memory price_);

Parameters

NameTypeDescription
databytesThe signed price update data.

Returns

NameTypeDescription
price_PriceInfoThe low-latency oracle price.

_convertPythPrice

Converts a formatted Pyth price into a PriceInfo.

function _convertPythPrice(FormattedPythPrice memory pythPrice) internal pure returns (PriceInfo memory price_);

Parameters

NameTypeDescription
pythPriceFormattedPythPriceThe formatted Pyth price containing the price and publish time.

Returns

NameTypeDescription
price_PriceInfoThe PriceInfo with the price, neutral price, and timestamp set from the Pyth price data.

_adjustDataStreamPrice

Applies the ask, bid or price according to the dir direction.

function _adjustDataStreamPrice(FormattedDataStreamsPrice memory formattedPrice, PriceAdjustment dir)
    internal
    pure
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
formattedPriceFormattedDataStreamsPriceThe Chainlink data streams formatted price.
dirPriceAdjustmentThe direction to adjust the price.

Returns

NameTypeDescription
price_PriceInfoThe adjusted price according to the direction.