> ## Documentation Index
> Fetch the complete documentation index at: https://goldrush.dev/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Real-Time DEX Analytics

> Stream decoded Solana DEX trade data into ClickHouse to build real-time analytics dashboards for volume, pricing, and trader activity.

## Use Case

You want to build a real-time DEX analytics dashboard by streaming trade data across multiple Solana DEX protocols (Raydium, Orca, Meteora, PumpFun, and others) into ClickHouse. The `dex_trades` entity aggregates trades from all supported protocols into a single, unified schema.

## Pipeline Configuration

<Steps>
  <Step title="Create a new pipeline">
    In the [GoldRush Platform](https://goldrush.dev/platform/), navigate to **Manage Pipelines** and click **Create Pipeline**. Name it `dex-analytics`.
  </Step>

  <Step title="Configure the ClickHouse destination">
    Select **ClickHouse** as the destination type:

    ```yaml theme={null}
    destination:
      type: "clickhouse"
      url: "clickhouse://your-host:8123/dex_data"
      user: "${CH_USER}"
      password: "${CH_PASSWORD}"
      batch_size: 10000
      flush_interval_ms: 3000
    ```
  </Step>

  <Step title="Select your source">
    Choose **Solana** as the chain and **Swaps** as the data type. This streams every decoded DEX trade across all supported Solana protocols.
  </Step>

  <Step title="Add a SQL transform (optional)">
    Filter out dust trades and select the most useful columns:

    ```yaml theme={null}
    transforms:
      swaps: >
        SELECT block_slot, block_date, tx_id, pool_address, base_mint, quote_mint,
               base_amount, quote_amount, price_usd, volume_usd, protocol_name,
               trader, txn_fee_lamports
        FROM swaps
        WHERE volume_usd > 10
    ```
  </Step>

  <Step title="Deploy">
    Review and deploy. DEX trades begin streaming to ClickHouse.
  </Step>
</Steps>

## Verify Data

```sql theme={null}
SELECT protocol_name, count(*) AS trades, sum(volume_usd) AS total_volume
FROM dex_analytics.sol_dex_trades
WHERE block_date = today()
GROUP BY protocol_name
ORDER BY total_volume DESC;
```

## Sample Dashboard Queries

**Volume by protocol over the last 24 hours:**

```sql theme={null}
SELECT protocol_name,
       count(*) AS trade_count,
       sum(volume_usd) AS volume_usd,
       avg(volume_usd) AS avg_trade_size
FROM dex_analytics.sol_dex_trades
WHERE block_date >= toString(today() - 1)
GROUP BY protocol_name
ORDER BY volume_usd DESC;
```

**Top pools by volume:**

```sql theme={null}
SELECT pool_address, base_mint, quote_mint, protocol_name,
       count(*) AS trades,
       sum(volume_usd) AS volume_usd
FROM dex_analytics.sol_dex_trades
WHERE block_date = today()
GROUP BY pool_address, base_mint, quote_mint, protocol_name
ORDER BY volume_usd DESC
LIMIT 20;
```

**OHLCV aggregation for a token (5-minute candles):**

```sql theme={null}
SELECT toStartOfFiveMinutes(parseDateTimeBestEffort(block_date)) AS candle_time,
       min(price_usd) AS low,
       max(price_usd) AS high,
       argMin(price_usd, block_slot) AS open,
       argMax(price_usd, block_slot) AS close,
       sum(volume_usd) AS volume
FROM dex_analytics.sol_dex_trades
WHERE base_mint = 'your-token-mint'
  AND block_date >= toString(today())
GROUP BY candle_time
ORDER BY candle_time;
```

**Most active traders:**

```sql theme={null}
SELECT trader,
       count(*) AS trade_count,
       sum(volume_usd) AS total_volume,
       countDistinct(base_mint) AS unique_tokens
FROM dex_analytics.sol_dex_trades
WHERE block_date = today()
GROUP BY trader
ORDER BY total_volume DESC
LIMIT 20;
```

## Production Tips

* **Flush interval**: A 3-second flush interval keeps dashboard data fresh. Increase to 5-10 seconds if write throughput becomes a bottleneck.
* **volume\_usd > 10 filter**: Removes dust trades that add noise to analytics. Adjust the threshold based on your use case.
* **Protocol coverage**: The `sol_dex_trades` entity aggregates trades from all supported Solana protocols. The `protocol_name` column identifies the source (Raydium, Orca, Meteora, etc.).
* **Materialized views**: For high-frequency dashboard queries, consider ClickHouse materialized views to pre-aggregate data by time bucket and protocol.
