Oracle Integration
Integrate price oracles for asset valuation in Flux.
Overview
Flux Protocol uses a centralized Oracle Registry architecture to provide consistent USD valuations across all vaults. The registry maps assets to oracle adapters that implement the IPriceOracle interface, enabling standardized price queries for health calculations, liquidations, and position tracking.
Architecture Diagram
┌─────────────────────────────────────────────────────────────────┐
│ Vault Operations │
│ (borrow, deposit, callback) │
└──────────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Asset Wrappers │
│ (ERC20, ERC4626, LP Positions) │
│ │
│ getValue(vault, manager, positionId) │
│ └── registry.getValueUSD(asset, balance) │
└──────────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Oracle Registry │
│ │
│ getValueUSD(asset, amount) → USD value (18 decimals) │
│ getPriceUSD(asset) → price per unit (18 decimals) │
│ getOracle(asset) → IPriceOracle adapter │
└──────────────────────────┬──────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────────┐
│ Oracle Adapters │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────────┐ │
│ │ Chainlink│ │ Uniswap │ │ Fixed │ │ Exchange Rate │ │
│ │ Adapter │ │ V3 TWAP │ │ Price │ │ Adapter │ │
│ └──────────┘ └──────────┘ └──────────┘ └──────────────────┘ │
└─────────────────────────────────────────────────────────────────┘Core Interfaces
IOracleRegistry
The Oracle Registry is the central hub for all price queries:
IPriceOracle
All oracle adapters must implement this interface:
Oracle Adapter Types
1. Chainlink Oracle Adapter
Best For: Major assets with established Chainlink price feeds (WETH, WBTC, USDC, DAI)
Configuration Example:
2. Uniswap V3 TWAP Oracle
Best For: Long-tail assets without Chainlink feeds but with sufficient Uniswap V3 liquidity
Security Considerations:
Use minimum 30-minute TWAP to resist manipulation
Ensure pool has sufficient liquidity (>$1M TVL recommended)
Monitor for liquidity withdrawal attacks
3. Fixed Price Oracle
Best For: Stablecoins pegged to $1 USD (USDC, USDT, DAI)
When to Use:
Highly trusted stablecoins with strong peg guarantees
Reduces oracle costs and complexity
Warning: Does not capture depeg events
4. Exchange Rate Oracle
Best For: Wrapped/rebasing tokens (wstETH, rETH, cbETH, stETH)
5. Composite Oracle with Fallback
Best For: Critical assets requiring high availability
Integrating Oracles with Asset Wrappers
Asset wrappers query the Oracle Registry to determine position values:
Decimal Handling
The Oracle Registry automatically normalizes all values to 18 decimals:
Examples:
USDC (6 dec)
1_000_000e6
1e18
1_000_000e6 × 1e18 / 1e6
1_000_000e18
WETH (18 dec)
10e18
3000e18
10e18 × 3000e18 / 1e18
30_000e18
WBTC (8 dec)
1e8
60_000e18
1e8 × 60_000e18 / 1e8
60_000e18
Health Ratio Calculation Flow
Oracle prices drive health and liquidation calculations:
Liquidation Threshold Formula:
Deploying the Oracle System
Step 1: Deploy Oracle Registry
Step 2: Deploy Oracle Adapters
Step 3: Register Oracles in Registry
Step 4: Connect Registry to Factory
Security Best Practices
1. Staleness Checks
Always validate price freshness:
2. Price Deviation Monitoring
Detect potential manipulation:
3. Multi-Oracle Validation
Use multiple sources for critical assets:
4. Circuit Breakers
Implement emergency pause functionality:
Oracle Selection Guidelines
Major tokens (ETH, BTC)
Chainlink
Decentralized, manipulation-resistant
Stablecoins (USDC, DAI)
Fixed Price or Chainlink
Low volatility, simple
LSTs (wstETH, rETH)
Exchange Rate + Base Oracle
Accurate intrinsic value
Long-tail ERC20
Uniswap V3 TWAP (30min+)
Manipulation-resistant
LP Tokens
Custom (component pricing)
Sum of underlying values
NFTs/ERC721
Floor price oracles
Specialized providers
Testing Oracle Integrations
Last updated