> ## Documentation Index
> Fetch the complete documentation index at: https://sonar-cf3aeac7.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# How data flows

> How on-chain swaps become USD prices, rolling stats, candles, REST responses and stream events.

This page explains, at a high level, where ChainQuery's numbers come from and what that means for freshness and coverage.

**1. Decode**

New blocks on each supported chain are decoded into pool creations, token launches and swaps for every [supported protocol](https://sonar-cf3aeac7.mintlify.site/concepts/protocols-and-launchpads.md). Pool state (reserves, price) is updated with each event.

**2. Classify and price**

Each swap is classified (a regular trade, a liquidity change, MEV or a bonding event) and priced in USD using the token prices at that moment.

**3. Aggregate**

Token prices, liquidity, market cap and rolling window stats (volume, trades, buyers, sellers, price change) are computed from the priced swaps.

**4. Serve**

The REST API reads the indexed data. The WebSocket surfaces publish each block's trades, in order, as they are processed.

## Token prices

A token's USD price is a weighted blend of its pools:

* Up to **50 pools** containing the token are considered, starting with the highest 24h volume.
* Each pool's price is converted to USD through the other token's USD price.
* Pools are weighted by 24h volume (pools with at least **\$1,000** of volume). If no pool has that much volume, they are weighted by liquidity instead (the pool's weaker side, in USD).
* Pools with a very small share of the token's volume are skipped. Pools with less than **\$1,000** of liquidity are ignored; if no pool reaches that, only the single deepest pool is used.
* Outlier prices are dropped before blending.
* Recognised stablecoins are pinned at **\$1**.

Per-trade prices (for example `tokenPrice` on a trade) are the prices at the moment of that trade. Candle prices only count trades of at least \$0.10, so dust trades do not distort charts.

## Rolling stats

Token and pool stats such as `volume24hUSD`, `trades1h`, `buyers5min` and `priceChange24hPercentage` are recomputed continuously, typically within about a minute of new trades. Expect REST stats to trail the live trade stream by up to that much.

[Pulse](https://sonar-cf3aeac7.mintlify.site/pulse/overview.md) keeps its own live windows for the short horizons (1 minute to 1 hour), so its live answers can move faster than REST stats, and numbers can differ slightly between the two.

## What the REST API reads

* The REST API only reads indexed data. It **never queries the chain on request**, with one exception: the native coin balance in [`GET /wallet/holdings`](https://sonar-cf3aeac7.mintlify.site/api-reference/wallet/get-wallet-holdings) is read live from a node (and cached for about 5 seconds).
* A token or pool must already be indexed. Requests for a token ChainQuery has not seen return `404 TOKEN_NOT_FOUND` (or `POOL_NOT_FOUND`); the API does not index it on demand.
* Token metadata (name, symbol, supply, logo, socials) is stored when a token is first seen and refreshed in the background, so supply-based values such as market cap can update on later calls.

## Which swaps count as trades

Only swaps classified as **trades** appear in [`GET /trades`](https://sonar-cf3aeac7.mintlify.site/api-reference/trades/get-trades), the [WebSocket channels](https://sonar-cf3aeac7.mintlify.site/websocket/overview.md), the [multi-events stream](https://sonar-cf3aeac7.mintlify.site/streams/overview.md) and candles. These are excluded:

* adding or removing liquidity,
* MEV activity, including sandwich attacks and the swaps they sandwich,
* bonding events (a launchpad token graduating to a DEX pool).

## Stream ordering and history

The WebSocket surfaces receive trades per chain, one block at a time:

* **Ordered.** Blocks are released strictly in order, and items inside a block keep their on-chain order. Every event has a cursor that encodes this position, except on sampled subscriptions (`maxUpdatesPerMinute`), which drop items and cannot resume.
* **Resumable.** About **10 minutes** of recent blocks are kept. Reconnect with your last cursor within that window and you receive exactly the events you missed, with no duplicates. Older cursors are refused with `RESUME_EXPIRED`. See [Cursors and resume](https://sonar-cf3aeac7.mintlify.site/websocket/cursors-and-resume.md).
* **Gaps.** If a block has not been processed after about **15 seconds**, the stream skips past it and sends a `gap` message with the affected block range. Trades in those blocks are not streamed; fetch them from REST if you need them.

## Freshness at a glance

| Data | Typical delay |
| - | - |
| WebSocket trades and candles | As each block is processed |
| Pulse live views | Near real time; full resync about every 30 seconds |
| REST trades and candles | As soon as swaps are indexed |
| REST rolling stats | Typically within about a minute |
| `GET /protocols` | Up to about 5 minutes |

Use [`GET /health/last-swaps`](https://sonar-cf3aeac7.mintlify.site/api-reference/health/get-last-swaps) (at the host root, not under `/api/v1`) to check how recent the newest indexed swap is on each chain.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.