Skip to main content

PythPriceFeeds

Git Source

Inherits: PriceSubmodule

Title: PythPriceFeeds

Author: 0xJem

forge-lint: disable-start(mixed-case-function)

Provides prices derived from Pyth Network price feed(s)

State Variables

_PRICE_DATA_SIZE

uint256 internal constant _PRICE_DATA_SIZE = 128

ONE_FEED_PARAMS_LENGTH

The expected length of the encoded one feed parameters (address + bytes32 + uint48 + uint256 = 128 bytes)

uint256 internal constant ONE_FEED_PARAMS_LENGTH = 128

TWO_FEED_PARAMS_LENGTH

The expected length of the encoded two feed parameters (2x address + 2x bytes32 + 2x uint48 + 3x uint256 = 288 bytes)

uint256 internal constant TWO_FEED_PARAMS_LENGTH = 288

MAX_NEGATIVE_EXPONENT

The maximum negative exponent allowed to prevent overflow in 10**-expo calculation

int32 private constant MAX_NEGATIVE_EXPONENT = -30

Functions

constructor

constructor(Module parent_) Submodule(parent_);

SUBKEYCODE

20 byte identifier for the submodule. First 5 bytes must match PARENT().

function SUBKEYCODE() public pure override returns (SubKeycode);

VERSION

function VERSION() public pure override returns (uint8 major, uint8 minor);

_validatePriceFeedResult

Validates the result of the price feed

This function will revert if:

  • Price <= 0

  • Publish time is before the update threshold from the current time

  • Confidence interval exceeds the maximum allowed

function _validatePriceFeedResult(
address pyth_,
bytes32 priceFeedId_,
IPyth.Price memory priceData,
uint256 blockTimestamp,
uint48 paramsUpdateThreshold,
uint64 paramsMaxConfidence
) internal pure;

Parameters

NameTypeDescription
pyth_addressPyth contract address
priceFeedId_bytes32The price feed ID
priceDataIPyth.PriceThe price data returned by the Pyth contract
blockTimestampuint256The current block timestamp
paramsUpdateThresholduint48The maximum number of seconds elapsed since the last price feed update
paramsMaxConfidenceuint64The maximum confidence interval allowed (in Pyth feed scale, i.e., 10^expo)

_getFeedPrice

Retrieves the latest price returned by the specified Pyth price feed.

The result is validated using _validatePriceFeedResult, and will revert if invalid

function _getFeedPrice(
address pyth_,
bytes32 priceFeedId_,
uint48 updateThreshold_,
uint256 maxConfidence_,
uint8 outputDecimals_
) internal view returns (FeedResult memory);

Parameters

NameTypeDescription
pyth_addressPyth contract address
priceFeedId_bytes32The Pyth price feed ID
updateThreshold_uint48The maximum number of seconds elapsed since the last price feed update
maxConfidence_uint256The maximum confidence interval allowed (in output decimals scale)
outputDecimals_uint8The number of decimals to return the price in

Returns

NameTypeDescription
<none>FeedResultFeedResult The validated price and confidence in the scale of outputDecimals_

_validateDerivedConfidence

Validates a derived confidence interval

function _validateDerivedConfidence(uint256 confidence_, uint256 maxConfidence_) internal pure;

Parameters

NameTypeDescription
confidence_uint256The derived confidence interval (in output decimals scale)
maxConfidence_uint256The maximum confidence interval allowed (in output decimals scale)

_deriveMulConfidence

Derives the confidence interval for a multiplication result

function _deriveMulConfidence(FeedResult memory first, FeedResult memory second, uint8 outputDecimals_)
internal
pure
returns (uint256);

Parameters

NameTypeDescription
firstFeedResultFirst feed result in output decimals scale
secondFeedResultSecond feed result in output decimals scale
outputDecimals_uint8The number of output decimals

Returns

NameTypeDescription
<none>uint256uint256 The derived confidence interval in output decimals scale

_deriveDivConfidence

Derives the confidence interval for a division result

function _deriveDivConfidence(
FeedResult memory numerator,
FeedResult memory denominator,
uint8 outputDecimals_,
uint256 priceResult_
) internal pure returns (uint256);

Parameters

NameTypeDescription
numeratorFeedResultNumerator feed result in output decimals scale
denominatorFeedResultDenominator feed result in output decimals scale
outputDecimals_uint8The number of output decimals
priceResult_uint256The derived division price in output decimals scale

Returns

NameTypeDescription
<none>uint256uint256 The derived confidence interval in output decimals scale

getOneFeedPrice

Returns the price from a single Pyth feed, as specified in params_.

This function will revert if:

  • Any parameter is invalid

  • The exponent calculation would result in an overflow

  • The price feed's results are invalid

function getOneFeedPrice(address, uint8 outputDecimals_, bytes calldata params_) external view returns (uint256);

Parameters

NameTypeDescription
<none>address
outputDecimals_uint8The number of output decimals (assumed to be the same as PRICE decimals)
params_bytesPyth feed parameters of type OneFeedParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_

getTwoFeedPriceDiv

Returns the result of dividing the price from the first Pyth feed by the price from the second.

For example, passing in ETH/USD and DAI/USD will return the ETH/DAI price.

This function will revert if:

  • Any parameter is invalid

  • The exponent calculation would result in an overflow

  • Any of the price feeds' results are invalid

  • The denominator confidence interval is greater than or equal to the denominator price

  • The derived output confidence interval exceeds outputMaxConfidence

function getTwoFeedPriceDiv(address, uint8 outputDecimals_, bytes calldata params_)
external
view
returns (uint256);

Parameters

NameTypeDescription
<none>address
outputDecimals_uint8The number of output decimals (assumed to be the same as PRICE decimals)
params_bytesPyth feed parameters of type TwoFeedParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_.

getTwoFeedPriceMul

Returns the result of multiplying the price from the first Pyth feed by the price from the second.

For example, passing in ETH/DAI and DAI/USD will return the ETH/USD price.

This function will revert if:

  • Any parameter is invalid

  • The exponent calculation would result in an overflow

  • Any of the price feeds' results are invalid

  • The derived output confidence interval exceeds outputMaxConfidence

function getTwoFeedPriceMul(address, uint8 outputDecimals_, bytes calldata params_)
external
view
returns (uint256);

Parameters

NameTypeDescription
<none>address
outputDecimals_uint8The number of output decimals (assumed to be the same as PRICE decimals)
params_bytesPyth feed parameters of type TwoFeedParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_.

Errors

Pyth_ParamsInvalid

The provided parameters are invalid

error Pyth_ParamsInvalid(bytes params_);

Parameters

NameTypeDescription
params_bytesThe encoded parameters

Pyth_ParamsPythInvalid

A Pyth contract address specified in the parameters is invalid

error Pyth_ParamsPythInvalid(uint8 paramsIndex_, address feed_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
feed_addressThe address of the Pyth contract

Pyth_ParamsPriceFeedIdInvalid

A price feed ID specified in the parameters is invalid

error Pyth_ParamsPriceFeedIdInvalid(uint8 paramsIndex_, bytes32 priceFeedId_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
priceFeedId_bytes32The price feed ID

Pyth_ParamsUpdateThresholdInvalid

An update threshold specified in the parameters is invalid

This currently occurs if the update threshold is 0

error Pyth_ParamsUpdateThresholdInvalid(uint8 paramsIndex_, uint48 updateThreshold_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
updateThreshold_uint48The update threshold

Pyth_ParamsMaxConfidenceInvalid

A maximum confidence specified in the parameters is invalid

This currently occurs if the maximum confidence is 0

error Pyth_ParamsMaxConfidenceInvalid(uint8 paramsIndex_, uint256 maxConfidence_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
maxConfidence_uint256The maximum confidence (in output decimals scale)

Pyth_FeedInvalid

The price feed is invalid

This is triggered if the Pyth contract call reverts,

and indicates that the feed address is not a valid Pyth contract.

error Pyth_FeedInvalid(address pyth_, bytes32 priceFeedId_);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID

Pyth_FeedPriceInvalid

The price returned by the price feed is invalid

This could be because:

  • The price is <= 0
error Pyth_FeedPriceInvalid(address pyth_, bytes32 priceFeedId_, int64 price_);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID
price_int64The price returned by the price feed

Pyth_FeedPublishTimeStale

The data returned by the price feed is stale

This could be because:

  • The price feed was last updated before the update threshold
error Pyth_FeedPublishTimeStale(
address pyth_, bytes32 priceFeedId_, uint256 publishTime_, uint256 thresholdTimestamp_
);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID
publishTime_uint256The publish time of the price feed
thresholdTimestamp_uint256The earliest acceptable timestamp

Pyth_FeedConfidenceExcessive

The confidence interval exceeds the maximum allowed

error Pyth_FeedConfidenceExcessive(address pyth_, bytes32 priceFeedId_, uint64 confidence_, uint64 maxConfidence_);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID
confidence_uint64The confidence interval returned by the price feed (in Pyth feed scale, i.e., 10^expo)
maxConfidence_uint64The maximum confidence interval allowed (in Pyth feed scale, i.e., 10^expo)

Pyth_DerivedFeedConfidenceExcessive

The derived confidence interval exceeds the maximum allowed

error Pyth_DerivedFeedConfidenceExcessive(uint256 confidence_, uint256 maxConfidence_);

Parameters

NameTypeDescription
confidence_uint256The derived confidence interval (in output decimals scale)
maxConfidence_uint256The maximum derived confidence interval allowed (in output decimals scale)

Pyth_DerivedFeedConfidenceInvalid

The derived confidence interval is invalid

This occurs if the denominator confidence interval reaches or exceeds the denominator price.

error Pyth_DerivedFeedConfidenceInvalid(uint256 denominatorPrice_, uint256 denominatorConfidence_);

Parameters

NameTypeDescription
denominatorPrice_uint256The denominator price (in output decimals scale)
denominatorConfidence_uint256The denominator confidence interval (in output decimals scale)

Pyth_ExponentPositive

The exponent from the price feed is positive, which results in loss of precision

Positive expo values should not be accepted as they cause precision loss

error Pyth_ExponentPositive(address pyth_, bytes32 priceFeedId_, int32 expo_);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID
expo_int32The exponent from the price feed (must be <= 0)

Pyth_ExponentTooLarge

The exponent from the price feed is too negative, which results in overflow

Exponents more negative than -30 would cause 10**-expo to overflow uint256

error Pyth_ExponentTooLarge(address pyth_, bytes32 priceFeedId_, int32 expo_);

Parameters

NameTypeDescription
pyth_addressThe address of the Pyth contract
priceFeedId_bytes32The price feed ID
expo_int32The exponent from the price feed

Structs

OneFeedParams

Parameters for a single Pyth price feed

struct OneFeedParams {
address pyth;
bytes32 priceFeedId;
uint48 updateThreshold;
uint256 maxConfidence;
}

Properties

NameTypeDescription
pythaddressAddress of the Pyth contract
priceFeedIdbytes32The Pyth price feed ID
updateThresholduint48The maximum number of seconds elapsed since the last price feed update
maxConfidenceuint256The maximum confidence interval allowed (in output decimals scale)

TwoFeedParams

Parameters for two Pyth price feeds

struct TwoFeedParams {
address firstPyth;
bytes32 firstPriceFeedId;
uint48 firstUpdateThreshold;
uint256 firstMaxConfidence;
address secondPyth;
bytes32 secondPriceFeedId;
uint48 secondUpdateThreshold;
uint256 secondMaxConfidence;
uint256 outputMaxConfidence;
}

Properties

NameTypeDescription
firstPythaddressFirst: Address of the Pyth contract
firstPriceFeedIdbytes32First: The Pyth price feed ID
firstUpdateThresholduint48First: The maximum number of seconds elapsed since the last price feed update
firstMaxConfidenceuint256First: The maximum confidence interval allowed (in output decimals scale)
secondPythaddressSecond: Address of the Pyth contract
secondPriceFeedIdbytes32Second: The Pyth price feed ID
secondUpdateThresholduint48Second: The maximum number of seconds elapsed since the last price feed update
secondMaxConfidenceuint256Second: The maximum confidence interval allowed (in output decimals scale)
outputMaxConfidenceuint256The maximum confidence interval allowed for the derived output (in output decimals scale)

FeedResult

Price feed result in output decimals scale

struct FeedResult {
uint256 price;
uint256 confidence;
}

Properties

NameTypeDescription
priceuint256The price returned by the feed
confidenceuint256The confidence interval returned by the feed