# Estimate Clipper Prices

In order to assist in integrating Clipper with DEX Aggregators & Resolvers, offchain state information that is used to calculate Clipper prices is made available in a read-only endpoint that can be queried frequently. This state information can then be used to accurately estimate Clipper price quotes when combined with onchain state information (pulled and maintained by the aggregators themselves).

{% hint style="success" %}
You can view code to see how to implement it, in our section [Complete Swap Flow](https://docs.clipper.exchange/disclaimers-and-technical/integrating-with-clipper-rfq/guides/integration-examples/complete-swap-flow)
{% endhint %}

## 1. Get pool data

Make a GET call to <mark style="color:orange;">`https://blade-api.sushi.com/rfq/v2/pool/{CHAIN_ID}?fieldset=offchain-data`</mark>

This endpoint returns a JSON blob that holds Clipper's offchain state information. Note that this response is modified from the standard response to the endpoint by removing *any* calls to the onchain contracts for response speed.

The API response will look like the following:&#x20;

```json
{
  "pool_type": "offchain",
  "pools": [
    {
      "pool": {
        "chain_id": 1,
        "address": "0x655eDCE464CC797526600a462A8154650EEe4B77",
        "num_assets": 4,
        "k": 0.06,
        "time_in_seconds": 60,
        "default_time_in_seconds": 60,
        "swaps_enabled": true
      },
      "assets": [
        {
          "name": "ETH",
          "address": "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
          "price_in_usd": 2076.799,
          "listing_weight": 79
        },
        {
          "name": "USDC",
          "address": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
          "price_in_usd": 1,
          "listing_weight": 188
        },
        {
          "name": "USDT",
          "address": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
          "price_in_usd": 1,
          "listing_weight": 305
        },
        {
          "name": "DAI",
          "address": "0x6B175474E89094C44Da98b954EedeAC495271d0F",
          "price_in_usd": 1,
          "listing_weight": 250
        }
      ],
      "pairs": [
        {
          "assets": [
            "ETH",
            "USDC"
          ],
          "fee_in_basis_points": 9
        },
        {
          "assets": [
            "ETH",
            "USDT"
          ],
          "fee_in_basis_points": 9
        },
        {
          "assets": [
            "ETH",
            "DAI"
          ],
          "fee_in_basis_points": 8
        },
        {
          "assets": [
            "USDC",
            "USDT"
          ],
          "fee_in_basis_points": 5
        },
        {
          "assets": [
            "USDC",
            "DAI"
          ],
          "fee_in_basis_points": 5
        },
        {
          "assets": [
            "USDT",
            "DAI"
          ],
          "fee_in_basis_points": 5
        }
      ]
    }
  ]
}
```

> 💡 To obtain more details about the endpoint, its parameters, and the response, please refer to the API Reference in the [pool](https://docs.clipper.exchange/disclaimers-and-technical/integrating-with-clipper-rfq/api-reference/api-v2/pool-v2) section.

## 2. Estimate Clipper Prices using On- and Off- chain Data

1. Convert (using <mark style="color:orange;">`decimals`</mark>) the onchain balances of the Clipper Pool - the quantity vector <mark style="color:orange;">`q`</mark> - into human-readable terms (i.e., a balance of 100 ETH instead of 1e20 ETH.
2. Calculate the fee-adjustment multiplier for the swapping pair <mark style="color:orange;">`M = (10000-fee_in_basis_points)/10000`</mark> (10,000 = number of basis points in 100%).
3. Solve for a swap from asset X to asset Y through root-finding or a closed form solution. Let <mark style="color:orange;">`pX`</mark>, <mark style="color:orange;">`qX`</mark>, <mark style="color:orange;">`wX`</mark>, and <mark style="color:orange;">`inX`</mark> represent the current price, quantity, listing weight, and swap input of asset X, and similarly <mark style="color:orange;">`pY`</mark>, <mark style="color:orange;">`qY`</mark>, <mark style="color:orange;">`wY`</mark>, and <mark style="color:orange;">`outY`</mark> represent the current price, quantity, listing weight, and swap output of asset Y. Exactly one of <mark style="color:orange;">`inX`</mark> or <mark style="color:orange;">`outY`</mark> should be unknown. That unknown quantity is then found by solving an indifference pricing equation relating current utility from the assets X and Y to the utility from those assets after the swap:

$$
\frac{(pX \cdot {qX})^{1-k}}{{wX}^k} + \frac{(pY \cdot {qY})^{1-k}}{{wY}^k} \ = \ \frac{(pX \cdot ({qX + M\cdot{inX}}))^{1-k}}{{wX}^k} + \frac{(pY \cdot ({qY}-{outY}))^{1-k}}{{wY}^k}
$$

Checks should be done to ensure an intermediate solution to this equation before attempting to solve:

* If <mark style="color:orange;">`outY`</mark> is specified:
  * <mark style="color:orange;">`outY`</mark> should be no larger than <mark style="color:orange;">`qY`</mark>.
  * The LHS of the formula (current utility) should be larger than the RHS (utility after swap) evaluated at <mark style="color:orange;">`inX=0`</mark>
  * The LHS of the formula should be smaller than the RHS when evaluated at very large <mark style="color:orange;">`inX`</mark>
* If <mark style="color:orange;">`inX`</mark> is specified:
  * The LHS of the formula (current utility) should not be smaller than the RHS (utility after swap) evaluated at `outY=qY`
  * The LHS of the formula should be smaller than the RHS when evaluated at <mark style="color:orange;">`outY=0`</mark>

### Closed Formed Solution

{% hint style="info" %}
In practice, this is the preferred way to solve the equation because it tends to be faster and more consistent. It is more numerically unstable in the worst case since it requires 1/(1-k) exponentiation, but in practice Clipper’s k tends to be small, making 1/(1-k) ≈ 1
{% endhint %}

* If <mark style="color:orange;">`inX`</mark> is specified, then:

$$
{outY} = {qY}-\frac{\left\[\left(\frac{(pX \cdot {qX})^{1-k}}{{wX}^k} + \frac{(pY \cdot {qY})^{1-k}}{{wY}^k} - \frac{(pX \cdot ({qX + M\cdot{inX}}))^{1-k}}{{wX}^k} \right)\cdot wY^k\right]^{\frac{1}{1-k}}}{pY}
$$

* If <mark style="color:orange;">`outY`</mark> is specified, then:

$$
{inX} = \frac{1}{M}\cdot\left\[\frac{\left\[\left(\frac{(pX \cdot {qX})^{1-k}}{{wX}^k} + \frac{(pY \cdot {qY})^{1-k}}{{wY}^k} - \frac{(pY \cdot ({qY}-{outY}))^{1-k}}{{wY}^k}\right)\cdot {wX}^k\right]^{\frac{1}{1-k}}}{pX}-{qX}\right]
$$

### Root Finding

A good initial guess (for a Newton method solver) for <mark style="color:orange;">`inX`</mark> or <mark style="color:orange;">`outY`</mark> is the FMV of the other value.

* If <mark style="color:orange;">`inX`</mark> is specified, then try:

$$
outY \approx M \cdot inX \cdot pX/pY
$$

* If <mark style="color:orange;">`outY`</mark> is specified, try:&#x20;

$$
inX \approx (outY \cdot pY) / (M\*pX)
$$

In practice, the actual Clipper price quote will also depend on the amount of time the quote is alive for, as well as the most recent price update. These should result in only small changes from the estimated quotes produced by this process.

### Final Check: Output Value No Larger than Input Value

Finally, we enforce that **the FMV (according to&#x20;*****our*** <mark style="color:orange;">**`price_in_usd`**</mark>**&#x20;values) of the contributed input is at least the FMV of the output.** This constraint will typically only bind when the Clipper pool is very far off target allocations, since in general fees plus slippage will be sufficient to ensure the condition.

If <mark style="color:orange;">`inX`</mark> was specified and <mark style="color:orange;">`outY`</mark> has been calculated to solve the formula, then as a final check:

$$
outY \equiv \min\left\[outY, (inX \cdot pX) / {pY} \right]
$$

If <mark style="color:orange;">`outY`</mark> is specified and <mark style="color:orange;">`inX`</mark> has been calculated to solve the formula, then as a final check:

$$
inX \equiv \max\left\[inX, (outY \cdot pY) / {pX} \right]
$$

{% hint style="warning" %}
Note that these tests are done *without regard to fees*.
{% endhint %}
