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
Oracle data includes price, confidence, and timing information:
export type OraclePriceData = { price: BN; // Current price confidence: BN; // Confidence interval delay: BN; // Age of the price data hasSufficientNumberOfDataPoints: boolean; maxPrice?: BN; // Maximum valid price};
export enum OracleValidity { NonPositive = 0, // Price <= 0 TooVolatile = 1, // Confidence too wide TooUncertain = 2, // Confidence interval too large StaleForMargin = 3, // Too old for margin calc InsufficientDataPoints = 4, // Not enough data sources StaleForAMMLowRisk = 5, // Too old for AMM operations isStaleForAmmImmediate = 6, // Too old for immediate fills Valid = 7, // Valid for use}
export type OracleGuardRails = { priceDivergence: { markOraclePercentDivergence: BN; // Max mark-oracle divergence oracleTwap5MinPercentDivergence: BN; // Max oracle-TWAP divergence }; validity: { slotsBeforeStaleForAmm: BN; // Max age for AMM operations slotsBeforeStaleForMargin: BN; // Max age for margin calc confidenceIntervalMaxSize: BN; // Max confidence interval tooVolatileRatio: BN; // Max volatility ratio };};
For margin calculations, conservative oracle prices are used:
sdk/src/math/margin.ts
export function calculateOraclePriceForPerpMargin( perpPosition: PerpPosition, market: PerpMarketAccount, oraclePriceData: OraclePriceData): BN
For Long Positions:marginPrice=oraclePrice−offsetFor Short Positions:marginPrice=oraclePrice+offsetWhere:offset=min(maxSpread×oraclePrice,confidence+baseSpread×oraclePrice)This provides a safety buffer against adverse price movements.
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
Strict staleness checks prevent use of outdated prices during oracle downtime. Operations are paused rather than using stale data.
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
Supporting multiple oracle providers (Pyth, Switchboard) provides redundancy and reduces single points of failure.