- Add Pyth Network price monitoring (WebSocket + polling fallback) - Add Position Manager with automatic exit logic (TP1/TP2/SL) - Implement dynamic stop-loss adjustment (breakeven + profit lock) - Add real-time P&L tracking and multi-position support - Create comprehensive test suite (3 test scripts) - Add 5 detailed documentation files (2500+ lines) - Update configuration to $50 position size for safe testing - All Phase 2 features complete and tested Core Components: - v4/lib/pyth/price-monitor.ts - Real-time price monitoring - v4/lib/trading/position-manager.ts - Autonomous position management - v4/app/api/trading/positions/route.ts - Query positions endpoint - v4/test-*.ts - Comprehensive testing suite Documentation: - PHASE_2_COMPLETE_REPORT.md - Implementation summary - v4/PHASE_2_SUMMARY.md - Detailed feature overview - v4/TESTING.md - Testing guide - v4/QUICKREF_PHASE2.md - Quick reference - install-phase2.sh - Automated installation script
103 lines
3.5 KiB
TypeScript
103 lines
3.5 KiB
TypeScript
#!/usr/bin/env tsx
|
|
|
|
/**
|
|
* Test Drift v4 Integration
|
|
*
|
|
* Verifies connection to Drift Protocol and basic functionality
|
|
*/
|
|
|
|
import { initializeDriftService, getDriftService } from './lib/drift/client'
|
|
import { getMergedConfig } from './config/trading'
|
|
|
|
async function main() {
|
|
console.log('🧪 Testing Drift v4 Integration\n')
|
|
|
|
try {
|
|
// Test 1: Configuration
|
|
console.log('📋 Test 1: Configuration')
|
|
const config = getMergedConfig()
|
|
console.log('✅ Config loaded:')
|
|
console.log(` Position size: $${config.positionSize}`)
|
|
console.log(` Leverage: ${config.leverage}x`)
|
|
console.log(` Stop loss: ${config.stopLossPercent}%`)
|
|
console.log(` TP1: ${config.takeProfit1Percent}%`)
|
|
console.log(` TP2: ${config.takeProfit2Percent}%`)
|
|
console.log('')
|
|
|
|
// Test 2: Drift Connection
|
|
console.log('📡 Test 2: Drift Connection')
|
|
const drift = await initializeDriftService()
|
|
console.log('✅ Drift service initialized')
|
|
console.log(` Wallet: ${drift.getClient().wallet.publicKey.toString()}`)
|
|
console.log('')
|
|
|
|
// Test 3: Account Balance
|
|
console.log('💰 Test 3: USDC Balance')
|
|
const balance = await drift.getUSDCBalance()
|
|
console.log(`✅ USDC Balance: $${balance.toFixed(2)}`)
|
|
console.log('')
|
|
|
|
// Test 4: Account Health
|
|
console.log('💊 Test 4: Account Health')
|
|
const health = await drift.getAccountHealth()
|
|
console.log('✅ Account health:')
|
|
console.log(` Total collateral: $${health.totalCollateral.toFixed(2)}`)
|
|
console.log(` Total liability: $${health.totalLiability.toFixed(2)}`)
|
|
console.log(` Free collateral: $${health.freeCollateral.toFixed(2)}`)
|
|
console.log(` Margin ratio: ${health.marginRatio === Infinity ? '∞' : health.marginRatio.toFixed(2)}`)
|
|
console.log('')
|
|
|
|
// Test 5: Active Positions
|
|
console.log('📊 Test 5: Active Positions')
|
|
const positions = await drift.getAllPositions()
|
|
console.log(`✅ Active positions: ${positions.length}`)
|
|
|
|
if (positions.length > 0) {
|
|
for (const pos of positions) {
|
|
console.log(` ${pos.symbol}:`)
|
|
console.log(` Side: ${pos.side}`)
|
|
console.log(` Size: ${pos.size.toFixed(4)}`)
|
|
console.log(` Entry: $${pos.entryPrice.toFixed(4)}`)
|
|
console.log(` P&L: $${pos.unrealizedPnL.toFixed(2)}`)
|
|
}
|
|
} else {
|
|
console.log(' No active positions')
|
|
}
|
|
console.log('')
|
|
|
|
// Test 6: Oracle Prices
|
|
console.log('💹 Test 6: Oracle Prices')
|
|
const solPrice = await drift.getOraclePrice(0) // SOL-PERP
|
|
console.log(`✅ SOL/USD: $${solPrice.toFixed(4)}`)
|
|
console.log('')
|
|
|
|
// Test 7: Disconnect
|
|
console.log('🔌 Test 7: Disconnect')
|
|
await drift.disconnect()
|
|
console.log('✅ Disconnected from Drift')
|
|
console.log('')
|
|
|
|
console.log('✅ All tests passed!')
|
|
console.log('')
|
|
console.log('🎯 Ready to trade!')
|
|
console.log('')
|
|
console.log('Next steps:')
|
|
console.log('1. Set up n8n workflow (import n8n-workflow-v4.json)')
|
|
console.log('2. Configure TradingView alerts')
|
|
console.log('3. Test with a manual alert trigger')
|
|
console.log('4. Start trading!')
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ Test failed:', error)
|
|
console.error('\nCommon issues:')
|
|
console.error('- DRIFT_WALLET_PRIVATE_KEY not set or invalid')
|
|
console.error('- Wallet not initialized on Drift')
|
|
console.error('- Insufficient SOL for gas fees')
|
|
console.error('- RPC connection issues')
|
|
console.error('\nCheck v4/SETUP.md for troubleshooting')
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
main()
|