Skip to main content

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

Protocol Metadata

The PROTOCOL_META array defines all supported Stacks DeFi protocols with their metadata and fallback values:
PROTOCOL_META
array
Array of protocol configuration objects

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.
fetchAllProtocolData
function
Returns enriched protocol data with live or fallback values
Returns: Promise<Array<ProtocolData>>
ProtocolData
object
Extended protocol object with live data

Usage Example

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);
  }
}

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
  })
);

TVL Formatting

The service includes a formatTVL() helper that converts raw USD values to readable strings:
Raw ValueFormatted
1,234,567,890$1.23B
45,600,000$45.60M
431,000$431.0K
850$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

Build docs developers (and LLMs) love