Overview
Drift Protocol v2 uses a virtual Automated Market Maker (vAMM) with a constant product formula to determine prices and execute trades. The AMM maintains reserves of base and quote assets that determine the market price.Core AMM Formula
The AMM follows the constant product invariant:k (stored as sqrtK) remains constant during swaps (before accounting for fees and funding).
Price Calculation
The mark price is calculated from the AMM reserves:Formula
Example
Bid-Ask Price Calculation
The AMM applies spreads to quote bid and ask prices:- Updates AMM reserves based on oracle price (if
withUpdate = true) - Calculates spread based on volatility, inventory, and market conditions
- Applies spread to get bid/ask reserves
- Calculates prices from bid/ask reserves
Reserve Updates After Swap
When a trade occurs, the AMM reserves change according to the constant product formula:Swap Output Formula
Example: Calculating Trade Impact
Spread Calculation
The AMM calculates dynamic spreads based on multiple factors:Spread Components
- Base Spread - Minimum spread set by the market
- Volatility Spread - Based on oracle confidence and market standard deviation
- Inventory Spread - Scales with AMM’s inventory imbalance
- Effective Leverage Spread - Increases with AMM’s leverage
- Revenue Retreat - Additional spread when AMM has losses