# Crypto.com OTC 2.0 API Integration

Complete integration with the [Crypto.com Exchange OTC 2.0 REST/WebSocket API](https://exchange-docs.crypto.com/exchange/v1/rest-ws/index_OTC2.html) for institutional OTC trading.

## Features

- **REST API**: Reference data, quote queries, deal queries, settle-later
- **WebSocket**: Real-time RFQ (Request for Quote), deal execution, subscriptions
- **Rate Limiting**: Token bucket for REST (1 req/s) and WebSocket (2 req/s)
- **Retry & Backoff**: Exponential backoff for transient failures and reconnection
- **FX Integration**: OTC prices feed into `FxService.getMarketPrice()`
- **Trade Persistence**: OTC deals persisted to `otc_trades` table
- **Settle Later Tracking**: Monitoring for T1 settlement limits and alerts
- **Market Reporting Adapter**: `CryptoComOtcMarketReportingAdapter` for price aggregation

## Setup

### Environment Variables

```bash
# Required for OTC features
CRYPTO_COM_API_KEY=your_api_key
CRYPTO_COM_API_SECRET=your_api_secret

# Optional (default: production)
CRYPTO_COM_ENVIRONMENT=production  # or 'uat' for sandbox
```

### Database Migration

Run Prisma migration to create the `otc_trades` table:

```bash
cd dbis_core
npx prisma migrate dev --name add_otc_trades
```

## API Routes

All routes are mounted at `/api/v1/crypto-com-otc` (requires auth).

| Method | Path | Description |
|--------|------|-------------|
| GET | /instruments | Available OTC instruments |
| GET | /quote-requests | Open quote requests |
| GET | /quote-requests/history | Quote request history |
| GET | /quotes | Open quotes |
| GET | /quotes/history | Quote history |
| GET | /deals | Open deals |
| GET | /deals/history | Deal history |
| GET | /settle-later/limit | Settle later limit |
| GET | /settle-later/unsettled | Unsettled amounts |
| GET | /settle-later/status | Full monitoring status |
| POST | /rfq/request-quote | Request a quote |
| POST | /rfq/request-deal | Execute a deal |
| GET | /status | Service status |

## Usage

```typescript
import { createCryptoComOtcService } from '@/core/exchange/crypto-com-otc';

const otcService = createCryptoComOtcService({
  apiKey: process.env.CRYPTO_COM_API_KEY!,
  apiSecret: process.env.CRYPTO_COM_API_SECRET!,
  environment: 'production',
});

await otcService.initialize();

otcService.on('newQuote', (quote) => {
  console.log('Quote received:', quote.quote_id);
});

await otcService.requestQuote({
  cl_quote_req_id: `quote-${Date.now()}`,
  leg_list: [{ instrument_name: 'BTC_USD', side: 'BUY', quantity: '1' }],
});
```

## FX Integration

When OTC is configured, `FxService.getMarketPrice()` automatically uses cached OTC prices when available. Prices are populated when quotes are received via WebSocket.

## Market Reporting

Use `CryptoComOtcMarketReportingAdapter` to get OTC prices for market reporting:

```typescript
import { CryptoComOtcMarketReportingAdapter, createCryptoComOtcService } from '@/core/exchange/crypto-com-otc';

const otcService = createCryptoComOtcService({ ... });
await otcService.initialize();

const adapter = new CryptoComOtcMarketReportingAdapter({ otcService });
const reports = await adapter.getCryptoPriceReports();
```

## Rate Limits

- **REST**: 1 request/second per endpoint
- **WebSocket**: 2 requests/second for request-quote, request-deal
- **Connection**: 1-second delay after WebSocket connect (per Crypto.com docs)
