feat: add DeFi data via DefiLlama API (TDD)
7 new endpoints under /api/v1/defi/ (all free, no API key):
- GET /defi/tvl/protocols - top DeFi protocols by TVL
- GET /defi/tvl/chains - chain TVL rankings
- GET /defi/tvl/{protocol} - single protocol TVL
- GET /defi/yields - top yield pools (filter by chain/project)
- GET /defi/stablecoins - stablecoin market data
- GET /defi/volumes/dexs - DEX volume overview
- GET /defi/fees - protocol fees/revenue overview
Data source: DefiLlama API (free, no key needed)
58 new tests (33 service + 25 route). All 561 tests passing.
This commit is contained in:
75
routes_defi.py
Normal file
75
routes_defi.py
Normal file
@@ -0,0 +1,75 @@
|
||||
"""DeFi data routes via DefiLlama API."""
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
import defi_service
|
||||
from models import ApiResponse
|
||||
from route_utils import safe
|
||||
|
||||
router = APIRouter(prefix="/api/v1/defi")
|
||||
|
||||
|
||||
@router.get("/tvl/protocols", response_model=ApiResponse)
|
||||
@safe
|
||||
async def tvl_protocols() -> ApiResponse:
|
||||
"""Get top DeFi protocols ranked by TVL."""
|
||||
data = await defi_service.get_top_protocols()
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/tvl/chains", response_model=ApiResponse)
|
||||
@safe
|
||||
async def tvl_chains() -> ApiResponse:
|
||||
"""Get TVL rankings for all chains."""
|
||||
data = await defi_service.get_chain_tvls()
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/tvl/{protocol}", response_model=ApiResponse)
|
||||
@safe
|
||||
async def protocol_tvl(protocol: str) -> ApiResponse:
|
||||
"""Get current TVL for a specific protocol slug."""
|
||||
tvl = await defi_service.get_protocol_tvl(protocol)
|
||||
if tvl is None:
|
||||
raise HTTPException(status_code=404, detail=f"Protocol '{protocol}' not found")
|
||||
return ApiResponse(data={"protocol": protocol, "tvl": tvl})
|
||||
|
||||
|
||||
@router.get("/yields", response_model=ApiResponse)
|
||||
@safe
|
||||
async def yield_pools(
|
||||
chain: str | None = Query(default=None, description="Filter by chain name"),
|
||||
project: str | None = Query(default=None, description="Filter by project name"),
|
||||
) -> ApiResponse:
|
||||
"""Get top yield pools, optionally filtered by chain and/or project."""
|
||||
data = await defi_service.get_yield_pools(chain=chain, project=project)
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/stablecoins", response_model=ApiResponse)
|
||||
@safe
|
||||
async def stablecoins() -> ApiResponse:
|
||||
"""Get top stablecoins by circulating supply."""
|
||||
data = await defi_service.get_stablecoins()
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/volumes/dexs", response_model=ApiResponse)
|
||||
@safe
|
||||
async def dex_volumes() -> ApiResponse:
|
||||
"""Get DEX volume overview including top protocols."""
|
||||
data = await defi_service.get_dex_volumes()
|
||||
if data is None:
|
||||
raise HTTPException(
|
||||
status_code=502,
|
||||
detail="Failed to fetch DEX volume data from DefiLlama",
|
||||
)
|
||||
return ApiResponse(data=data)
|
||||
|
||||
|
||||
@router.get("/fees", response_model=ApiResponse)
|
||||
@safe
|
||||
async def protocol_fees() -> ApiResponse:
|
||||
"""Get protocol fees and revenue overview."""
|
||||
data = await defi_service.get_protocol_fees()
|
||||
return ApiResponse(data=data)
|
||||
Reference in New Issue
Block a user