Quick Start Guide
This guide will walk you through placing your first perpetual trade on Drift Protocol, from initialization to order execution.This quickstart uses devnet for testing. No real funds are required!
Overview
You’ll learn how to:- Initialize the Drift SDK
- Connect your wallet
- Create a Drift user account
- Deposit collateral (USDC)
- Place a perpetual order
- Monitor your position
Complete Example
Here’s a complete example that places a 1 SOL-PERP long order:trade-example.ts
Step-by-Step Breakdown
Let’s break down each step in detail:1. Initialize the SDK
initialize function returns configuration for the specified environment:
DRIFT_PROGRAM_ID- The Drift program addressUSDC_MINT_ADDRESS- The USDC token mint- Market configurations and more
2. Set Up Connection and Wallet
3. Initialize Drift Client
DriftClient is your main interface to the Drift Protocol.
Account Subscription Types
Account Subscription Types
Drift supports multiple subscription types:
- Polling: Regular interval updates (good for bots)
- Websocket: Real-time updates (good for UIs)
- gRPC: High-performance streaming (requires additional setup)
4. Create User Account
5. Place an Order
BASE_PRECISION is 10^9, so new BN(1).mul(BASE_PRECISION) equals 1 SOLOrder Types
Drift supports multiple order types:- Market Order
- Limit Order
- Trigger Order
Execute immediately at the best available price:
Working with Positions
After placing orders, monitor and manage your positions:Closing Positions
To close a position, place an order in the opposite direction:Getting Market Data
Access real-time market information:Best Practices
Always Use BigNum for Amounts
Always Use BigNum for Amounts
Never use regular JavaScript numbers for token amounts:
Handle Errors Gracefully
Handle Errors Gracefully
Wrap operations in try-catch blocks:
Refresh Data Before Trading
Refresh Data Before Trading
Always fetch the latest account data:
Use Reduce-Only for Closing
Use Reduce-Only for Closing
Prevent accidentally opening opposite positions:
Running Your First Trade
What’s Next?
Advanced Trading
Learn about limit orders, stop losses, and advanced strategies
Build a Trading Bot
Explore example bot implementations
API Reference
Complete TypeScript SDK documentation
Risk Management
Learn about margin, liquidations, and risk controls
Common Issues
Insufficient collateral error
Insufficient collateral error
Ensure you have enough USDC deposited:Deposit more if needed:
Transaction simulation failed
Transaction simulation failed
Common causes:
- Insufficient SOL for transaction fees
- Slippage too high on large orders
- Market conditions changed
User account not found
User account not found
The user account doesn’t exist yet. Create it: