> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/drift-labs/protocol-v2/llms.txt
> Use this file to discover all available pages before exploring further.

# Protocol Overview

> Architectural overview of Drift Protocol v2, a decentralized perpetuals and spot exchange on Solana

## 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

<CardGroup cols={2}>
  <Card title="State Account" icon="gear">
    Global protocol configuration including exchange status, fee structures, and admin settings
  </Card>

  <Card title="Market Accounts" icon="chart-line">
    Per-market configuration for perpetual and spot markets, including AMM parameters
  </Card>

  <Card title="User Accounts" icon="user">
    Individual trading accounts holding positions, orders, and margin
  </Card>

  <Card title="User Stats" icon="chart-bar">
    Aggregate statistics for fee tiers, referrals, and trading volume
  </Card>
</CardGroup>

### State Account Structure

```typescript sdk/src/types.ts theme={null}
export type StateAccount = {
  admin: PublicKey;
  exchangeStatus: number;
  whitelistMint: PublicKey;
  discountMint: PublicKey;
  oracleGuardRails: OracleGuardRails;
  numberOfAuthorities: BN;
  numberOfSubAccounts: BN;
  numberOfMarkets: number;
  numberOfSpotMarkets: number;
  minPerpAuctionDuration: number;
  defaultMarketOrderTimeInForce: number;
  defaultSpotAuctionDuration: number;
  liquidationMarginBufferRatio: number;
  settlementDuration: number;
  maxNumberOfSubAccounts: number;
  signer: PublicKey;
  signerNonce: number;
  srmVault: PublicKey;
  perpFeeStructure: FeeStructure;
  spotFeeStructure: FeeStructure;
  lpCooldownTime: BN;
  initialPctToLiquidate: number;
  liquidationDuration: number;
  maxInitializeUserFee: number;
  featureBitFlags: number;
};
```

## 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

<Info>
  Spot markets use a scaled balance system with cumulative interest tracking to efficiently manage deposits and borrows across all users.
</Info>

## Market Status

Markets can be in various states that control allowed operations:

```typescript sdk/src/types.ts theme={null}
export class MarketStatus {
  static readonly INITIALIZED = { initialized: {} };
  static readonly ACTIVE = { active: {} };
  static readonly FUNDING_PAUSED = { fundingPaused: {} };
  static readonly AMM_PAUSED = { ammPaused: {} };
  static readonly FILL_PAUSED = { fillPaused: {} };
  static readonly WITHDRAW_PAUSED = { withdrawPaused: {} };
  static readonly REDUCE_ONLY = { reduceOnly: {} };
  static readonly SETTLEMENT = { settlement: {} };
  static readonly DELISTED = { delisted: {} };
}
```

## Contract Tiers

Perp markets are categorized by risk tiers that determine insurance coverage and margin requirements:

```typescript sdk/src/types.ts theme={null}
export class ContractTier {
  static readonly A = { a: {} };              // Highest insurance coverage
  static readonly B = { b: {} };              // Medium insurance coverage
  static readonly C = { c: {} };              // Lower insurance coverage
  static readonly SPECULATIVE = { speculative: {} };           // No insurance
  static readonly HIGHLY_SPECULATIVE = { highlySpeculative: {} }; // No insurance
  static readonly ISOLATED = { isolated: {} }; // Isolated margin only
}
```

<Warning>
  Isolated tier markets require isolated margin positions and cannot be used as cross-collateral.
</Warning>

## 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:

```typescript sdk/src/types.ts theme={null}
export type AMM = {
  baseAssetReserve: BN;          // Virtual base reserves
  quoteAssetReserve: BN;         // Virtual quote reserves
  sqrtK: BN;                     // Sqrt of constant product
  pegMultiplier: BN;             // Price peg to oracle
  cumulativeFundingRate: BN;     // Cumulative funding
  oracle: PublicKey;             // Oracle address
  oracleSource: OracleSource;    // Oracle type
  baseAssetAmountWithAmm: BN;    // Net AMM position
  baseAssetAmountLong: BN;       // Total long positions
  baseAssetAmountShort: BN;      // Total short positions
  // ... additional fields
};
```

## Exchange Status

The protocol has various operational modes:

```typescript sdk/src/types.ts theme={null}
export enum ExchangeStatus {
  ACTIVE = 0,
  DEPOSIT_PAUSED = 1,
  WITHDRAW_PAUSED = 2,
  AMM_PAUSED = 4,
  FILL_PAUSED = 8,
  LIQ_PAUSED = 16,
  FUNDING_PAUSED = 32,
  SETTLE_PNL_PAUSED = 64,
  AMM_IMMEDIATE_FILL_PAUSED = 128,
  PAUSED = 255,
}
```

## Key Features

<AccordionGroup>
  <Accordion title="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.
  </Accordion>

  <Accordion title="Sub-Accounts">
    Each user can create multiple sub-accounts (up to 1000) to separate trading strategies or risk profiles.
  </Accordion>

  <Accordion title="Decentralized Keepers">
    The protocol relies on permissionless keeper bots to trigger liquidations, settle funding, and fill orders.
  </Accordion>

  <Accordion title="Insurance Fund">
    Protocol-owned insurance fund backstops losses from liquidations and bankruptcy events.
  </Accordion>
</AccordionGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Markets" icon="store" href="/concepts/markets">
    Learn about perpetual and spot market mechanics
  </Card>

  <Card title="Positions" icon="chart-area" href="/concepts/positions">
    Understand how positions are managed
  </Card>

  <Card title="Orders" icon="list-check" href="/concepts/orders">
    Explore order types and execution
  </Card>

  <Card title="Margin System" icon="scale-balanced" href="/concepts/margin">
    Deep dive into margin calculations
  </Card>
</CardGroup>
