LiqX/data/protocol_risk.py
prazwal ratti 3ebebf6a72 bhok
Signed-off-by: prazwal ratti <prazwalr07@gmail.com>
2025-10-25 14:32:54 +05:30

176 lines
5.7 KiB
Python

"""
Protocol Risk Scoring
Fetches real risk metrics from DeFi Llama and DeFi Safety
"""
import aiohttp
import asyncio
from typing import Optional, Dict
from loguru import logger
class ProtocolRiskScorer:
"""
Fetch and calculate protocol risk scores from multiple sources
"""
def __init__(self):
self.defillama_url = "https://api.llama.fi"
self.risk_cache = {}
self.cache_ttl = 3600 # 1 hour cache
# Known protocol risk factors (updated from audits & exploits)
self.base_risk_scores = {
'aave': 2, # Very safe - audited, battle-tested
'compound': 3, # Safe - well-audited
'lido': 2, # Very safe - large TVL, audited
'maker': 2, # Very safe - oldest DeFi protocol
'uniswap': 3, # Safe - decentralized, audited
'curve': 3, # Safe - audited, complex but tested
'yearn': 5, # Moderate - complex strategies
'convex': 5, # Moderate - layered protocols
'kamino': 6, # Higher risk - newer protocol
'drift': 6, # Higher risk - newer protocol
'marinade': 5, # Moderate - Solana LST
}
logger.info("🛡️ ProtocolRiskScorer initialized")
logger.info(" Using DeFi Llama TVL + base risk scores")
async def get_protocol_tvl(self, protocol: str) -> Optional[float]:
"""
Get protocol TVL from DeFi Llama
Returns:
TVL in USD (billions)
"""
try:
# Normalize protocol name for DeFi Llama
protocol_map = {
'aave-v3': 'aave',
'compound-v3': 'compound',
'aave-v2': 'aave',
}
protocol_clean = protocol_map.get(
protocol.lower(), protocol.lower())
url = f"{self.defillama_url}/protocol/{protocol_clean}"
async with aiohttp.ClientSession() as session:
async with session.get(url, timeout=aiohttp.ClientTimeout(total=5)) as response:
if response.status == 200:
data = await response.json()
tvl = data.get('tvl', [{}])
if tvl and len(tvl) > 0:
current_tvl = tvl[-1].get('totalLiquidityUSD', 0)
tvl_billions = current_tvl / 1e9
logger.info(
f"📊 {protocol_clean} TVL: ${tvl_billions:.2f}B")
return tvl_billions
except Exception as e:
logger.debug(f"Could not fetch TVL for {protocol}: {e}")
return None
async def calculate_risk_score(
self,
protocol: str,
chain: str = 'ethereum',
amount_usd: float = 0
) -> int:
"""
Calculate comprehensive risk score (1-10, lower is safer)
Factors:
- Base protocol risk (audits, exploits, age)
- TVL (higher = safer)
- Chain risk (Ethereum safer than newer chains)
- Amount size (larger amounts = more scrutiny needed)
Returns:
Risk score 1-10
"""
# Start with base risk score
protocol_clean = protocol.lower().replace('-v3', '').replace('-v2', '')
base_risk = self.base_risk_scores.get(
protocol_clean, 7) # Default 7/10 for unknown
# Adjust for TVL (higher TVL = lower risk)
tvl = await self.get_protocol_tvl(protocol)
tvl_adjustment = 0
if tvl:
if tvl > 10: # >$10B TVL
tvl_adjustment = -2
elif tvl > 5: # >$5B TVL
tvl_adjustment = -1
elif tvl > 1: # >$1B TVL
tvl_adjustment = 0
elif tvl < 0.1: # <$100M TVL
tvl_adjustment = +2
else: # $100M-$1B TVL
tvl_adjustment = +1
# Adjust for chain risk
chain_risk = {
'ethereum': 0,
'arbitrum': +1,
'optimism': +1,
'polygon': +1,
'base': +1,
'avalanche': +1,
'solana': +2,
'ethereum-sepolia': +1, # Testnet
}
chain_adjustment = chain_risk.get(chain.lower(), +2)
# Adjust for amount size (large amounts need extra safety)
amount_adjustment = 0
if amount_usd > 1000000: # >$1M
amount_adjustment = -1 # Be more conservative
elif amount_usd > 100000: # >$100K
amount_adjustment = 0
# Calculate final score
final_score = base_risk + tvl_adjustment + chain_adjustment + amount_adjustment
# Clamp to 1-10 range
final_score = max(1, min(10, final_score))
logger.info(
f"🛡️ Risk Score for {protocol} on {chain}: {final_score}/10")
logger.info(
f" Base: {base_risk} | TVL: {tvl_adjustment:+d} | Chain: {chain_adjustment:+d} | Amount: {amount_adjustment:+d}")
return int(final_score)
def get_risk_description(self, risk_score: int) -> str:
"""Get human-readable risk description"""
if risk_score <= 2:
return "Very Low Risk"
elif risk_score <= 4:
return "Low Risk"
elif risk_score <= 6:
return "Moderate Risk"
elif risk_score <= 8:
return "High Risk"
else:
return "Very High Risk"
# Singleton instance
_protocol_risk_scorer = None
def get_protocol_risk_scorer() -> ProtocolRiskScorer:
"""Get global ProtocolRiskScorer instance"""
global _protocol_risk_scorer
if _protocol_risk_scorer is None:
_protocol_risk_scorer = ProtocolRiskScorer()
return _protocol_risk_scorer