ChainlinkAdapterLib
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
| Name | Type | Description |
|---|---|---|
| $ | 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
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The feed identifier. |
Returns
| Name | Type | Description |
|---|---|---|
| feed | address | Address of the AggregatorV3 feed. |
| maxStaleness | uint48 | Maximum 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
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The feed identifier. |
Returns
| Name | Type | Description |
|---|---|---|
| answer | int256 | Raw answer from the feed (must be > 0). |
| updatedAt | uint256 | Timestamp of the last update. |
| decimals_ | uint8 | Number 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
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The feed identifier. |
Returns
| Name | Type | Description |
|---|---|---|
| answerWad | int256 | The 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
| Name | Type | Description |
|---|---|---|
| key | bytes32 | Arbitrary identifier for this feed. |
| feed | address | Address of the AggregatorV3 contract. |
| maxStaleness | uint48 | Maximum 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
| Name | Type | Description |
|---|---|---|
| key | bytes32 | The feed identifier to remove. |