> For the complete documentation index, see [llms.txt](https://docs.elara.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.elara.fi/protocol-reference/reference/api-reference/oracle/contract.priceoracle.md).

# PriceOracle

[Git Source](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/9301de6eec32ceafb4021f4e7da883b507799ebc/contracts/oracle/PriceOracle.sol)

**Inherits:** [ElaraBase](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/docs/latest/contracts/base/ElaraBase.sol/abstract.ElaraBase.md), IPriceOracle

**Title:** Price Oracle Contract

Extensible price oracle with support for multiple oracle types

* Currently supports Chainlink with ability to add more oracle types
* Invariants:
* Only authorized accounts can configure token pricing
* Prices are always returned with 18 decimals precision
* Oracle prices must be positive and not stale
* Upgradeability:
* Uses {ElaraBase} for UUPS upgradeability pattern
* Initializer must be called exactly once; preserve storage layout
* Access Control:
* Configuration functions restricted via [ElaraBase-restricted](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/docs/latest/lib/openzeppelin-contracts-upgradeable/contracts/access/manager/AccessManagedUpgradeable.sol/abstract.AccessManagedUpgradeable.md#restricted) modifier

**Note:** security-contact: <security@elara.finance>

## State Variables

### DECIMALS

Number of decimals used for price values

All prices returned by this oracle are normalized to 18 decimals

```solidity
uint8 private constant DECIMALS = 18
```

### defaultHeartbeat

Default maximum acceptable age of price data in seconds

Used when a token config omits a heartbeat; typically 3600 seconds (1 hour)

```solidity
uint256 private defaultHeartbeat
```

### oracleTypes

Oracle type for each token

Maps token address to oracle type (NONE if not configured)

```solidity
mapping(address => OracleType) public oracleTypes
```

### chainlinkConfigs

Chainlink configurations

Only populated for tokens with OracleType.CHAINLINK

```solidity
mapping(address => ChainlinkConfig) private chainlinkConfigs
```

## Functions

### initialize

Initialize the price oracle with default configuration

Security:

* Only callable during initialization via {initializer} modifier
* Sets up the base contract via [ElaraBase-\_\_ElaraBase\_init](https://github.com/TrueFi-Protocol/elara-contracts-v2/blob/docs/latest/contracts/base/ElaraBase.sol/abstract.ElaraBase.md#__elarabase_init)
* Sets defaultHeartbeat to 3600 seconds (1 hour)
* Critical for establishing access control; must be called exactly once per proxy

```solidity
function initialize(address _accessManager) external initializer;
```

**Parameters**

| Name             | Type      | Description                            |
| ---------------- | --------- | -------------------------------------- |
| `_accessManager` | `address` | Address of the access manager contract |

### configureChainlink

```solidity
function configureChainlink(address token, address feed, uint256 heartbeat) external override restricted;
```

### setDefaultHeartbeat

```solidity
function setDefaultHeartbeat(uint256 newHeartbeat) external override restricted;
```

### deactivateToken

```solidity
function deactivateToken(address token) external override restricted;
```

### getPrice

```solidity
function getPrice(address token) external view override returns (uint256);
```

### decimals

```solidity
function decimals() external pure override returns (uint8);
```

### getOracleType

```solidity
function getOracleType(address token) external view override returns (OracleType);
```

### \_getChainlinkPrice

Fetches price from Chainlink feed and validates it

Reverts if price is non-positive or stale. Any revert from the external feed call (including malformed data or feed downtime) is caught and re-thrown as {InvalidPrice}.

```solidity
function _getChainlinkPrice(address token) private view returns (uint256);
```

**Parameters**

| Name    | Type      | Description                    |
| ------- | --------- | ------------------------------ |
| `token` | `address` | Token address to get price for |

**Returns**

| Name     | Type      | Description                                 |
| -------- | --------- | ------------------------------------------- |
| `<none>` | `uint256` | Normalized price with 18 decimals precision |

### \_normalizeDecimals

Normalize decimals from one precision to another

```solidity
function _normalizeDecimals(uint256 value, uint8 fromDecimals, uint8 toDecimals) private pure returns (uint256);
```

**Parameters**

| Name           | Type      | Description               |
| -------------- | --------- | ------------------------- |
| `value`        | `uint256` | Value to normalize        |
| `fromDecimals` | `uint8`   | Current decimal precision |
| `toDecimals`   | `uint8`   | Target decimal precision  |

**Returns**

| Name     | Type      | Description      |
| -------- | --------- | ---------------- |
| `<none>` | `uint256` | Normalized value |

## Structs

### ChainlinkConfig

Chainlink-specific oracle configuration per token

```solidity
struct ChainlinkConfig {
    /// @dev Chainlink AggregatorV3 price feed contract
    AggregatorV3Interface feed;
    /// @dev Maximum acceptable age of price data in seconds; prices older than this revert with {StalePrice}
    uint256 heartbeat;
    /// @dev Decimal precision reported by the Chainlink feed (used for normalization to 18 decimals)
    uint8 feedDecimals;
}
```
