Skip to main content

ChainlinkPriceFeeds

Git Source

Inherits: PriceSubmodule

Title: ChainlinkPriceFeeds

Author: 0xJem

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

Provides prices derived from Chainlink price feed(s)

State Variables

BASE_10_MAX_EXPONENT

Any token or pool with a decimal scale greater than this would result in an overflow

uint8 internal constant BASE_10_MAX_EXPONENT = 50

ONE_FEED_PARAMS_LENGTH

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

uint256 internal constant ONE_FEED_PARAMS_LENGTH = 64

TWO_FEED_PARAMS_LENGTH

The expected length of the encoded two feed parameters (2x address + 2x uint48 = 128 bytes)

uint256 internal constant TWO_FEED_PARAMS_LENGTH = 128

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:

  • Answer <= 0

  • Updated at timestamp before the update threshold from the current time

function _validatePriceFeedResult(
AggregatorV2V3Interface feed_,
FeedRoundData memory roundData,
uint256 blockTimestamp,
uint256 paramsUpdateThreshold
) internal pure;

Parameters

NameTypeDescription
feed_AggregatorV2V3InterfaceChainlink price feed
roundDataFeedRoundDataThe round data returned by the price feed
blockTimestampuint256The current block timestamp
paramsUpdateThresholduint256The maximum number of seconds elapsed since the last price feed update

_getFeedPrice

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

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

function _getFeedPrice(
AggregatorV2V3Interface feed_,
uint256 updateThreshold_,
uint8 feedDecimals_,
uint8 outputDecimals_
) internal view returns (uint256);

Parameters

NameTypeDescription
feed_AggregatorV2V3InterfaceChainlink price feed
updateThreshold_uint256The maximum number of seconds elapsed since the last price feed update
feedDecimals_uint8The number of decimals of the price feed
outputDecimals_uint8The number of decimals to return the price in

Returns

NameTypeDescription
<none>uint256uint256 The validated price in the scale of outputDecimals_

getOneFeedPrice

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

This function will revert if:

  • PRICE's priceDecimals or the feed's decimals are out of bounds and would lead to 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_bytesChainlink pool 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 Chainlink feed by the price from the second.

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

This function will revert if:

  • PRICE's priceDecimals or any of the feed's decimals are out of bounds and would lead to an overflow

  • Any of the price feeds' results are invalid

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_bytesChainlink pool 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 Chainlink 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:

  • PRICE's priceDecimals or any of the feed's decimals are out of bounds and would lead to an overflow

  • Any of the price feeds' results are invalid

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_bytesChainlink pool parameters of type TwoFeedParams

Returns

NameTypeDescription
<none>uint256uint256 Price in the scale of outputDecimals_

Errors

The provided parameters are invalid

error Chainlink_ParamsInvalid(bytes params_);

Parameters

NameTypeDescription
params_bytesThe encoded parameters

The number of decimals of the price feed is greater than the maximum allowed

error Chainlink_FeedDecimalsOutOfBounds(address feed_, uint8 feedDecimals_, uint8 maxDecimals_);

Parameters

NameTypeDescription
feed_addressThe address of the price feed
feedDecimals_uint8The number of decimals of the price feed
maxDecimals_uint8The maximum number of decimals allowed

The price returned by the price feed is invalid

This could be because:

  • The price is <= 0
error Chainlink_FeedPriceInvalid(address feed_, int256 price_);

Parameters

NameTypeDescription
feed_addressThe address of the price feed
price_int256The price returned by the price feed

The data returned by the price feed is stale

This could be because:

  • The price feed was last updated before the update threshold
error Chainlink_FeedRoundStale(address feed_, uint256 roundTimestamp_, uint256 thresholdTimestamp_);

Parameters

NameTypeDescription
feed_addressThe address of the price feed
roundTimestamp_uint256The timestamp of the round returned by the price feed
thresholdTimestamp_uint256The earliest acceptable timestamp

A price feed specified in the parameters is invalid

error Chainlink_ParamsFeedInvalid(uint8 paramsIndex_, address feed_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
feed_addressThe address of the price feed

An update threshold specified in the parameters is invalid

This currently occurs if the update threshold is 0

error Chainlink_ParamsUpdateThresholdInvalid(uint8 paramsIndex_, uint48 updateThreshold_);

Parameters

NameTypeDescription
paramsIndex_uint8The index of the parameter
updateThreshold_uint48The update threshold

The price feed is invalid

This is triggered if the price feed reverted when called,

and indicates that the feed address is not a Chainlink price feed.

error Chainlink_FeedInvalid(address feed_);

Parameters

NameTypeDescription
feed_addressThe address of the price feed

The number of decimals to return the price in is greater than the maximum allowed

error Chainlink_OutputDecimalsOutOfBounds(uint8 outputDecimals_, uint8 maxDecimals_);

Parameters

NameTypeDescription
outputDecimals_uint8The number of decimals to return the price in
maxDecimals_uint8The maximum number of decimals allowed

Structs

OneFeedParams

Parameters for a single Chainlink price feed

struct OneFeedParams {
AggregatorV2V3Interface feed;
uint48 updateThreshold;
}

Properties

NameTypeDescription
feedAggregatorV2V3InterfaceAddress of the Chainlink price feed
updateThresholduint48The maximum number of seconds elapsed since the last price feed update

TwoFeedParams

Parameters for a two Chainlink price feeds

struct TwoFeedParams {
AggregatorV2V3Interface firstFeed;
uint48 firstUpdateThreshold;
AggregatorV2V3Interface secondFeed;
uint48 secondUpdateThreshold;
}

Properties

NameTypeDescription
firstFeedAggregatorV2V3InterfaceFirst: Address of the Chainlink price feed
firstUpdateThresholduint48First: The maximum number of seconds elapsed since the last price feed update
secondFeedAggregatorV2V3InterfaceSecond: Address of the Chainlink price feed
secondUpdateThresholduint48Second: The maximum number of seconds elapsed since the last price feed update

FeedRoundData

Struct to represent data returned by the Chainlink price feed

struct FeedRoundData {
uint80 roundId;
int256 priceInt;
uint256 startedAt;
uint256 updatedAt;
}