OracleMiddlewareWithPyth

Git Source

Inherits: CommonOracleMiddleware, IOracleMiddlewareWithPyth

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.

State Variables

BPS_DIVISOR

Gets the denominator for the variables using basis points as a unit.

uint16 public constant BPS_DIVISOR = 10_000;

MAX_CONF_RATIO

Gets the maximum value for _confRatioBps.

uint16 public constant MAX_CONF_RATIO = BPS_DIVISOR * 2;

_confRatioBps

Ratio to applied to the Pyth confidence interval (in basis points).

uint16 internal _confRatioBps = 4000;

Functions

constructor

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

Parameters

NameTypeDescription
pythContractaddressAddress of the Pyth contract.
pythFeedIdbytes32The Pyth price feed ID for the asset.
chainlinkPriceFeedaddressAddress of the Chainlink price feed.
chainlinkTimeElapsedLimituint256The duration after which a Chainlink price is considered stale.

getConfRatioBps

Gets the confidence ratio.

This ratio is used to apply a specific portion of the confidence interval provided by an oracle, which is used to adjust the precision of predictions or estimations.

function getConfRatioBps() external view returns (uint16 ratio_);

Returns

NameTypeDescription
ratio_uint16The confidence ratio (in basis points).

setConfRatio

Sets the confidence ratio.

The new value should be lower than MAX_CONF_RATIO.

function setConfRatio(uint16 newConfRatio) external onlyRole(ADMIN_ROLE);

Parameters

NameTypeDescription
newConfRatiouint16the new confidence ratio.

_getLowLatencyPrice

Gets the price from the low-latency oracle.

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

Parameters

NameTypeDescription
databytesThe signed price update data.
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.

_adjustPythPrice

Applies the confidence interval in the dir direction, scaled by the configured _confRatioBps.

function _adjustPythPrice(FormattedPythPrice memory pythPrice, PriceAdjustment dir)
    internal
    view
    returns (PriceInfo memory price_);

Parameters

NameTypeDescription
pythPriceFormattedPythPriceThe formatted Pyth price object.
dirPriceAdjustmentThe direction of the confidence interval to apply.

Returns

NameTypeDescription
price_PriceInfoThe adjusted price according to the confidence interval and confidence ratio.