PythOracle

Git Source

Inherits: IPythOracle

This contract is used to get the price of the asset that corresponds to the stored feed ID.

Is implemented by the CommonOracleMiddleware contract.

State Variables

_pythFeedId

The ID of the Pyth price feed.

bytes32 internal immutable _pythFeedId;

_pyth

The address of the Pyth contract.

IPyth internal immutable _pyth;

_pythRecentPriceDelay

The maximum age of a recent price to be considered valid.

uint64 internal _pythRecentPriceDelay = 45 seconds;

Functions

constructor

constructor(address pythAddress, bytes32 pythFeedId);

Parameters

NameTypeDescription
pythAddressaddressThe address of the Pyth contract.
pythFeedIdbytes32The ID of the Pyth price feed.

getPyth

Gets the Pyth contract address.

function getPyth() external view returns (IPyth);

Returns

NameTypeDescription
<none>IPythpyth_ The Pyth contract address.

getPythFeedId

Gets the ID of the price feed queried by this contract.

function getPythFeedId() external view returns (bytes32);

Returns

NameTypeDescription
<none>bytes32feedId_ The Pyth price feed ID.

getPythRecentPriceDelay

Gets the recent price delay.

function getPythRecentPriceDelay() external view returns (uint64);

Returns

NameTypeDescription
<none>uint64recentPriceDelay_ The maximum age of a recent price to be considered valid.

_getPythPrice

Gets the price of the asset from the stored Pyth price feed.

function _getPythPrice(bytes calldata priceUpdateData, uint128 targetTimestamp, uint128 targetLimit)
    internal
    returns (PythStructs.Price memory);

Parameters

NameTypeDescription
priceUpdateDatabytesThe data required to update the price feed.
targetTimestampuint128The timestamp of the price in the given priceUpdateData. If zero, then we accept all recent prices.
targetLimituint128The most recent timestamp a price can have. Can be zero if targetTimestamp is zero.

Returns

NameTypeDescription
<none>PythStructs.Priceprice_ The raw price of the asset returned by Pyth.

_getFormattedPythPrice

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

function _getFormattedPythPrice(
    bytes calldata priceUpdateData,
    uint128 targetTimestamp,
    uint256 middlewareDecimals,
    uint128 targetLimit
) internal returns (FormattedPythPrice memory price_);

Parameters

NameTypeDescription
priceUpdateDatabytesThe data required to update the price feed.
targetTimestampuint128The timestamp of the price in the given priceUpdateData. If zero, then we accept all recent prices.
middlewareDecimalsuint256The number of decimals to format the price to.
targetLimituint128The most recent timestamp a price can have. Can be zero if targetTimestamp is zero.

Returns

NameTypeDescription
price_FormattedPythPriceThe Pyth price formatted with middlewareDecimals.

_formatPythPrice

Formats a Pyth price object to normalize to the specified number of decimals.

function _formatPythPrice(PythStructs.Price memory pythPrice, uint256 middlewareDecimals)
    internal
    pure
    returns (FormattedPythPrice memory price_);

Parameters

NameTypeDescription
pythPricePythStructs.PriceA Pyth price object.
middlewareDecimalsuint256The number of decimals to format the price to.

Returns

NameTypeDescription
price_FormattedPythPriceThe Pyth price formatted with middlewareDecimals.

_getPythUpdateFee

Gets the fee required to update the price feed.

function _getPythUpdateFee(bytes calldata priceUpdateData) internal view returns (uint256);

Parameters

NameTypeDescription
priceUpdateDatabytesThe data required to update the price feed.

Returns

NameTypeDescription
<none>uint256updateFee_ The fee required to update the price feed.

_getLatestStoredPythPrice

Gets the latest seen (cached) price from the Pyth contract.

function _getLatestStoredPythPrice(uint256 middlewareDecimals)
    internal
    view
    returns (FormattedPythPrice memory price_);

Parameters

NameTypeDescription
middlewareDecimalsuint256The number of decimals for the returned price.

Returns

NameTypeDescription
price_FormattedPythPriceThe formatted cached Pyth price, or all-zero values if there was no valid Pyth price on-chain.