# ChainLinkDataConsumer

## Introduction

The `ChainLinkDataConsumer` contract is a utility component within the protocol, designed to provide real-time, reliable token price data using Chainlink’s decentralized oracle network. Its primary purpose is to normalize and convert the yield generated from different staking tokens (e.g., stETH, wBTC, cbETH) into a common base currency, such as USD. This normalization is crucial for accurately aggregating yield across various DepositPools and ensuring fair distribution of MOR rewards, regardless of the asset type contributed by users.

The contract supports the composition of multiple Chainlink feeds into a single price path (e.g., `cbETH/ETH/USD`), which allows for flexible and accurate multi-hop price resolution. These paths are referenced via unique string identifiers and stored as a hash in the contract’s mapping structure. Though the contract does not calculate MOR rewards directly, it provides the foundational price data necessary for consistent yield evaluation.

### Key Responsibilities

1. Composable price path resolution: supports multi-step Chainlink feed composition using stored feed sequences.
2. Accurate yield normalization: normalizes yield values to a unified base unit (18 decimals, typically USD), enabling consistent reward calculations.
3. Dynamic feed configuration: allows the owner to register or update Chainlink data feeds by providing token path identifiers and feed arrays.
4. Chainlink oracle integration: queries live Chainlink aggregators and computes composite price outputs with correct decimal adjustment and error handling.

## Storage

### dataFeeds

The Chainlink data feed addresses. `dataFeeds[<pathId>]`

```solidity
mapping(bytes32 => address[]) public dataFeeds;
```

### allowedPriceUpdateDelay

The maximum allowed delay in seconds for the price update between `ChainLink` data and current timestamp in seconds. Where `address` - feed, `uint64` - timestamp.

```solidity
mapping(address => uint64) public allowedPriceUpdateDelay;
```

## Write functions for the contract owner

### updateDataFeeds

Updates the list of Chainlink price feed contracts for specified paths. Only callable by the contract owner.

```solidity
function updateDataFeeds(
  string[] calldata paths_,
  address[][] calldata feeds_
) external onlyOwner
```

| Name     | Description                                                                                                       |
| -------- | ----------------------------------------------------------------------------------------------------------------- |
| `paths_` | Array of unique feed path strings (e.g., \["ETH/USD"])                                                            |
| `feeds_` | Array of feed address chains, each corresponding to a path. Can include multiple hops (e.g., \[ETH/USD, USD/DAI]) |

### setAllowedPriceUpdateDelay

The function to set the maximum delay between the returned update time and the current time. For example, if set to 120 seconds, the price returned by `ChainLink` should not be older than 120 seconds from the time of the call.

```solidity
function setAllowedPriceUpdateDelay(
  address feed_,
  uint64 allowedPriceUpdateDelay_
) external onlyOwner;
```

| Name                       | Description                                   |
| -------------------------- | --------------------------------------------- |
| `feed_`                    | The feed address from the ChainLink protocol. |
| `allowedPriceUpdateDelay_` | The seconds.                                  |

## Read functions

### getPathId

Returns a `bytes32` hash of a feed path string. Used to access stored feeds in the mapping.

```solidity
function getPathId(string memory path_) public pure returns (bytes32)
```

| Name    | Description                            |
| ------- | -------------------------------------- |
| `path_` | The string representing the feed path. |

### decimals

Returns the standard number of decimals used for price normalization. Returns `18` as standard

```solidity
function decimals() public pure returns (uint8)
```

### getChainLinkDataFeedLatestAnswer

Fetches the latest value from the Chainlink feeds assigned to a specific path ID. Handles multi-hop price feeds (e.g., ETH → USD → EUR).

```solidity
function getChainLinkDataFeedLatestAnswer(
  bytes32 pathId_
) external view returns (uint256)
```

| Name      | Description                           |
| --------- | ------------------------------------- |
| `pathId_` | The hashed identifier of a feed path. |

### supportsInterface

Used for interface detection (ERC165). Returns true if the contract supports a specific `interfaceId_`. Supports `IChainLinkDataConsumer`, `IERC165`.

```solidity
function supportsInterface(bytes4 interfaceId_) external pure returns (bool)
```

### version

Returns the current version of the contract.

```solidity
function version() external pure returns (uint256)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gitbook.mor.org/smart-contracts/documentation/distribution-protocol/v7-protocol/contracts/chainlinkdataconsumer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
