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

```bash
# 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](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:**
```json
{
  "symbol": "SOLUSD",
  "side": "BUY",
  "amount": 100,
  "orderType": "MARKET",
  "price": 45.50
}
```

## Testing

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

```bash
./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:
- **Drift Protocol**: Visit [https://docs.drift.trade](https://docs.drift.trade)
- **This Integration**: Check the console logs and error messages
- **Solana Network**: Check [https://status.solana.com](https://status.solana.com)
