Skip to main content

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:
Where 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:
The function:
  1. Updates AMM reserves based on oracle price (if withUpdate = true)
  2. Calculates spread based on volatility, inventory, and market conditions
  3. Applies spread to get bid/ask reserves
  4. 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

  1. Base Spread - Minimum spread set by the market
  2. Volatility Spread - Based on oracle confidence and market standard deviation
  3. Inventory Spread - Scales with AMM’s inventory imbalance
  4. Effective Leverage Spread - Increases with AMM’s leverage
  5. Revenue Retreat - Additional spread when AMM has losses

Inventory Scale

The AMM adjusts spreads based on inventory imbalance:

Reference Price Offset

For markets with high liquidity intensity, the AMM applies a reference price offset:

Terminal Price

The terminal price is the price at which the AMM’s inventory would be fully closed:

Peg Adjustment

The AMM can adjust its peg multiplier to keep the mark price close to the oracle price:

Practical Examples

Get Current Market Price

Calculate Slippage