# Price Oracles

## ChainLink

When making a request to the ChainLink oracle to fetch the price of a token pair, it returns a price that is scaled to a predefined number of decimals within the oracle contract. Because each token has their own number of decimals, the resulting price sometimes needs to be converted to be represented in the token that is used as collateral. The math for doing so will be outline in the next section.

### Price Conversion

Prices in Teller are represented as 1 WHOLE lending token in collateral UNITS. Lets use an example to help clarify whats happening. Say we want the price for **LINK** (lending token) in **USDC** (collateral token).

Let:

* **x** be the actual price in decimals of 1 **LINK** in **USDC**
  * where $$x = 4.14$$&#x20;
* **y** be the price returned from the oracle
  * where $$y = x \* 10 ^ {R}$$&#x20;
* **R** be the number of decimals within the oracle
* **C** be the number of decimals for the collateral token

We want to take the formula for **y** and make it $$y = x \* 10^{C}$$. To do this:

* If `C > R` the calculation is $$y \* 10^{C-R}$$&#x20;
* if `R > C` the calculation is $$y / 10^{R-C}$$&#x20;

This formula is defined within the `ChainlinkPairAggregator._normalizeResponse()` function [here](https://github.com/teller-protocol/teller-protocol-v1/blob/04fb9313fff535fd4ab00687c4a93a9154adf72d/contracts/providers/chainlink/ChainlinkPairAggregator.sol#L111).

### Inverse Price Conversion

ChainLink does not have some price oracles, which means sometimes we have to get the price of 1 WHOLE collateral token in lending UNITS and then invert the price as prices should still be represented as 1 WHOLE lending token in collateral UNITS.

Because the oracle returns $$y = x \* 10 ^ {R}$$ this means $$x = y / 10 ^ {R}$$.

This means 1 collateral token = $$y / 10 ^ {R}$$ lending tokens.

Therefore 1 lending token = $$10 ^ {R} /y$$ collateral tokens.

Therefore 1 lending token = $$(10 ^ {R} + 10 ^ {C}) / y$$ collateral units.

This formula is defined within the `InverseChainlinkPairAggregator._inverseValue()` function [here](https://github.com/teller-protocol/teller-protocol-v1/blob/04fb9313fff535fd4ab00687c4a93a9154adf72d/contracts/providers/chainlink/InverseChainlinkPairAggregator.sol#L41).


---

# 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.teller.org/teller-1/contracts/price-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.
