- Created /api/testing/drift-init endpoint for systematic RPC testing - Tested Alchemy: 17-71 subscription errors per init (49 avg over 5 runs) - Tested Helius: 0 subscription errors, 800ms init time - DEFINITIVE PROOF: Alchemy rate limits break Drift SDK initialization - Root cause: Burst subscription pattern hits CUPS limits - SDK doesn't retry failed subscriptions → unstable state - Documented complete findings in docs/ALCHEMY_RPC_INVESTIGATION_RESULTS.md - Investigation CLOSED - Helius is the only reliable solution
165 lines
4.8 KiB
TypeScript
165 lines
4.8 KiB
TypeScript
/**
|
|
* Diagnostic endpoint to test Drift SDK initialization with different RPCs
|
|
*
|
|
* Usage:
|
|
* curl http://localhost:3001/api/testing/drift-init?rpc=alchemy
|
|
* curl http://localhost:3001/api/testing/drift-init?rpc=helius
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js'
|
|
import { DriftClient } from '@drift-labs/sdk'
|
|
import bs58 from 'bs58'
|
|
|
|
const ALCHEMY_RPC = 'https://solana-mainnet.g.alchemy.com/v2/5A0iA5UYpsmP9gkuezYeg'
|
|
const HELIUS_RPC = 'https://mainnet.helius-rpc.com/?api-key=5e236449-f936-4af7-ae38-f15e2f1a3757'
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const searchParams = request.nextUrl.searchParams
|
|
const rpcType = searchParams.get('rpc') || 'helius'
|
|
|
|
const rpcUrl = rpcType === 'alchemy' ? ALCHEMY_RPC : HELIUS_RPC
|
|
|
|
const log: string[] = []
|
|
const logLine = (msg: string) => {
|
|
console.log(msg)
|
|
log.push(msg)
|
|
}
|
|
|
|
logLine(`🔬 Testing Drift SDK initialization with ${rpcType.toUpperCase()}`)
|
|
logLine(`RPC: ${rpcUrl}`)
|
|
|
|
// Parse wallet
|
|
let secretKey: Uint8Array
|
|
const privateKey = process.env.DRIFT_WALLET_PRIVATE_KEY!
|
|
|
|
if (privateKey.startsWith('[')) {
|
|
const keyArray = JSON.parse(privateKey)
|
|
secretKey = new Uint8Array(keyArray)
|
|
} else {
|
|
secretKey = bs58.decode(privateKey)
|
|
}
|
|
|
|
const keypair = Keypair.fromSecretKey(secretKey)
|
|
logLine(`📍 Wallet: ${keypair.publicKey.toString()}`)
|
|
|
|
// Track subscription events
|
|
let subscriptionErrors = 0
|
|
let accountSubscribeErrors: any[] = []
|
|
|
|
// Intercept console.error temporarily
|
|
const originalError = console.error
|
|
console.error = function(...args) {
|
|
const message = args.join(' ')
|
|
|
|
if (message.includes('accountSubscribe')) {
|
|
subscriptionErrors++
|
|
accountSubscribeErrors.push({
|
|
time: new Date().toISOString(),
|
|
message: message.substring(0, 200)
|
|
})
|
|
}
|
|
|
|
originalError.apply(console, args)
|
|
}
|
|
|
|
const startTime = Date.now()
|
|
|
|
try {
|
|
// Create connection
|
|
logLine('🔌 Creating Solana connection...')
|
|
const connection = new Connection(rpcUrl, 'confirmed')
|
|
|
|
// Create Drift client
|
|
logLine('🚀 Initializing Drift SDK...')
|
|
const driftClient = new DriftClient({
|
|
connection,
|
|
wallet: {
|
|
publicKey: keypair.publicKey,
|
|
signTransaction: async (tx) => {
|
|
if (typeof tx.partialSign === 'function') {
|
|
tx.partialSign(keypair)
|
|
}
|
|
return tx
|
|
},
|
|
signAllTransactions: async (txs) => {
|
|
txs.forEach(tx => {
|
|
if (typeof tx.partialSign === 'function') {
|
|
tx.partialSign(keypair)
|
|
}
|
|
})
|
|
return txs
|
|
}
|
|
},
|
|
programID: new PublicKey(process.env.DRIFT_PROGRAM_ID || 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH'),
|
|
env: 'mainnet-beta',
|
|
})
|
|
|
|
logLine('⏳ Subscribing to account updates...')
|
|
await driftClient.subscribe()
|
|
|
|
const initTime = Date.now() - startTime
|
|
logLine(`✅ Drift SDK initialized (${initTime}ms)`)
|
|
|
|
// Restore console.error
|
|
console.error = originalError
|
|
|
|
logLine(`\n📊 Subscription errors: ${subscriptionErrors}`)
|
|
|
|
// Test basic operation
|
|
logLine('\n🏥 Testing account health...')
|
|
const user = driftClient.getUser()
|
|
const equity = user.getTotalCollateral()
|
|
logLine(` Total collateral: $${(equity.toNumber() / 1e6).toFixed(2)}`)
|
|
|
|
logLine('\n🔍 Testing position query...')
|
|
const positions = user.getActivePerpPositions()
|
|
logLine(` Active positions: ${positions.length}`)
|
|
|
|
// Wait and test stability
|
|
logLine('\n⏳ Waiting 5 seconds...')
|
|
await new Promise(resolve => setTimeout(resolve, 5000))
|
|
|
|
logLine('🔍 Testing second position query...')
|
|
const positions2 = user.getActivePerpPositions()
|
|
logLine(` Active positions: ${positions2.length}`)
|
|
|
|
const totalTime = Date.now() - startTime
|
|
logLine(`\n✅ Test complete (${totalTime}ms total)`)
|
|
|
|
// Cleanup
|
|
await driftClient.unsubscribe()
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
rpc: rpcType,
|
|
initTime,
|
|
totalTime,
|
|
subscriptionErrors,
|
|
accountSubscribeErrors: accountSubscribeErrors.slice(0, 5), // First 5 only
|
|
collateral: equity.toNumber() / 1e6,
|
|
activePositions: positions2.length,
|
|
log
|
|
})
|
|
|
|
} catch (error: any) {
|
|
const failTime = Date.now() - startTime
|
|
|
|
// Restore console.error
|
|
console.error = originalError
|
|
|
|
logLine(`\n❌ Test failed (${failTime}ms)`)
|
|
logLine(` Error: ${error.message}`)
|
|
|
|
return NextResponse.json({
|
|
success: false,
|
|
rpc: rpcType,
|
|
failTime,
|
|
error: error.message,
|
|
subscriptionErrors,
|
|
accountSubscribeErrors: accountSubscribeErrors.slice(0, 10),
|
|
log
|
|
}, { status: 500 })
|
|
}
|
|
}
|