> ## 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.

# Filters

> Filter grammar for the multi-events stream: operators, fields, buy and sell, limits, validation errors and a cookbook.

A filter decides which trades a subscription receives. The server evaluates it before sending, and replayed trades go through the same filter. Pass it in `params.filters` when you [subscribe](https://sonar-cf3aeac7.mintlify.site/streams/subscribing.md#params).

```json theme={null}
"filters": {
  "and": [
    { "eq": ["tokenAddress", "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"] },
    { "eq": ["type", "buy"] },
    { "gte": ["tokenAmountUsd", 100000] }
  ]
}
```

## Grammar

A filter is a JSON object with **exactly one key**, the operator. An object with no key, an unknown key, or two keys is rejected with `Each condition has one operator: eq, in, gt, gte, lt, lte, and, or`.

| Operator | Form | Fields | Matches when |
| - | - | - | - |
| `eq` | `{ "eq": [field, "value"] }` | `poolType`, `poolAddress`, `tokenAddress`, `sender`, `type` | The field equals the value |
| `in` | `{ "in": [field, ["v1", "v2"]] }` | Same as `eq` | The field equals any listed value. The list holds 1 to 500 values. |
| `gt`, `gte`, `lt`, `lte` | `{ "gte": ["tokenAmountUsd", 100000] }` | `tokenAmountUsd` only | The trade's USD value is greater than, greater than or equal to, less than, or less than or equal to the number |
| `and` | `{ "and": [cond, cond] }` | Nested conditions | Every child matches. At least 1 child. |
| `or` | `{ "or": [cond, cond] }` | Nested conditions | Any child matches. At least 1 child. |

* `eq` and `in` values are JSON strings.
* Comparison values are JSON numbers. The string `"100000"` is rejected.
* `eq` and `in` don't work on `tokenAmountUsd`, and comparisons don't work on any other field. Use `gte` and `lte` together for an exact USD range.

## Fields

| Field | Accepted values | Checks payload field |
| - | - | - |
| `poolType` | String, 1 to 64 characters. **Case-sensitive**, not normalized. | `poolType` |
| `poolAddress` | `0x` + 40 hex (address) or `0x` + 64 hex (32-byte pool id, for example Uniswap v4). Any case; lowercased. | `pool` |
| `tokenAddress` | `0x` + 40 hex. Any case; lowercased. A 64-hex id is rejected. | `token0` **or** `token1` (either side of the trade) |
| `sender` | `0x` + 40 hex. Any case; lowercased. | `initiator`: the wallet that sent the transaction, not the router that called the pool |
| `type` | `"buy"` or `"sell"` | Derived; see [below](#type-buy-or-sell) |
| `tokenAmountUsd` | A number, comparisons only | `amountUSD` |

* Pool type values are the lowercase protocol slugs used across the API, such as `uniswap-v2`, `uniswap-v3`, `uniswap-v4`, `pancakeswap-v3` or `clanker-v4`. See [Protocols and launchpads](https://sonar-cf3aeac7.mintlify.site/concepts/protocols-and-launchpads.md). `"Uniswap-V3"` matches nothing.
* A trade without a `poolType` never matches a `poolType` condition.
* An invalid address is rejected with `Invalid <field>: must be a valid token or wallet address`, or for `poolAddress`, `Invalid poolAddress: must be a valid token address or pool id`.

### `type`: buy or sell

The payload has no side field, so the server works out a side for each trade:

1. **Reference token.** If the whole filter names exactly one distinct `tokenAddress` (through `eq` or `in`, anywhere in the tree, including inside `or`) and that token is in the trade, it is the reference. Otherwise the reference is the trade's `baseToken`.
2. **Side.** If the reference token's raw amount is positive (it went into the pool), the side is `sell`. Otherwise it is `buy`.

So `{ "and": [{ "eq": ["tokenAddress", WETH] }, { "eq": ["type", "buy"] }] }` matches trades where WETH was bought, whichever side of the pool WETH is on.

> [!WARNING]
>
> If the filter names two or more tokens, the side is taken from each trade's `baseToken`, even for trades of the tokens you named. `{ "in": ["tokenAddress", [A, B]] }` combined with `{ "eq": ["type", "buy"] }` means "the base token was bought". To filter buys of several specific tokens, use one subscription per token.

See [Deriving buy or sell](https://sonar-cf3aeac7.mintlify.site/streams/event-payloads.md#deriving-buy-or-sell) to compute the same side on the client.

## Limits

| Limit | Value | Error message |
| - | - | - |
| Nesting of `and` / `or` | 3 levels. `and` → `and` → `and` → condition is allowed; a 4th level is rejected. | `At most 3 levels of (and / or) nesting` |
| Conditions | 50 leaf conditions in the whole filter. An `in` with 500 values counts as one condition. | `At most 50 conditions` |
| Values per `in` list | 500 | Too-big list error at the list's path |
| Empty lists | `and: []`, `or: []` and `in` with `[]` are rejected | Too-small error at the list's path |

The whole subscribe message must also fit in 64 KiB. A full list of 500 addresses takes about 22 KB, so at most two full lists fit in one message. See [Limits and subscription cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md).

## One filter or one per event kind

`filters` takes two forms:

* **One filter.** A single condition applies to every kind in `events`.
* **A map.** An object whose keys are all event kinds (`swap`, `swap-enriched`) gives each kind its own filter. No event kind is named like an operator, so the two forms can't be confused.

```json theme={null}
"events": ["swap", "swap-enriched"],
"filters": {
  "swap": { "eq": ["sender", "0xd0b53d9277642d899df5c87a3966a349a798f224"] },
  "swap-enriched": { "gte": ["tokenAmountUsd", 100000] }
}
```

Map rules:

* Every kind in `events` needs an entry. A missing one fails with `filters.<kind>: every requested event needs a filter`.
* A key that isn't in `events` fails with `filters.<key>: not in events`.
* You can't leave one kind unfiltered inside a map. Use a second subscription instead.

Without `filters`, every trade on the requested chains is delivered.

## Cookbook

Each example shows the value of `params.filters` and its [cost per pair](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md). "Chain-wide" filters cost 10 per pair because the server checks them against every trade on the chain.

> [accordion] Uniswap v3 or v4 trades (chain-wide, 10)
>
> ```json theme={null}
> { "or": [ { "eq": ["poolType", "uniswap-v3"] }, { "eq": ["poolType", "uniswap-v4"] } ] }
> ```
>
> `{ "in": ["poolType", ["uniswap-v3", "uniswap-v4"]] }` is equivalent.

> [accordion] Trades in specific pools (1)
>
> ```json theme={null}
> { "in": ["poolAddress", ["0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640", "0x11b815efb8f581194ae79006d24e0d814b7697f6"]] }
> ```

> [accordion] Every trade of a token, in any pool (1)
>
> ```json theme={null}
> { "eq": ["tokenAddress", "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"] }
> ```
>
> Matches trades where the token is on either side, including WETH/USDC and memecoin/WETH pools.

> [accordion] Trades sent by a list of wallets (1 per started 500 wallets)
>
> ```json theme={null}
> { "in": ["sender", ["0xd0b53d9277642d899df5c87a3966a349a798f224", "0xa6b579684e943f7d00d616a48cf99b5147fc57a5"]] }
> ```

> [accordion] Those wallets, only on Uniswap (1)
>
> ```json theme={null}
> {
>   "and": [
>     { "in": ["poolType", ["uniswap-v2", "uniswap-v3", "uniswap-v4"]] },
>     { "in": ["sender", ["0xd0b53d9277642d899df5c87a3966a349a798f224"]] }
>   ]
> }
> ```

> [accordion] Trades between $1,000 and $100,000 (chain-wide, 10)
>
> ```json theme={null}
> { "and": [ { "gte": ["tokenAmountUsd", 1000] }, { "lt": ["tokenAmountUsd", 100000] } ] }
> ```

> [accordion] WETH buys of \$100,000 or more (1)
>
> ```json theme={null}
> {
>   "and": [
>     { "eq": ["tokenAddress", "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"] },
>     { "eq": ["type", "buy"] },
>     { "gte": ["tokenAmountUsd", 100000] }
>   ]
> }
> ```

> [accordion] Watched wallets or any trade of \$100,000 or more (chain-wide, 10)
>
> ```json theme={null}
> {
>   "or": [
>     { "in": ["sender", ["0xd0b53d9277642d899df5c87a3966a349a798f224"]] },
>     { "gte": ["tokenAmountUsd", 100000] }
>   ]
> }
> ```
>
> The `gte` branch pins nothing, so the whole `or` is chain-wide. See [Limits and subscription cost](https://sonar-cf3aeac7.mintlify.site/streams/limits-and-cost.md#how-a-filter-is-priced).

> [accordion] Sells of a token on one pool type (1)
>
> ```json theme={null}
> {
>   "and": [
>     { "eq": ["tokenAddress", "0x6982508145454ce325ddbe47a25d4ec3d2311933"] },
>     { "eq": ["poolType", "uniswap-v2"] },
>     { "eq": ["type", "sell"] }
>   ]
> }
> ```

## Validation errors

An invalid filter fails the subscription with code `INVALID_PARAMETERS`. Each problem is listed in `details` with a dotted `field` path:

* The path starts with `filters`, then the event kind when you use the map form, then the operators and array indexes down to the bad value.
* `filters.and.0.gte.0` is the field name (index 0) of the `gte` condition that is the first child of `and`.
* `filters.swap-enriched.and.0.gte.0` is the same position in the `swap-enriched` filter of a map.

Filter errors always use `INVALID_PARAMETERS`, including bad addresses.

```jsonc Comparison on the wrong field theme={null}
// → "filters": { "and": [ { "gte": ["sender", 1] } ] }
{
  "type": "error",
  "id": "bad",
  "code": "INVALID_PARAMETERS",
  "message": "filters.and.0.gte.0: Invalid input: expected \"tokenAmountUsd\"",
  "details": [{ "field": "filters.and.0.gte.0", "message": "Invalid input: expected \"tokenAmountUsd\"" }]
}
```

```jsonc Bad address in a list theme={null}
// → "filters": { "in": ["sender", ["0xd0b53d9277642d899df5c87a3966a349a798f224", "0x12"]] }
{
  "type": "error",
  "id": "wallets",
  "code": "INVALID_PARAMETERS",
  "message": "filters.in.1.1: Invalid sender: must be a valid token or wallet address",
  "details": [{ "field": "filters.in.1.1", "message": "Invalid sender: must be a valid token or wallet address" }]
}
```

```jsonc Missing map entry theme={null}
// → "events": ["swap", "swap-enriched"], "filters": { "swap": { "eq": ["type", "buy"] } }
{
  "type": "error",
  "id": "keyed",
  "code": "INVALID_PARAMETERS",
  "message": "filters.swap-enriched: every requested event needs a filter",
  "details": [{ "field": "filters.swap-enriched", "message": "every requested event needs a filter" }]
}
```

Rely on the `field` path and `code` in your code. The wording of type and range messages (such as an expected number) may change.


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