Oracle Overview
Drift Protocol relies on decentralized oracle networks to provide accurate, manipulation-resistant price feeds for all markets. Oracles are critical for:- Marking positions to market for PnL calculations
- Calculating margin requirements and liquidation thresholds
- Determining funding rates for perpetual markets
- Validating order prices and preventing market manipulation
Supported Oracle Sources
Drift supports multiple oracle providers:sdk/src/types.ts
Pyth Network
Pyth is the primary oracle provider for Drift:- High-frequency updates: Sub-second price updates
- Confidence intervals: Built-in uncertainty quantification
- Pull model: Users submit price updates in transactions
- Push model: Traditional on-chain price updates
- Multiple price feeds: Standard, 1K precision, 1M precision for different asset types
Pyth Pull oracles require the user to submit a price update in the same transaction as their trade. This ensures the latest price is always used.
Switchboard
Switchboard provides decentralized oracle aggregation:- On-demand: Users can trigger updates when needed
- Aggregated data: Multiple data sources combined
- Lower latency: Fast price updates
Prelaunch Oracle
For markets before official price feeds exist:sdk/src/types.ts
Oracle Price Data
Oracle data includes price, confidence, and timing information:MM Oracle Price Data
For market making, additional data is included:Oracle Validity Checks
The protocol validates oracle data before use:sdk/src/types.ts
Validity Guard Rails
The protocol enforces guard rails on oracle data:sdk/src/types.ts
Historical Oracle Data
Markets maintain historical oracle data for TWAP calculations:sdk/src/types.ts
TWAP Calculation
Time-Weighted Average Price provides manipulation resistance: Where:period= TWAP window (e.g., 3600 seconds for 1-hour TWAP)Δt= time since last update
- Funding rate calculations
- Detecting price manipulation
- Guard rails against oracle divergence
Oracle Price in Margin Calculations
For margin calculations, conservative oracle prices are used:sdk/src/math/margin.ts
Oracle Price for AMM Operations
The AMM uses oracle prices to determine repegging targets:Oracle Confidence Intervals
Confidence intervals represent price uncertainty:- Margin calculations: Use worst-case price (minPrice for longs, maxPrice for shorts)
- Liquidations: Use conservative prices to avoid premature liquidation
- Order validation: Reject orders if confidence too wide
Oracle Update Frequency
Different oracle types have different update frequencies:Staleness Thresholds
The protocol defines staleness thresholds:Oracle Precision
Different oracle variants have different precision:- Standard: 6 decimals (e.g., $100.000000)
- 1K: 3 decimals (e.g., $100.000) - for higher-priced assets
- 1M: 0 decimals (e.g., $100) - for very high-priced assets
- StableCoin: 6 decimals with tighter bounds for stablecoins
PRICE_PRECISION (1e6) internally.
Oracle Fallbacks
If primary oracle fails, the protocol has fallback mechanisms:- Use last valid price: Within staleness threshold
- Use TWAP: If available and recent
- Use alternative oracle: If configured
- Pause operations: If no valid price available
Market Maker Oracle Updates
Market makers can submit their own oracle updates:Oracle Integration Example
Reading oracle data:Oracle Security Considerations
Price Manipulation Resistance
Price Manipulation Resistance
TWAPs and confidence intervals make flash loan attacks and single-block manipulation ineffective. The protocol uses multiple data points and time-weighted averages.
Staleness Protection
Staleness Protection
Strict staleness checks prevent use of outdated prices during oracle downtime. Operations are paused rather than using stale data.
Confidence Interval Validation
Confidence Interval Validation
Wide confidence intervals during volatility prevent risky operations. The protocol waits for confidence to narrow before allowing certain actions.
Multiple Oracle Support
Multiple Oracle Support
Supporting multiple oracle providers (Pyth, Switchboard) provides redundancy and reduces single points of failure.
Oracle Price Examples
Standard Asset (SOL):Next Steps
Markets
See how oracles are configured per market
Margin System
Understand oracle price usage in margin
AMM
Learn about AMM repegging with oracles
Integration Guide
Integrate oracle data in your app