AI Service Architecture
The AI Risk Engine is a Python FastAPI service that analyzes DeFi protocols, generates allocation recommendations, and commits them on-chain for verification.
System Overview
Data Sources
The AI service fetches real-time data from trusted DeFi data providers:
DefiLlama
Primary source for protocol TVL and historical data.
- ‣Current TVL per protocol
- ‣7-day TVL change percentage
- ‣Protocol category and chain
CoinGecko
Token price data and volatility metrics.
- ‣Current token prices
- ‣7-day price change
- ‣Market cap and volume
Risk Model
The risk model produces a score from 0 (lowest risk) to 100 (highest risk) based on multiple weighted factors:
| Factor | Weight | Low Risk | High Risk |
|---|---|---|---|
| TVL Size | 25% | >$1B TVL | <$100M TVL |
| TVL Stability | 20% | <5% change/week | >20% change/week |
| Volatility | 25% | <10% 7d change | >30% 7d change |
| Protocol Age | 15% | >2 years | <6 months |
| Audit Status | 15% | Multiple audits | No audits |
Allocation Engine
The allocator optimizes for risk-adjusted returns while respecting DAO constraints.
Fetch risk data
APY / Risk ratio
Apply DAO limits
Allocation weights
Fetch risk data
APY / Risk ratio
Apply DAO limits
Allocation weights
The algorithm calculates: score = APY / (riskScore + 1) for each protocol, then normalizes to 100%.
Volatility Simulator
For demo purposes, the service can inject market scenarios that affect risk scores:
| Scenario | Effect | Duration | Use Case |
|---|---|---|---|
| stable | No changes | Indefinite | Normal operation |
| mild_stress | 2x volatility, -5% TVL | 5 min | Minor market dip |
| flash_crash | 5x volatility, -20% TVL | 3 min | Major crash event |
| exploit_scare | Single protocol spike | 4 min | Exploit news |
| bull_run | 1.5x volatility, +10% TVL | 5 min | Market rally |
OBSQRA_MODE=demo. In testnet and mainnet modes, only real data is used.Configuration
The service supports three modes with different behaviors:
| Setting | Demo | Testnet | Mainnet |
|---|---|---|---|
| Volatility Injection | Enabled | Disabled | Disabled |
| Rebalance Trigger | 5% drift | 10% drift | 15% drift |
| Min Rebalance Interval | 1 minute | 5 minutes | 1 hour |
| Audit Logging | Always On | Always On | Always On |
| Data Refresh | 5 minutes | 5 minutes | 5 minutes |
API Endpoints
/healthHealth check endpoint. Returns service status and model state.
/risk-score/allGet risk scores for all supported protocols. Returns scores 0-100.
/generate-recommendationGenerate a new allocation recommendation based on current pool state.
/simulate/scenarioTrigger a volatility scenario (demo mode only). Scenarios: stable, mild_stress, flash_crash, exploit_scare, bull_run.