Overview
The DefiLlama service integrates with DefiLlama’s API to fetch real-time Total Value Locked (TVL) and Annual Percentage Yield (APY) data for Stacks DeFi protocols.
Location: src/services/defiLlamaService.js
API Strategy
Data Sources
TVL: api.llama.fi/protocol/{slug} - Works for all Stacks protocols
APY: Fetched from medianApy field in protocol endpoint
Logos: icons.llama.fi/{slug}.png
The PROTOCOL_META array defines all supported Stacks DeFi protocols with their metadata and fallback values:
Array of protocol configuration objects Show Protocol Object Properties
Unique protocol identifier (e.g., stackingdao, zest, alex)
Display name of the protocol
DefiLlama protocol slug for API calls
Protocol category: Stacking, Lending, DEX, Yield, or Borrowing
Primary asset type (e.g., STX, sBTC, sBTC/STX)
Risk level: Low, Medium, or High
Whether the protocol has been security audited
Minimum deposit requirement
Logo URL (local or DefiLlama CDN)
Brand color hex code for UI theming
Fallback APY when API returns null (percentage)
Fallback TVL in USD (optional, for protocols with inconsistent API data)
Formatted fallback TVL string (optional)
Example Protocol Entry
{
id : 'stackingdao' ,
name : 'StackingDAO' ,
slug : 'stackingdao' ,
type : 'Stacking' ,
asset : 'STX' ,
risk : 'Low' ,
audited : true ,
minDeposit : '100 STX' ,
url : 'https://stackingdao.com' ,
logo : '/logos/stackingdao.png' ,
color : '#3B82F6' ,
fallbackApy : 9.5 ,
}
Main Function
fetchAllProtocolData()
Fetches live TVL and APY data for all protocols in parallel.
Returns enriched protocol data with live or fallback values
Returns: Promise<Array<ProtocolData>>
Extended protocol object with live data Raw TVL in USD (null if unavailable)
Formatted TVL string (e.g., $45.2M, $1.2B)
APY percentage (live or fallback)
Formatted APY string (e.g., 9.5%)
Data source: live or fallback
Usage Example
Basic Usage
React Component
Filter by Type
import { fetchAllProtocolData } from './services/defiLlamaService' ;
async function loadProtocols () {
try {
const protocols = await fetchAllProtocolData ();
protocols . forEach ( protocol => {
console . log ( ` ${ protocol . name } :` );
console . log ( ` TVL: ${ protocol . tvl } ` );
console . log ( ` APY: ${ protocol . apyDisplay } ` );
console . log ( ` Data: ${ protocol . apySource } ` );
});
} catch ( error ) {
console . error ( 'Failed to fetch protocol data:' , error );
}
}
import { useState , useEffect } from 'react' ;
import { fetchAllProtocolData } from '../services/defiLlamaService' ;
function ProtocolList () {
const [ protocols , setProtocols ] = useState ([]);
const [ loading , setLoading ] = useState ( true );
useEffect (() => {
fetchAllProtocolData ()
. then ( setProtocols )
. finally (() => setLoading ( false ));
}, []);
if ( loading ) return < div > Loading protocols... </ div > ;
return (
< div >
{ protocols . map ( p => (
< div key = { p . id } >
< h3 > { p . name } </ h3 >
< p > TVL: { p . tvl } | APY: { p . apyDisplay } </ p >
</ div >
)) }
</ div >
);
}
import { fetchAllProtocolData } from './services/defiLlamaService' ;
async function getStackingProtocols () {
const all = await fetchAllProtocolData ();
// Filter by protocol type
const stackingProtocols = all . filter ( p => p . type === 'Stacking' );
// Sort by APY descending
stackingProtocols . sort (( a , b ) => ( b . apy || 0 ) - ( a . apy || 0 ));
return stackingProtocols ;
}
Implementation Details
TVL Fetching
The service prioritizes Stacks-specific chain TVL over total protocol TVL:
const tvlRaw =
data ?. currentChainTvls ?. Stacks ??
data ?. currentChainTvls ?. stacks ??
data ?. tvl ??
null ;
APY Resolution
APY is sourced from DefiLlama’s medianApy field with fallback:
const apyRaw =
data ?. medianApy ??
data ?. apy ??
null ;
const apy = fetched . apy ?? meta . fallbackApy ?? null ;
Error Handling
Uses Promise.allSettled() to ensure partial failures don’t break entire data fetch:
const results = await Promise . allSettled (
PROTOCOL_META . map ( async ( meta ) => {
// Individual protocol fetch with try/catch
})
);
The service includes a formatTVL() helper that converts raw USD values to readable strings:
Raw Value Formatted 1,234,567,890 $1.23B45,600,000 $45.60M431,000 $431.0K850 $850
Supported Protocols
StackingDAO Liquid stacking protocol for STX
Zest Protocol Bitcoin-backed lending with sBTC
ALEX Lab Decentralized exchange and AMM
Bitflow Liquidity pools and swaps
Hermetica Yield optimization strategies
Velar DEX with advanced trading features
Granite Bitcoin-collateralized borrowing
useProtocolData Hook React hook wrapper with auto-refresh
Portfolio Protocols Detect user positions in protocols