WebSocket), or Python 3 with the websockets package.
1. Choose the subscription
chainsuses theevm:<chainId>form. Bare numbers are rejected.swap-enrichedaddsbaseTokenDataandquoteTokenDatawith name, symbol, decimals and logo, so you don’t need a separate token lookup. It contains every field ofswap, so there is no reason to request both.tokenAmountUsdchecks the trade’samountUSD. Comparisons need a JSON number, not a string.senderchecks the trade’sinitiator: the wallet that sent the transaction. Up to 500 wallets fit in oneinlist.
What it costs
The connection budget is 100 cost units. This subscription has two pairs,swap-enriched|evm:1 and swap-enriched|evm:8453, and each costs 10, for a total of 20.
Each pair is chain-wide because of the or. A filter that only names wallets would cost 1 per pair, since the server only has to look at those wallets’ trades. But the gte branch doesn’t name any address, so the server must check every trade on the chain against the threshold, and an or with one such branch is chain-wide as a whole.
Splitting it into two subscriptions doesn’t make it cheaper: a threshold subscription costs 2 × 10 = 20 and a wallet subscription 2 × 1 = 2, total 22. Split it only if you want each rule under its own subscription id. Adding the plain swap kind would double the cost to 40. See Limits and subscription cost.
2. Read the trades
Each match arrives as anevent. With two pairs, the event has a pair field and a cursor:
- Amount: the absolute value of
amount0oramount1on the base token’s side. - Price:
baseTokenData.priceUSD, which is the price at this trade. IfbaseTokenDataisnull, usepriceUSD0orpriceUSD1. - USD value:
amountUSD. - Symbol:
baseTokenData.symbol. Token data can benullwhen the token isn’t known yet or the lookup was slow, so fall back to the address.
3. Keep cursors and resume
Store the newest cursor of each pair. Cursors arrive in three places:subscribedhas acursorsmap with the starting cursor of each pair.- Every
eventhas acursorfor itspair. - Every 30 seconds, a
cursormessage moves each pair forward, even when nothing matched. A rare filter like this one depends on these checkpoints to resume after a quiet period.
params and a since map:
RESUME_EXPIRED, and details.pairs lists the expired pairs. Drop those pairs from since and subscribe again. See Cursors and resuming.
Full example
The client below prints alerts, saves cursors tocursors.json so it can resume after a restart, and reconnects with backoff.
hash and logIndex if that matters.
If you change the chains or event kinds, delete the saved cursors for pairs that no longer exist. Sending a pair the new params don’t produce fails with Unknown pairs in since.
Optional: backfill a gap or an expired resume
Two situations leave holes in the feed:- A
gapmessage: the stream skipped the listed blocks on that chain, inclusive, and won’t send their trades. RESUME_EXPIRED: you were disconnected for longer than the stream’s history.
GET /trades reads trades from storage and can fill a hole when you know which token or pool to look at. It takes a token or pool address, a chain, and a time window in Unix milliseconds (from, to), not block numbers. Use the timestamp of your last trade before the hole and of the first one after it.
- Pages hold up to 100 trades. For the next page, pass the
cursorof the last trade with the same query, and stop when a page has fewer thanlimittrades. - The REST trade shape differs from the stream payload: it is oriented to one token and has
tokenAmountUsd,type(buyorsell) andsenderfields. Apply your threshold and wallet list to those fields. - For a token address, only the token’s two most active pools are searched. Query by pool address when you can.