OracleMiddlewareWithPyth
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
Name | Type | Description |
---|---|---|
pythContract | address | Address of the Pyth contract. |
pythFeedId | bytes32 | The Pyth price feed ID for the asset. |
chainlinkPriceFeed | address | Address of the Chainlink price feed. |
chainlinkTimeElapsedLimit | uint256 | The 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
Name | Type | Description |
---|---|---|
ratio_ | uint16 | The 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
Name | Type | Description |
---|---|---|
newConfRatio | uint16 | the 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
Name | Type | Description |
---|---|---|
data | bytes | The signed price update data. |
actionTimestamp | uint128 | The timestamp of the action corresponding to the price. If zero, then we must accept all prices younger than the recent price delay. |
dir | PriceAdjustment | The direction for the low latency price adjustment. |
targetLimit | uint128 | The most recent timestamp a price can have (can be zero if actionTimestamp is zero). |
Returns
Name | Type | Description |
---|---|---|
price_ | PriceInfo | The 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
Name | Type | Description |
---|---|---|
data | bytes | The low latency data. |
dir | PriceAdjustment | The direction to adjust the price (when using a low latency price). |
Returns
Name | Type | Description |
---|---|---|
price_ | PriceInfo | The 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
Name | Type | Description |
---|---|---|
pythPrice | FormattedPythPrice | The formatted Pyth price object. |
dir | PriceAdjustment | The direction of the confidence interval to apply. |
Returns
Name | Type | Description |
---|---|---|
price_ | PriceInfo | The adjusted price according to the confidence interval and confidence ratio. |