Implement working Drift leverage trading

Key Features:
-  Drift SDK v2.126.0-beta.14 integration with Helius RPC
-  User account initialization and balance reading
-  Leverage trading API with real trades executed
-  Support for SOL, BTC, ETH, APT, AVAX, BNB, MATIC, ARB, DOGE, OP
-  Transaction confirmed: gNmaWVqcE4qNK31ksoUsK6pcHqdDTaUtJXY52ZoXRF

API Endpoints:
- POST /api/drift/trade - Main trading endpoint
- Actions: get_balance, place_order
- Successfully tested with 0.01 SOL buy order at 2x leverage

Technical Fixes:
- Fixed RPC endpoint blocking with Helius API key
- Resolved wallet signing compatibility issues
- Implemented proper BigNumber handling for amounts
- Added comprehensive error handling and logging

Trading Bot Status: 🚀 FULLY OPERATIONAL with leverage trading!
This commit is contained in:
mindesbunister
2025-07-22 12:23:51 +02:00
parent 491ff51ba9
commit fb194f1b12
28 changed files with 7285 additions and 198 deletions

66
test-drift-devnet.js Normal file
View File

@@ -0,0 +1,66 @@
#!/usr/bin/env node
// Test Drift SDK on devnet to avoid RPC restrictions
import { DriftClient, initialize, Wallet } from '@drift-labs/sdk'
import { Connection, Keypair } from '@solana/web3.js'
async function testDriftDevnet() {
try {
console.log('🧪 Testing Drift SDK on devnet...')
// Use devnet which is more permissive
const connection = new Connection('https://api.devnet.solana.com', 'confirmed')
// Create a random keypair for testing
const keypair = Keypair.generate()
const wallet = new Wallet(keypair)
console.log('🔐 Test wallet:', keypair.publicKey.toString())
// Initialize for devnet
const env = 'devnet'
const sdkConfig = initialize({ env })
console.log('📋 Drift Program ID:', sdkConfig.DRIFT_PROGRAM_ID.toString())
const driftClient = new DriftClient({
connection,
wallet,
programID: sdkConfig.DRIFT_PROGRAM_ID,
opts: {
commitment: 'confirmed',
},
})
console.log('✅ DriftClient created successfully')
// Try to get program account info (read-only operation)
const programInfo = await connection.getAccountInfo(sdkConfig.DRIFT_PROGRAM_ID)
console.log('📊 Program account info:', programInfo ? 'Found' : 'Not found')
// Try subscribing (this might fail without funds but should show what works)
try {
await driftClient.subscribe()
console.log('✅ Subscribed to Drift client')
// Try to get state
const state = await driftClient.getStateAccount()
console.log('📈 Drift state:', {
admin: state.admin.toString(),
numberOfAuthorities: state.numberOfAuthorities
})
await driftClient.unsubscribe()
} catch (subscribeError) {
console.log(' Subscribe failed (expected for unfunded account):', subscribeError.message)
}
console.log('✅ Basic Drift SDK functionality working on devnet')
} catch (error) {
console.error('❌ Drift devnet test failed:', error)
}
}
testDriftDevnet()