# GET Open Interest

### /open-interest

```json
/open-interest
```

**Example Query:**

```
https://base-hedger82.rasa.capital/open-interest
```

**Example Response:**

```json
{
  "total_cap": "2438560.667955634347925220",
  "used": "111969.991183710650000000"
}
```

#### **Data Source**

All values are configured and managed exclusively by the hedger. This data reflects the hedger’s internal risk management settings and is **not** derived from on-chain contracts or user positions.

#### **Response Fields**

* **`total_cap`**: The maximum total open interest across all assets (in collateral, e.g., USDT).&#x20;
* **`used`**: The portion of the total capacity currently utilized by open positions across all assets.

## **Open Interest Implementation (Rasa)**

***

### Router

```python
@common_router.get(
    '/open-interest',
    responses={status.HTTP_200_OK: {"model": OpenInterestResponseSchema}},
    response_model=OpenInterestResponseSchema,
    dependencies=[
        Depends(CustomRateLimiter(times=1, seconds=1)),
        Depends(CustomRateLimiter(times=40, minutes=1)),
        Depends(CustomRateLimiter(times=1500, hours=1)),
    ]
)
@with_context_db_session
async def get_open_interest():
    # 1. Compute hedger’s capacity and usage
    total_cap, used = await OpenCapEngine.get_total_cap_and_used_amount_async(from_stats=True)

    # 2. Emit metrics for monitoring
    apm.label(total_cap=total_cap, used=used)

    # 3. Return JSON response
    return {
        "total_cap": total_cap,
        "used": used
    }
```

* **Path**: `GET /open-interest`
* **Rate‐limits**: 1 req/sec, 40 req/min, 1 500 req/hr
* **DB session**: injected via `@with_context_db_session` (in case the capacity engine reads from SQL)

***

### Logic

1. **Call Capacity Engine**

   ```python
   total_cap, used = await OpenCapEngine.get_total_cap_and_used_amount_async(from_stats=True)
   ```

   * Invokes your hedger’s internal risk‐management component to compute:
     * **total\_cap**: how much notional collateral the hedger is willing to allow in aggregate.
     * **used**: how much of that allowance is currently locked up by open positions.
2. **Instrumentation**

   ```python
   apm.label(total_cap=total_cap, used=used)
   ```

   * Sends metrics (via your APM/tracing tool) for observability.
3. **Return Payload**

   ```python
   return {"total_cap": total_cap, "used": used}
   ```

   * FastAPI serializes this to JSON matching `OpenInterestResponseSchema`.

   ```python
   class OpenInterestResponseSchema(BaseModel):
       total_cap: Decimal
       used: Decimal
   ```

***

#### Steps to Implement Your Own Version

1. **Define the Response Schema**

   ```python
   class OpenInterestResponseSchema(BaseModel):
       total_cap: Decimal
       used: Decimal
   ```
2. **Build or Wire Up Your Capacity Engine**
   * Implement `OpenCapEngine.get_total_cap_and_used_amount_async(from_stats: bool) → Tuple[Decimal, Decimal]`.
   * Internally, read from your own data sources:
     * **Configuration table** (max capacities per asset or global).
     * **Positions table** (sum up open positions’ collateral needs).
   * Return `(total_cap, used)`.
3. **Add the FastAPI Route**
   * In your `common_router`, add the `@common_router.get("/open-interest")` handler shown above.
   * Import `OpenInterestResponseSchema` and `OpenCapEngine`.
   * Include your rate‐limiting dependencies and `@with_context_db_session` if your engine needs DB access.
4. **Instrumentation (Optional)**
   * If you have an APM or metrics library, record these values on each call.

***
