Introduction
Drift Protocol v2 is a fully on-chain decentralized exchange (DEX) on Solana that offers perpetual futures and spot trading with up to 20x leverage. The protocol combines an Automated Market Maker (AMM) with a central limit order book (CLOB) to provide deep liquidity and efficient price discovery.Core Architecture
Drift’s architecture consists of several key components that work together to enable decentralized trading:Program Structure
The protocol is implemented as a Solana program with the following core modules:- State Management: Global state, market accounts, and user accounts
- Market Operations: Order matching, position management, and settlements
- Risk Engine: Margin calculations, liquidations, and risk parameters
- Oracle Integration: Price feeds from Pyth, Switchboard, and other sources
Account Types
State Account
Global protocol configuration including exchange status, fee structures, and admin settings
Market Accounts
Per-market configuration for perpetual and spot markets, including AMM parameters
User Accounts
Individual trading accounts holding positions, orders, and margin
User Stats
Aggregate statistics for fee tiers, referrals, and trading volume
State Account Structure
sdk/src/types.ts
Market Types
Drift supports two primary market types:Perpetual Markets
Perpetual futures contracts that track the price of an underlying asset through funding rates. Key features:- No expiration date: Positions can be held indefinitely
- Funding payments: Periodic payments between longs and shorts to keep mark price close to oracle price
- Up to 20x leverage: Depending on contract tier and position size
- AMM + CLOB hybrid: Combines automated market making with order book matching
Spot Markets
Spot markets allow trading and borrowing of assets:- Borrow and lend: Earn interest on deposits or borrow with collateral
- Cross-collateral: Use any whitelisted asset as collateral
- Interest rates: Dynamic rates based on utilization
- Swaps: Atomic swaps between spot assets
Spot markets use a scaled balance system with cumulative interest tracking to efficiently manage deposits and borrows across all users.
Market Status
Markets can be in various states that control allowed operations:sdk/src/types.ts
Contract Tiers
Perp markets are categorized by risk tiers that determine insurance coverage and margin requirements:sdk/src/types.ts
AMM Design
Drift uses a novel AMM design based on a virtual AMM (vAMM) that:- Maintains virtual reserves of base and quote assets
- Uses a constant product formula with repegging capability
- Adjusts the peg multiplier to track oracle prices
- Concentrates liquidity around the current price
- Socializes profit and loss through the PnL pool
AMM Structure
The AMM maintains several key parameters:sdk/src/types.ts
Exchange Status
The protocol has various operational modes:sdk/src/types.ts
Key Features
Cross-Margin by Default
Cross-Margin by Default
All positions and collateral are pooled by default, maximizing capital efficiency. Users can also opt for isolated margin on supported markets.
Sub-Accounts
Sub-Accounts
Each user can create multiple sub-accounts (up to 1000) to separate trading strategies or risk profiles.
Decentralized Keepers
Decentralized Keepers
The protocol relies on permissionless keeper bots to trigger liquidations, settle funding, and fill orders.
Insurance Fund
Insurance Fund
Protocol-owned insurance fund backstops losses from liquidations and bankruptcy events.
Next Steps
Markets
Learn about perpetual and spot market mechanics
Positions
Understand how positions are managed
Orders
Explore order types and execution
Margin System
Deep dive into margin calculations