Sub topic 8
A tour of DefiLlama
This chapter walks through DefiLlama's website and the data model behind it. It assumes you know what a DeFi protocol is, what an ERC-20 token is, and what a decentralized exchange does. Basic Python helps for the last example. You do not need Dune for this chapter. Later chapters compare the two tools.
What DefiLlama is
DefiLlama is a free aggregator of decentralized finance data. It tracks protocols across hundreds of blockchains and publishes the results as web dashboards and as a public API. Three properties shape how you should use it:
- It is open. The site needs no account, and the main API needs no key. The code that computes the numbers is public on GitHub.
- It is protocol-centric. Dune lets you query any transaction. DefiLlama gives you a curated list of protocols, each with a fixed set of metrics.
- It is contributed. Anyone can submit a pull request that adds or fixes a protocol's data adapter. A reviewer checks it before it goes live.
The last point matters for interpretation. A DefiLlama number is the output of a small program written by a person, not a raw measurement of the chain.
Where the numbers come from
Each protocol has an adapter, a short JavaScript module. The adapter says how to compute one metric for that protocol, such as which contracts to read or which token balances to sum. DefiLlama's servers run the adapters on a schedule and store the results in a time series. The website and the API both read from that store.
There are two repositories to know. One holds the TVL adapters. A separate one holds the "dimension" adapters for volume, fees and revenue. When a number looks odd, reading the adapter is often the fastest way to learn what it counts.
Adapters need prices as well as balances. Most TVL adapters return token balances. DefiLlama then multiplies them by its own price data. Because of this step, a DefiLlama figure can differ from a figure you compute yourself.
The main sections of the site
The left sidebar groups pages by metric. The table lists the sections you will use most and what each one answers.
| Section | Question it answers | Unit |
|---|---|---|
| DeFi (home) | Which protocols and chains hold the most value? | USD of TVL |
| Chains | How does value split across blockchains? | USD of TVL |
| Yields | What APY do pools pay, and where does it come from? | Percent APY |
| Stablecoins | How much of each stablecoin exists, and on which chains? | USD circulating |
| DEXs / Volumes | How much traded on exchanges in a period? | USD volume |
| Fees and Revenue | What did users pay, and what did protocols keep? | USD per period |
| Borrow | How much is lent and borrowed? | USD |
| Bridges | How much moves between chains? | USD flow |
| Hacks | Which protocols were exploited, and for how much? | USD lost |
| Raises | Who funded which projects? | USD raised |
A few sections, such as Hacks and Raises, are curated records. Most of the others, such as TVL, volume and fees, are computed from chain data. The curated ones depend on public reports. Treat the two kinds differently.
Reading the home page
The home page lists protocols ranked by TVL, which is total value locked. TVL is the USD value of assets held in a protocol's smart contracts. The next chapter covers what it includes and leaves out. For this tour, notice the table's columns and controls:
- Name and category. Each protocol has one category, such as Dexs, Lending, Liquid Staking or Bridge. Categories are a single label, so a protocol that does two things still gets one.
- Chains. A protocol can be deployed on many chains. The page shows a combined figure, and you can filter to one chain.
- Change columns. The 1-day, 7-day and 1-month changes mix two causes: assets moving in or out, and price movement. A rising TVL during a market rally may involve no new deposits.
- Settings menu. Toggles include or exclude categories of value, such as staking, borrowed amounts and pool2. Pool2 means liquidity-provider tokens for the protocol's own token. Changing a toggle changes every total on the page, so note your settings when you record a number.
Protocol pages
Click a protocol to open its page. You will find a TVL chart over time, a breakdown by chain and by token, and links to the website, contracts and adapter code. Other metrics appear on the same page when adapters exist, for example volume and fees for a DEX.
The token breakdown is useful for sanity checks. If one token makes up nearly all of a protocol's TVL, then a drop in that token's price will move the whole chart.
Chain pages
A chain page shows total TVL on that chain, its top protocols, stablecoin supply and DEX volume. Use it to compare ecosystems. Chain totals do not simply add up across chains, because the same asset can be counted twice when it is bridged. The pitfalls chapter returns to this.
Time, granularity and revisions
DefiLlama stores daily data points for most metrics. This has consequences:
- A day is a UTC day. If you compare with Dune, align on UTC dates.
- The latest point is partial. The current day can still change. Charts and API responses may include an incomplete last value.
- History can be revised. A contributor can fix an adapter and DefiLlama may recompute past values. A number you saved last month may no longer match the site.
For reproducible work, save the raw response with a retrieval date.
Pulling data from the API
The website is a front end for the API. The base URL https://api.llama.fi serves TVL data, and the same project runs other hosts for stablecoins and yields. A later chapter covers the API in more detail. The script below gives a first look at the data. It fetches the protocol list and sums TVL by category, which mirrors what you see on the home page.
import requests
from collections import defaultdict
resp = requests.get("https://api.llama.fi/protocols", timeout=30)
resp.raise_for_status()
protocols = resp.json()
totals = defaultdict(float)
for p in protocols:
tvl = p.get("tvl")
if not tvl:
continue
totals[p.get("category") or "n/a"] += tvl
ranked = sorted(totals.items(), key=lambda kv: kv[1], reverse=True)[:10]
for category, tvl in ranked:
print(f"{category:<25} ${tvl / 1e9:,.2f}B")Three details are worth noting. raise_for_status() turns an HTTP error into an exception instead of letting you parse an error page as data. The timeout stops the script from hanging. The if not tvl check skips protocols with a missing or zero value, since some entries lack a current figure.
Strengths and limits
DefiLlama is good at three things. It gives a fast, consistent view across many protocols. It is transparent, because you can read each adapter. And it needs no setup, which makes it a quick reference.
Its limits follow from its design:
- Coverage depends on adapters. A protocol with no adapter is invisible. A new or obscure protocol may be missing.
- Definitions are fixed. You cannot ask for volume excluding one pool or fees for one wallet. For that you need a query tool such as Dune.
- Prices are DefiLlama's. USD values depend on its price source and on how it prices unusual tokens.
- Methods differ by adapter. Two protocols in the same category may be measured with slightly different rules.
A short checklist for using a number
- Identify the metric (TVL, volume, fees or revenue) and its unit.
- Check the chain filter and the settings toggles.
- Note whether the value is for a full UTC day or the current partial day.
- Open the protocol page and look at the token breakdown for concentration.
- If the number matters, read the adapter or compare with an independent source.
Exercise
A colleague says a lending protocol's TVL "grew 40% this week" based on DefiLlama's 7-day change column. Name two different causes that could produce that figure, and one thing you would check on the protocol page to tell them apart.
Show answer
One cause is net deposits: users added more assets to the protocol. Another is price appreciation: the existing deposited tokens became worth more USD, with no new deposits. To tell them apart, open the protocol page and look at the token breakdown and the token's price move over the same week. If the dominant token's price rose by about the same percentage, the growth is mostly price. You could also check the settings toggles to see whether a category such as borrowed amounts was included or excluded.
Exercise
Why might a DefiLlama chart for a protocol show a different value for last month's date today than it did when you saved it a month ago?
Show answer
DefiLlama stores historical data computed by adapters. If a contributor fixes or changes an adapter, the past values can be recomputed, so history can be revised. For reproducible work, save the raw API response along with the retrieval date.