Data access · 10 min read
How to index Uniswap v3 swaps on Base into Postgres with TypeScript
This guide builds a TypeScript indexer that reads Uniswap v3 Swap events on Base and writes one Postgres row per swap, using SQD's Pipes SDK and the keyless public Portal. The code, the terminal output and the psql results below are pasted from a run on December 1, 2026, over a pinned window of 3,000 Base blocks, so you can rerun it and compare. Two traps silently miscount Uniswap swaps on Base, and both are measured here.
1. What you end up with
The finished indexer is one TypeScript file and one Postgres table. It asks SQD Portal for the Swap logs of three pools, decodes them with the pool ABI, and inserts them through a Drizzle target that also stores the stream cursor and undoes rows from blocks a reorg replaces. The pools are WETH/USDC on Uniswap v3, one per fee tier, which together had 839 swaps in the window, a third of all Uniswap v3 swaps on Base in those blocks.
The 0.01% pool, 0xb4cb…00e5, was the busiest Uniswap v3 pool on Base in the window: its 644 swaps were 25.6% of the 2,515 Uniswap v3 swaps across 333 pools, and the next pool had 185. The window is 3,000 blocks, ten minutes of Base, because the public Portal is a rate-limited exploration endpoint. The same code runs over any range; a full-history backfill needs a paid plan (section 11).
2. Find the pools from the factory
Take pool addresses from the factory contract, not from a token list or a search result. Uniswap's Base deployments page lists UniswapV3Factory at 0x3312…FDfD, and its getPool(tokenA, tokenB, fee) returns the one canonical pool per pair and fee. The tokens are WETH (0x4200…0006) and USDC (0x8335…2913, from Circle's address list). For the 0.05% tier, fee 500:
Fee 100 (0x64) returns 0xb4cb…00e5 and fee 3000 (0xbb8) returns 0x6c56…1372. Those three addresses are the whole configuration of the indexer.
3. Set up Postgres and the project
Any Postgres works; a throwaway container is enough to follow along. The SDK version below is the npm latest tag on the day of the run, 1.0.0-beta.12 (MIT license). It is a beta and its export names have changed between releases, so pin it, and check the names against the installed package if you use a newer one. drizzle-orm and pg are pinned to the versions the SDK lists as peer dependencies. The run used Node 22.23.
4. The indexer
The whole indexer is the file below, exactly as it ran. It has four parts: the Swap event ABI, the pool list, the Drizzle table, and the stream wired to the Postgres target.
Four details in it matter more than they look:
- Amounts go in as
numeric.amount0andamount1areint256andsqrtPriceX96isuint160; they decode tobigintand overflowbigintcolumns, so the transform turns them into strings for Postgresnumeric. - The table needs a primary key. The target refuses a table without one, because it keys its reorg undo log on it.
(block_number, log_index)identifies a log uniquely on one chain. - The
idis the cursor key. The target saves its position in asynctable under this id, so a restart resumes instead of reinserting. Change the id and the pipe starts over. - The range is pinned.
to: 64_702_999makes the process exit after the window. Removetoand it keeps following the chain head (section 9).
5. Run it
On the public endpoint, the first request is often refused and retried. Four of the seven runs made for this guide on December 1, including one from a fresh directory, got a 529 overloaded on their first request; the SDK logs a warning, waits ten seconds and retries on its own. The run below, from a fresh directory on December 2, got no 529 and took 1.6 seconds end to end. Timestamps are the machine's local clock (UTC+1):
Running the same command again inserts nothing, because the cursor in sync already points at the end of the window:
The row count stayed at 839 after the second run. The database now has three tables: uniswap_v3_swaps, the cursor table sync, and uniswap_v3_swaps__snapshots, the undo log the target creates for reorgs.
6. Query the swaps in Postgres
Swap counts and volume per fee tier are one GROUP BY. USDC has 6 decimals, so the USDC leg of each swap divided by 1e6 is its dollar size:
Swap count and dollar volume rank the tiers in opposite orders. The 0.30% pool had the fewest swaps and three quarters of the volume, $537.3k of about $718.2k, and most of that came from a handful of trades: the five largest swaps in the tier were $295.1k, 54.9% of it. The largest single one sold 62.30 WETH for 106,677.64 USDC in 0x2c92…bab1. In a ten-minute window, a few trades decide the volume ranking, so read the tier split as a property of this window, not of the pools.
Individual swaps read the same way. The first row in block order is the first swap of the window:
The signs are from the pool's side. A positive amount entered the pool and a negative amount left it, so in 0xad0e…46ae at block 64,700,007 the trader paid 5.611888 USDC into the 0.01% pool and received 0.003272 WETH, a price of 1,714.99 USDC per WETH. Volume queries take abs() of one leg; buy and sell splits read the sign.
7. Trap 1: the Swap topic is not Uniswap
Filtering on the Swap event signature alone counts every fork of Uniswap v3 as Uniswap. Forks that kept the pool contract emit the same event, so they share its topic hash, 0xc42079f9…ca67. Over the same 3,000 blocks, the Portal returned 10,227 logs with that topic from 667 contracts. Checking each contract against Uniswap's factory left 2,515 swaps from 333 pools, 24.6% of the topic count:
A topic-only filter would have overstated Uniswap v3 swaps on Base about fourfold in this window. The check used two methods that agreed on every address: each contract's factory() return value, and the pool address recomputed with CREATE2 from the factory, the pool's token0, token1 and fee, and Uniswap's pool init code hash. Only the second is proof, because any contract can return any address from factory(). The indexer above avoids the trap by passing pool addresses in contracts, which the Portal applies as a log filter before anything is sent.
8. Trap 2: a factory registry needs history
To index every Uniswap v3 pool instead of a list, the SDK provides contractFactory, documented in the factory transformers guide. It reads the factory's PoolCreated events into a SQLite registry and keeps a Swap only when its emitter is in that registry. The registry only learns pools created inside the range you index, though. Here is the factory version run over the same window (contractFactorySqliteStore needs better-sqlite3, an optional peer dependency of the SDK: npm install better-sqlite3@12.4.5):
After three 529 retries, it decoded 0 of the window's 2,515 Uniswap v3 swaps, without an error. The factory created no pools in those 3,000 blocks, so the registry stayed empty and every swap was dropped as unknown. A registry is complete only when the range starts at the factory's first PoolCreated event, which on Base is block 2,009,446 (July 31, 2023, transaction 0x9a9b…8b08).
Starting there is a full-history backfill of 62.7 million blocks, and it costs more than the pool events. To find Uniswap's swaps, contractFactory asks the Portal for every log with the Swap topic and filters in your process, so it downloads the forks' swaps too, about four times the Uniswap volume if this window is typical. That is a job for a paid plan. For a free-tier exploration, a named pool list is the correct design.
9. Following the chain head
Without a to, the same pipe catches up and then keeps polling for new blocks. Started 300 blocks behind the head into a separate database, it reported in sync within five seconds and then advanced about five blocks a second:
In 45 seconds it wrote 143 swaps. At the head, blocks are not yet final, and the target handles that without code from you: for every row written in an unfinalized block it records an entry in uniswap_v3_swaps__snapshots (143 entries for the 143 rows here), and if the chain reorganizes it uses them to remove or restore rows before writing the new branch. The pinned window was already final when it ran, so its snapshot table stayed empty.
10. Why this is harder over RPC
An RPC-based indexer reads the same logs with eth_getLogs, and the free endpoints cap it by range and by age. The same three-pool query for the 3,000-block window, sent to public Base endpoints on the same day, came back three different ways:
The endpoint that answered agrees with the Portal to the row, so the data is the same; the paging around it is not. At drpc's 100-block limit, the window takes 30 requests, and reading Base from genesis to this window takes about 647,000. The endpoint that accepted wide ranges refuses old blocks without an archive token. The Portal answered the 3,000-block window in one response, and the same request shape runs over any range of the chain without an archive node to provision. The eth_getLogs limits guide covers the caps in detail.
11. Limits of the free endpoint
The public Portal is sized for trying things, and this guide stays inside that. Its published allowance is about 50 MB a month at 0.1 MB/s. The response for this window was 877,677 bytes, so the allowance covers roughly 57 windows like it, under ten hours of WETH/USDC swaps at this window's rate, and it answered 529 to the first request of four of the seven runs on December 1.
A full-history backfill of these pools, or of every Uniswap v3 pool through contractFactory, needs a paid Portal plan; plans and their throughput are on the pricing page, and billing is not live yet. One measurement shows the gap: a factory-only PoolCreated scan from genesis, retrying 529s on the public endpoint, covered blocks 0 to 2,893,225, under 5% of the chain, in about ten minutes. The decoder and the target do not change between plans. Only the range changes, and the portal option, which takes the plan's endpoint and an API key sent as an x-api-key header.
Frequently asked questions
How do I index Uniswap v3 swaps on Base into Postgres with TypeScript?
What is the Uniswap v3 factory address on Base?
Why does filtering on the Uniswap v3 Swap topic return too many swaps?
Can I index every Uniswap v3 pool on Base, not only WETH/USDC?
Can the free public Portal backfill all Uniswap v3 history on Base?
How does this compare to Ponder or Envio?
Related guides
Index your own pools
The Pipes SDK quickstart scaffolds the same Postgres setup for any contract on any of the Portal's EVM networks.