# Oracle

Provides price and liquidity data useful for a wide variety of system designs

Instances of stored oracle data, "observations", are collected in the oracle array Every pool is initialized with an oracle array length of 1. Anyone can pay the SSTOREs to increase the maximum length of the oracle array. New slots will be added when the array is fully populated. Observations are overwritten when the full length of the oracle array is populated. The most recent observation is available, independent of the length of the oracle array, by passing 0 to observe()

### Functions[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#functions) <a href="#functions" id="functions"></a>

#### initialize[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#initialize) <a href="#initialize" id="initialize"></a>

```solidity
  function initialize(
    struct Oracle.Observation[65535] self,
    uint32 time
  ) internal returns (uint16 cardinality, uint16 cardinalityNext)
```

Initialize the oracle array by writing the first slot. Called once for the lifecycle of the observations array

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#parameters)

| Name   | Type                              | Description                                                                    |
| ------ | --------------------------------- | ------------------------------------------------------------------------------ |
| `self` | struct Oracle.Observation\[65535] | The stored oracle array                                                        |
| `time` | uint32                            | The time of the oracle initialization, via block.timestamp truncated to uint32 |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#return-values)

| Name              | Type   | Description                                                   |
| ----------------- | ------ | ------------------------------------------------------------- |
| `cardinality`     | uint16 | The number of populated elements in the oracle array          |
| `cardinalityNext` | uint16 | The new length of the oracle array, independent of population |

#### write[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#write) <a href="#write" id="write"></a>

```solidity
  function write(
    struct Oracle.Observation[65535] self,
    uint16 index,
    uint32 blockTimestamp,
    int24 tick,
    uint128 liquidity,
    uint16 cardinality,
    uint16 cardinalityNext
  ) internal returns (uint16 indexUpdated, uint16 cardinalityUpdated)
```

Writes an oracle observation to the array

Writable at most once per block. Index represents the most recently written element. cardinality and index must be tracked externally. If the index is at the end of the allowable array length (according to cardinality), and the next cardinality is greater than the current one, cardinality may be increased. This restriction is created to preserve ordering.

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#parameters-1)

| Name              | Type                              | Description                                                     |
| ----------------- | --------------------------------- | --------------------------------------------------------------- |
| `self`            | struct Oracle.Observation\[65535] | The stored oracle array                                         |
| `index`           | uint16                            | The location of the most recently updated observation           |
| `blockTimestamp`  | uint32                            | The timestamp of the new observation                            |
| `tick`            | int24                             | The active tick at the time of the new observation              |
| `liquidity`       | uint128                           | The total in-range liquidity at the time of the new observation |
| `cardinality`     | uint16                            | The number of populated elements in the oracle array            |
| `cardinalityNext` | uint16                            | The new length of the oracle array, independent of population   |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#return-values-1)

| Name                 | Type   | Description                                                            |
| -------------------- | ------ | ---------------------------------------------------------------------- |
| `indexUpdated`       | uint16 | The new index of the most recently written element in the oracle array |
| `cardinalityUpdated` | uint16 | The new cardinality of the oracle array                                |

#### grow[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#grow) <a href="#grow" id="grow"></a>

```solidity
  function grow(
    struct Oracle.Observation[65535] self,
    uint16 current,
    uint16 next
  ) internal returns (uint16)
```

Prepares the oracle array to store up to `next` observations

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#parameters-2)

| Name      | Type                              | Description                                                               |
| --------- | --------------------------------- | ------------------------------------------------------------------------- |
| `self`    | struct Oracle.Observation\[65535] | The stored oracle array                                                   |
| `current` | uint16                            | The current next cardinality of the oracle array                          |
| `next`    | uint16                            | The proposed next cardinality which will be populated in the oracle array |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#return-values-2)

| Name   | Type   | Description                                                      |
| ------ | ------ | ---------------------------------------------------------------- |
| `next` | uint16 | The next cardinality which will be populated in the oracle array |

#### observe[​](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#observe) <a href="#observe" id="observe"></a>

```solidity
  function observe(
    struct Oracle.Observation[65535] self,
    uint32 time,
    uint32[] secondsAgos,
    int24 tick,
    uint16 index,
    uint128 liquidity,
    uint16 cardinality
  ) internal view returns (int56[] tickCumulatives, uint160[] liquidityCumulatives)
```

Returns the accumulator values as of each time seconds ago from the given time in the array of `secondsAgos`

Reverts if `secondsAgos` > oldest observation

**Parameters:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#parameters-3)

| Name          | Type                              | Description                                                                           |
| ------------- | --------------------------------- | ------------------------------------------------------------------------------------- |
| `self`        | struct Oracle.Observation\[65535] | The stored oracle array                                                               |
| `time`        | uint32                            | The current block.timestamp                                                           |
| `secondsAgos` | uint32\[]                         | Each amount of time to look back, in seconds, at which point to return an observation |
| `tick`        | int24                             | The current tick                                                                      |
| `index`       | uint16                            | The location of a given observation within the oracle array                           |
| `liquidity`   | uint128                           | The current in-range pool liquidity                                                   |
| `cardinality` | uint16                            | The number of populated elements in the oracle array                                  |

**Return Values:**[**​**](https://docs.uniswap.org/contracts/v3/reference/core/libraries/Oracle#return-values-3)

| Name                   | Type       | Description                                                                                 |
| ---------------------- | ---------- | ------------------------------------------------------------------------------------------- |
| `tickCumulatives`      | int56\[]   | The tick \* time elapsed since the pool was first initialized, as of each `secondsAgo`      |
| `liquidityCumulatives` | uint160\[] | The liquidity \* time elapsed since the pool was first initialized, as of each `secondsAgo` |


---

# 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://docs.dragonswap.app/dragonswap/resources/developer-resources/smart-contracts/dragonswapv2/core/libraries/oracle.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.
