analysis

How to Use Dune Analytics for DeFi Research

Learn how to use Dune Analytics to query on-chain DeFi data with SQL. Step-by-step examples for tracking swaps, lending health, and whale activity.

How to Use Dune Analytics for DeFi Research

Dune Analytics is a powerful blockchain data platform that lets anyone query on-chain activity using SQL, making it an essential tool for DeFi research. By allowing you to access raw transaction data from Ethereum and other chains, Dune replaces guesswork with verifiable metrics. This guide will walk you through the basics, from writing your first query to analyzing real-world DeFi protocols.

What Makes Dune Analytics Essential for DeFi Research?

Dune Analytics stands out because it combines raw on-chain transparency with a user-friendly SQL interface. Instead of relying on third-party dashboards that may aggregate data incorrectly, you can write custom queries to verify any metric yourself. Here are the key features that make it indispensable for DeFi research:

  • Free access to historical blockchain data – query blocks, transactions, logs, and token transfers without API limits for most use cases.
  • Community-contributed dashboards – thousands of pre-built visualizations you can fork and modify.
  • SQL-driven analysis – use standard SQL to filter, aggregate, and join tables from multiple protocols.
  • Cross-chain support – Ethereum, Polygon, BNB Chain, Arbitrum, Optimism, and more.

The platform’s data is organized into decoded tables (e.g., uniswap_v3.swaps) and raw tables (e.g., ethereum.transactions). For DeFi research, decoded tables are the most convenient because they already translate smart contract parameters into readable columns like amount0 or recipient. The table below outlines common DeFi metrics you can extract with Dune Analytics.

MetricDescriptionExample Table
Swap volumeTotal tokens exchanged in a DEX pooluniswap_v3.swaps
Lending depositsAmount deposited into a lending poolaave_v2.deposits
Borrow activityTotal borrowed assets in a protocolaave_v2.borrows
Liquidation eventsLoans that dropped below collateral thresholdcompound_v2.liquidations
Token transfersMovement of ERC-20 tokens between addresseserc20_ethereum.evt_Transfer

⚠️ Warning: Decoded tables only cover protocols that have been officially decoded by Dune’s team or community. If you need data from a newer or less popular protocol, you may have to query raw event logs — this is more complex but still possible.

Getting Started: Your First Dune Analytics Query for DeFi Data

To begin, create a free account at dune.com. Once logged in, you can explore existing dashboards or open the Query Editor to write your own SQL. The simplest way to start is by forking an existing dashboard — find one that tracks Uniswap V3 volume, click “Fork”, and examine the underlying query.

For your first custom query, let’s retrieve the total number of swaps on Uniswap V3 on Ethereum for the past 7 days. Enter the following SQL in the editor:

SELECT COUNT(*) AS swap_count
FROM ethereum.uniswap_v3.swaps
WHERE block_time >= now() - interval '7' day

Run the query by clicking Execute. Dune will return a single number: the total swaps. To add more context, you can group by pool:

SELECT pool,
       COUNT(*) AS swap_count,
       SUM(amount0) AS total_volume
FROM ethereum.uniswap_v3.swaps
WHERE block_time >= now() - interval '7' day
GROUP BY pool
ORDER BY total_volume DESC

This query lists pools by trading activity. Notice the use of SUM(amount0) — the exact meaning of amount0 depends on the pool’s token configuration, but Dune’s decoded tables always include documentation you can review by clicking the “?” icon next to the table name.

💡 Pro Tip: Always add a time filter (WHERE block_time >= ...) to your queries. Without it, you may scan the entire history of a protocol, which can take minutes and consume credits. Start with a short range (e.g., 7 days) and expand only as needed.

Practical Example: Analyzing a DeFi Lending Protocol’s Health

A common DeFi research task is determining whether a lending protocol like Aave is over-leveraged. You can use Dune to calculate the utilization ratio — the proportion of deposited assets that have been borrowed. A very high ratio signals increased liquidation risk.

Here’s a query for Aave V2 on Ethereum that returns the total deposited USDC and total borrowed USDC:

SELECT 
    SUM(CAST(amount AS DOUBLE) / 1e6) AS total_deposits,
    (SELECT SUM(CAST(amount AS DOUBLE) / 1e6) 
     FROM ethereum.aave_v2.borrows
     WHERE currency = 'USDC') AS total_borrows
FROM ethereum.aave_v2.deposits
WHERE currency = 'USDC'

Because amounts are stored in their smallest unit (e.g., 6 decimals for USDC), you divide by 1e6 to get human-readable numbers. The result tells you if borrowing is approaching deposit levels — a high utilization scenario.

You can extend this query by adding a block_time range and plotting the ratio over time using Dune’s chart visualization. Choose a line chart and set the X-axis to time and Y-axis to the utilization formula total_borrows / total_deposits. This historical view helps you spot trends, such as a rapid increase in borrowing before a market crash.

⚠️ Warning: Dune’s data is only as accurate as the protocol’s contract events. If a lending pool uses a different contract version (e.g., Aave V3 instead of V2), your query may return zero rows. Always double-check the table name matches the protocol version you intend to analyze.

Using Dune Analytics to Track Token Flows and Whale Activity

Beyond protocol-level metrics, Dune is excellent for tracking individual token transfers and identifying whale behavior. For instance, you can monitor large stablecoin movements that often precede market moves. The erc20_ethereum.evt_Transfer table contains every ERC-20 transfer on Ethereum. Run this query to see the 100 largest USDC transfers in the last 24 hours:

SELECT 
    block_time,
    "from" AS sender,
    "to" AS receiver,
    CAST(value AS DOUBLE) / 1e6 AS amount
FROM erc20_ethereum.evt_Transfer
WHERE contract_address = 0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48  -- USDC contract
    AND block_time >= now() - interval '1' day
ORDER BY CAST(value AS DOUBLE) DESC
LIMIT 100

You can then join this with Dune’s labels table (e.g., labels.labels) to identify known exchange wallets or DeFi contracts. For example, if a large USDC transfer goes to a Binance hot wallet, it might indicate an upcoming trade. Dune’s labeling system is community-maintained, so always verify critical addresses independently.

To monitor whales in real time, create a dashboard with this query and set the refresh rate to every 10 minutes. Dune will automatically update the results, allowing you to spot suspicious activity quickly.

Conclusion: How Dune Analytics Empowers Your DeFi Research

Dune Analytics gives you direct access to the immutable ledger of DeFi, turning raw transactions into actionable insights. By learning to write simple SQL queries, you can verify liquidity, assess protocol health, and track capital flows without relying on opaque third-party dashboards. Start with the examples in this guide, fork community dashboards, and gradually explore more complex joins and visualizations. As your skills grow, Dune Analytics will become your most reliable lens into decentralized finance.