Files
trading_bot_v3/DRIFT_INTEGRATION.md
mindesbunister e985a9ec6f 🚀 Fix Drift Protocol integration - Connection now working
 Key fixes:
- Bypass problematic SDK subscription that caused 410 Gone errors
- Use direct account verification without subscription
- Add fallback modes for better reliability
- Switch to Helius RPC endpoint for better rate limits
- Implement proper error handling and retry logic

🔧 Technical changes:
- Enhanced drift-trading.ts with no-subscription approach
- Added Drift API endpoints (/api/drift/login, /balance, /positions)
- Created DriftAccountStatus and DriftTradingPanel components
- Updated Dashboard.tsx to show Drift account status
- Added comprehensive test scripts for debugging

📊 Results:
- Connection Status: Connected 
- Account verification: Working 
- Balance retrieval: Working  (21.94 total collateral)
- Private key authentication: Working 
- User account: 3dG7wayp7b9NBMo92D2qL2sy1curSC4TTmskFpaGDrtA

🌐 RPC improvements:
- Using Helius RPC for better reliability
- Added fallback RPC options in .env
- Eliminated rate limiting issues
2025-07-13 00:20:01 +02:00

3.9 KiB

Drift Trading Integration

This document explains how to use the Drift trading functionality in the Trading Bot v3.

Overview

The application now includes integration with Drift Protocol, a decentralized perpetual futures exchange on Solana. This allows you to:

  • Connect to your Drift account using your Solana private key
  • View real-time account balance and collateral information
  • Monitor open positions with live P&L calculations
  • Execute trades directly through the interface

Setup

1. Environment Variables

Make sure you have the following environment variables configured in your .env file:

# Solana/Drift Trading
SOLANA_RPC_URL=https://api.mainnet-beta.solana.com
SOLANA_PRIVATE_KEY=[your_solana_private_key_array]

The SOLANA_PRIVATE_KEY should be your Solana wallet private key as a JSON array of numbers (not base58 string).

2. Drift Account Initialization

Before you can trade, you need to have a Drift account initialized. If you don't have one:

  1. Visit https://app.drift.trade
  2. Connect your Solana wallet
  3. Initialize your account by depositing some USDC collateral

Features

Account Status Panel

The Drift Account Status panel shows:

  • Connection Status: Whether your wallet is connected to Drift
  • Wallet Address: Your Solana public key (truncated for security)
  • User Account: Whether your Drift account is initialized
  • Account Balance: Real-time collateral and margin information
  • Open Positions: All active positions with live P&L

Trading Panel

The Drift Trading panel allows you to:

  • Select Symbol: Choose from 20+ available markets (SOL, BTC, ETH, etc.)
  • Choose Side: Buy (LONG) or Sell (SHORT)
  • Order Type: Market or Limit orders
  • Set Amount: Trade size in USD
  • Set Price: For limit orders only

Supported Markets

The following markets are currently supported:

  • SOLUSD, BTCUSD, ETHUSD
  • DOTUSD, AVAXUSD, ADAUSD
  • MATICUSD, LINKUSD, ATOMUSD
  • NEARUSD, APTUSD, ORBSUSD
  • RNDUSD, WIFUSD, JUPUSD
  • TNSUSD, DOGEUSD, PEPE1KUSD
  • POPCATUSD, BOMERUSD

API Endpoints

Login/Authentication

POST /api/drift/login

Authenticates your wallet and checks Drift account status.

Account Balance

GET /api/drift/balance

Returns account collateral, margin, and leverage information.

Positions

GET /api/drift/positions

Returns all open positions with real-time P&L.

Execute Trade

POST /api/trading

Executes a trade through Drift Protocol.

Request Body:

{
  "symbol": "SOLUSD",
  "side": "BUY",
  "amount": 100,
  "orderType": "MARKET",
  "price": 45.50
}

Testing

You can test the Drift integration using the provided test script:

./test-drift-trading.js

This will test:

  1. Login/authentication
  2. Account balance retrieval
  3. Position fetching

Troubleshooting

Common Issues

  1. "User account does not exist"

    • You need to initialize your Drift account first
    • Visit app.drift.trade and deposit some USDC
  2. "Failed to login"

    • Check your SOLANA_PRIVATE_KEY is correctly formatted
    • Ensure you have sufficient SOL for transaction fees
    • Verify your RPC endpoint is working
  3. "Insufficient collateral"

    • Deposit more USDC to your Drift account
    • Reduce your trade size

Network Issues

If you experience connection issues:

  • Try switching to a different Solana RPC endpoint
  • Check if Drift Protocol is experiencing downtime
  • Ensure your internet connection is stable

Security Notes

  • Never share your private key
  • The private key is stored securely in environment variables
  • All transactions require your wallet signature
  • The interface only shows truncated public keys

Support

For issues with: