Skip to content
Lattice

ChainlinkAdapterLib

Git Source

Title: ChainlinkAdapterLib

Authors: David Dada <daveproxy80@gmail.com> (https://github.com/dadadave80), Modified from Chainlink (https://github.com/smartcontractkit/chainlink-evm/blob/develop/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol)

Library that wraps Chainlink AggregatorV3 price feeds with per-feed staleness configuration and WAD normalisation.

Functions

chainlinkAdapterStorage

Returns the ERC-7201 storage struct for ChainlinkAdapter.

function chainlinkAdapterStorage() internal pure returns (ChainlinkAdapterStorage storage $);

Returns

NameTypeDescription
$ChainlinkAdapterStorage

__ChainlinkAdapter_init

Registers the IChainlinkAdapter ERC-165 interface.

Must be called between preInitializer / postInitializer.

function __ChainlinkAdapter_init() internal;

registerInterface

Writes true to the ERC-165 map slot for IChainlinkAdapter.

function registerInterface() internal;

getFeed

Returns the registered feed address and staleness threshold.

function getFeed(bytes32 key) internal view returns (address feed, uint48 maxStaleness);

Parameters

NameTypeDescription
keybytes32The feed identifier.

Returns

NameTypeDescription
feedaddressAddress of the AggregatorV3 feed.
maxStalenessuint48Maximum age in seconds before data is stale.

latestAnswerRaw

Returns the raw latest price, update timestamp, and feed decimals.

function latestAnswerRaw(bytes32 key) internal view returns (int256 answer, uint256 updatedAt, uint8 decimals_);

Parameters

NameTypeDescription
keybytes32The feed identifier.

Returns

NameTypeDescription
answerint256Raw answer from the feed (must be > 0).
updatedAtuint256Timestamp of the last update.
decimals_uint8Number of decimals used by this feed.

latestAnswer

Returns the latest price normalised to 18 decimal places (WAD).

Reverts ChainlinkInvalidAnswer with the raw answer when a feed with more than 18 decimals reports a price that scales down to zero.

function latestAnswer(bytes32 key) internal view returns (int256 answerWad);

Parameters

NameTypeDescription
keybytes32The feed identifier.

Returns

NameTypeDescription
answerWadint256The price scaled to 1e18.

registerFeed

Registers a Chainlink price feed under the given key.

Caller must hold DEFAULT_ADMIN_ROLE. Caches the feed's decimals() to avoid a read on every price query. Reverts if feed is the zero address or maxStaleness is zero.

function registerFeed(bytes32 key, address feed, uint48 maxStaleness) internal;

Parameters

NameTypeDescription
keybytes32Arbitrary identifier for this feed.
feedaddressAddress of the AggregatorV3 contract.
maxStalenessuint48Maximum age (seconds) before an answer is stale.

unregisterFeed

Removes a registered price feed.

Caller must hold DEFAULT_ADMIN_ROLE.

function unregisterFeed(bytes32 key) internal;

Parameters

NameTypeDescription
keybytes32The feed identifier to remove.