Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

OracleLib

Git Source

Title: OracleLib

Library for common oracle validation patterns.

Provides reusable functions for sequencer checks, staleness validation, and price validation.

Functions

checkSequencer

Check if the L2 sequencer is up and grace period has passed.

Skips check if sequencerFeed is address(0) (e.g., on L1 or testnets).

function checkSequencer(
    AggregatorV3Interface sequencerFeed,
    uint256 gracePeriod
) internal view;

Parameters

NameTypeDescription
sequencerFeedAggregatorV3InterfaceThe Chainlink sequencer uptime feed.
gracePerioduint256The grace period in seconds after sequencer comes back up.

checkStaleness

Check if the oracle price is stale.

function checkStaleness(
    uint256 updatedAt,
    uint256 timeout
) internal view;

Parameters

NameTypeDescription
updatedAtuint256The timestamp when the price was last updated.
timeoutuint256The maximum age in seconds for a valid price.

checkStalenessAt

Check staleness relative to a specific reference timestamp instead of block.timestamp.

function checkStalenessAt(
    uint256 updatedAt,
    uint256 timeout,
    uint256 referenceTime
) internal pure;

Parameters

NameTypeDescription
updatedAtuint256The timestamp when the price was last updated.
timeoutuint256The maximum age in seconds for a valid price.
referenceTimeuint256The timestamp to measure staleness against.

verifyHistoricalPrice

Verifies a caller-provided hint round is the correct price at a target timestamp.

Avoids backward traversal that breaks on Chainlink phase boundaries.

function verifyHistoricalPrice(
    AggregatorV3Interface feed,
    uint256 targetTimestamp,
    uint80 hintRoundId
) internal view returns (int256 price, uint256 updatedAt);

Parameters

NameTypeDescription
feedAggregatorV3InterfaceThe Chainlink price feed.
targetTimestampuint256The timestamp to look up the price for.
hintRoundIduint80The round ID that the caller claims was active at targetTimestamp.

Returns

NameTypeDescription
priceint256The price at the target timestamp.
updatedAtuint256The timestamp of the round found.

getValidatedPrice

Get a validated price from an oracle with staleness and sequencer checks.

Reverts on zero or negative prices to prevent operations during oracle failures.

function getValidatedPrice(
    AggregatorV3Interface oracle,
    AggregatorV3Interface sequencerFeed,
    uint256 gracePeriod,
    uint256 timeout
) internal view returns (uint256 price);

Parameters

NameTypeDescription
oracleAggregatorV3InterfaceThe price oracle.
sequencerFeedAggregatorV3InterfaceThe sequencer uptime feed (can be address(0) to skip).
gracePerioduint256The sequencer grace period in seconds.
timeoutuint256The staleness timeout in seconds.

Returns

NameTypeDescription
priceuint256The validated price.

tryGetValidatedPrice

Non-reverting variant of getValidatedPrice for best-effort oracle reads.

function tryGetValidatedPrice(
    AggregatorV3Interface oracle,
    AggregatorV3Interface sequencerFeed,
    uint256 gracePeriod,
    uint256 timeout
) internal view returns (bool success, uint256 price);

Returns

NameTypeDescription
successboolTrue if the price passed all validation checks.
priceuint256The validated price (0 if success is false).

Errors

OracleLib__SequencerDown

error OracleLib__SequencerDown();

OracleLib__SequencerGracePeriod

error OracleLib__SequencerGracePeriod();

OracleLib__StalePrice

error OracleLib__StalePrice();

OracleLib__InvalidPrice

error OracleLib__InvalidPrice();

OracleLib__NoPriceAtExpiry

error OracleLib__NoPriceAtExpiry();

OracleLib__InsufficientGas

error OracleLib__InsufficientGas();