The local powerhouse: A core pattern for on-chain analysis
Why make thousands of slow, rate-limited API calls when you can run complex SQL queries instantly on your own machine? This tutorial introduces the most effective pattern for crypto data analysis: creating a local, high-performance copy of a complete on-chain dataset. By fetching the data once, you unlock unlimited, high-speed analytical capabilities.Looking for other analytics solutions? Check out our full list of API Tutorials for more step-by-step guides.
uniswap_v3.db
file containing all pools and their recent trading history from Uniswap v3 on Ethereum. You will be able to:
- Run a robust, high-performance ETL script that pulls a complete dataset from the DexPaprika API.
- Perform complex SQL queries on this data instantly, without rate limits.
- Understand a professional workflow for acquiring and analyzing on-chain data.
- Eliminates Rate Limiting: Instead of thousands of small, repetitive API calls, you perform one efficient batch download.
- Unlocks True Analytical Power: Run complex joins, aggregations, and window functions that are impossible with a simple API.
- Creates a Foundation: The skills you learn here can be applied to any data source, preparing you for more advanced, real-time analysis.
Step 1: ETL pipeline
Create a Python script to fetch complete Uniswap v3 pool and OHLCV data.
Step 2: Instant SQL analysis
Populate your database and run complex SQL queries to find insights.
Step 3: AI-powered analysis with an MCP server
Connect your database to an AI assistant for natural language queries.
Step 1: Build your local data pipeline
First, let’s create a Python script to act as our ETL (Extract, Transform, Load) pipeline. This script will fetch all pool data for Uniswap v3 on Ethereum and their recent trading history, then load it into a local DuckDB database file. It leverages two key endpoints: the Top Pools on a DEX endpoint to discover pools, and the Pool OHLCV Data endpoint to fetch historical price data. Create a new file namedbuild_uniswap_db.py
.
build_uniswap_db.py
A simple, sequential script is great for learning, but real-world data fetching requires a more robust approach. Here is what we’ve used to make sure it runs reliably:
- Asynchronous Operations: By using
asyncio
andaiohttp
, the script can make many API requests concurrently instead of one by one. This means shorter time for completion. - Dynamic Windowing: The
get_pool_ohlcv
function calculates how much data to request per API call so that it gets all the data for each pool. - Concurrency Control & Throttling: An
asyncio.Semaphore
, combined with carefully tunedBATCH_SIZE
andasyncio.sleep()
calls, makes sure we don’t hit the rate limit. - Resiliency: The
fetch_with_retry
function automatically retries failed requests with an exponential backoff delay, making the pipeline resilient to temporary network issues.
Required libraries
Before running the script, make sure you have the necessary Python libraries installed.Step 2: Run the pipeline and query with SQL
Now, execute the script from your terminal. It will fetch all Uniswap v3 pool data from Ethereum and their recent trading history, then create auniswap_v3.db
file in a new dbs
directory. This may take several minutes, but it will be significantly faster than a purely sequential script.
Querying your new database
Once the script completes, you have a powerful local database at your fingertips. You can now use any SQL client that supports DuckDB, or Python itself, to perform instant, complex analysis. In step 3, we will connect the database to an AI assistant for natural language queries. If you want to query the database with a Python script, create a new file namedquery_duckdb.py
and paste the following code into it.
query_duckdb.py
Step 3: AI-powered analysis with an MCP server
While you can use any SQL client to query your database, the real power comes from connecting it to an AI assistant. By using a Model Context Protocol (MCP) server, you can enable your assistant to directly query theuniswap_v3.db
file you created. This allows you to ask for insights in plain English instead of writing SQL.
For this, we will use mcp-server-duckdb
, an open-source MCP server for DuckDB.
Install the DuckDB MCP server
You can install the server easily usingnpx
:
Configure your AI assistant
Next, you need to tell your AI assistant how to run the server. Add the following to yourclaude_desktop_config.json
file.
If you see a “Server disconnected” error after restarting your AI assistant, it means the application cannot find the
uvx
or npx
command. This happens because the application doesn’t share the same PATH
environment variable as your terminal.To fix this, you must use the full, absolute path to the command.- Find the absolute path by running
which uvx
orwhich npx
in your terminal. - Copy the output (e.g.,
/Users/yourname/.local/bin/uvx
or/opt/homebrew/bin/npx
). - Use that full path as the
command
value in the JSON configuration below.
uvx
, which is recommended. Make sure to replace </path/to/your/project>
with the actual absolute path to your project directory.
- “Using the duckdb-crypto tool, find the 5 pools with the highest 24-hour volume.”
- “What was the hourly volatility for the top pool yesterday?”
What you’ve built: From API calls to analytics powerhouse
By completing this tutorial, you have successfully transitioned from being a passive data consumer to an active data analyst. You’ve replaced the slow, restrictive pattern of making individual API calls with a fast, powerful, and scalable local analytics workflow. Key achievements:- Built a professional ETL pipeline: You have a reusable, high-performance Python script that can create a comprehensive local database from any supported DEX and network.
- Unlocked high-speed SQL: You can now perform complex analytical queries on a rich dataset in milliseconds, directly on your machine.
- Mastered a foundational workflow: This “local-first” data strategy is a cornerstone of professional data analysis. It enables deeper exploration, from high-level market trends down to individual wallet behaviors.
- Created a Reusable Asset: Your
uniswap_v3.db
file is a valuable, reusable asset for any future analysis, dashboarding, or AI integration project.