# Oracles

As of now, the developed oracle adapters are Chainlink and Dia oracle adapters. Both adapters include an ERC4626 base and quote vault converter. So if the market token is an ERC4626, you can easily convert this oracles underlying to the overlying ERC4626 price (exclusive of fees while minting and burning such ERC4626 token).

Another type of oracle exist allowing you to combine multiple Mangrove vaults oracle together. This can be used in case an oracle needs both chainlink and dia for example.

### Querying an oracle

Here is how to query an oracle. The price adjustement is based on the WETH/USDC market (12 decimals shift).

{% code title="oracle-query.ts" %}

```typescript
// Get the oracle price
import { type Address, parseAbi, createPublicClient } from "viem";
import { priceFromTick } from "@mangrovedao/mgv";

const client = createPublicClient(...);
const oracle: Address = "0x...";

const abi = parseAbi([
    "function tick() external view returns (int256)"
])

const tick = await client.readContract({
    address: oracle,
    abi,
    functionName: "tick"
})

const rawPrice = priceFromTick(tick); // 1.0001 ** Number(tick)
const adjustedPrice = rawPrice * 1e12 // adjust for decimals
```

{% endcode %}

{% hint style="info" %}
Further demonstration will not include javascript snippets, but the flow would be more or less the one recommended by the [viem](https://viem.sh) documentation.
{% endhint %}

{% hint style="info" %}
Addresses of factories can be found in [Deployment adresses](/quick-links/deployment-adresses.md).
{% endhint %}

### Deploying a chainlink oracle adapter (v2)

{% hint style="info" %}
Find the addresses for the chainlink price feeds [here](https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum\&page=1).
{% endhint %}

#### Choosing the parameters

The parameters to provide are the feed addresses (2 base feed, and 2 quote feed at most allowing feed chaining). One base and one quote ERC4626 vaults can also be provided as well as an optional salt.

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/chainlink/v2/MangroveChainlinkOracleV2.sol#L40-L46>" %}

In order to chain feeds together, we need to specify the token decimals for each feed token. If the token does not exist, assuming 18 decimals is recommended.

For reference, ETH/USD feed, ETH is 18 decmals, and USD does not exist so we take 18 decimals *(unless we assume USD to be USDC or USDT in which cas we choose 6)*. For USDC/USD, we will take 6 for USDC, and 18 for USD. Here is an example of a [valid deployment on base](https://basescan.org/tx/0x397c521c8b78b0a4eb591b159072ec86852229d86db04dd17d582e310ff2b857) for WETH/USDC market.

#### Checking if the oracle is already deployed

These oracles are deployed with CREATE2 allowing to share an oracle easily by finding vaults with same parameters. All values are immutable ensuring no changes have been done since deployment.

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/chainlink/v2/MangroveChainlinkOracleFactoryV2.sol#L18-L37>" %}

Then to check if it has been deployed, we just need to check the code size.

#### Deploying the oracle

To deploy the oracle we pass the very same parameters to the factory:

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/chainlink/v2/MangroveChainlinkOracleFactoryV2.sol#L57-L65>" %}

### Deploying a Dia oracle adapter

{% hint style="info" %}
Dia oracles are only limited to DiaOracleV2
{% endhint %}

Up to 4 feeds (2 base and 2 quote) in addition to one base and one quote vault can be combined.

#### Choosing the parameters

In order to choose the parameter, do the same as [#choosing-the-parameters](#choosing-the-parameters "mention"). But you also have to provide a string key. The string key then has to be packed encoded. The maximum size of the encoding is now limited to 32 bytes for efficiency.

```typescript
// Getting the key
import {encodePacked} from "viem";

const key = "ETH/USD";
const encodedKey = encodePacked(["string"], key);
// the byte length of this encoded key must be 32 bytes or less.
```

#### Checking if the oracle is already deployed

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/dia/MangroveDiaOracleFactory.sol#L29-L37>" %}

Check the returned address code size to check if it is already deployed.

#### Deploying the oracle

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/dia/MangroveDiaOracleFactory.sol#L57-L65>" %}

### Combining oracles together

An oracle combiner can combine up to 4 Mangrove compatible oracles together.

#### Checking existing oracle

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/OracleCombinerFactory.sol#L29-L32>" %}

#### Deploying the oracle

{% @github-files/github-code-block url="<https://github.com/mangrovedao/mangrove-vault/blob/899abdb187f1801ba44621c0e25a697edda859e7/src/oracles/OracleCombinerFactory.sol#L50-L53>" %}


---

# 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.mangrove.exchange/dev/vaults/custom-interactions/oracles.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.
