'use client' import React, { useState, useEffect } from 'react' // Available timeframes for automation (matching analysis page format) const timeframes = [ { label: '5m', value: '5' }, { label: '15m', value: '15' }, { label: '30m', value: '30' }, { label: '1h', value: '60' }, { label: '2h', value: '120' }, { label: '4h', value: '240' }, { label: '1d', value: 'D' }, ] export default function AutomationPageV2() { const [config, setConfig] = useState({ mode: 'SIMULATION', dexProvider: 'DRIFT', symbol: 'SOLUSD', timeframe: '1h', // Primary timeframe for backwards compatibility selectedTimeframes: ['60'], // Multi-timeframe support tradingAmount: 100, maxLeverage: 5, stopLossPercent: 2, takeProfitPercent: 6, riskPercentage: 2 }) const [status, setStatus] = useState(null) const [balance, setBalance] = useState(null) const [positions, setPositions] = useState([]) const [loading, setLoading] = useState(false) useEffect(() => { fetchStatus() fetchBalance() fetchPositions() const interval = setInterval(() => { fetchStatus() fetchBalance() fetchPositions() }, 30000) return () => clearInterval(interval) }, []) const toggleTimeframe = (timeframe) => { setConfig(prev => ({ ...prev, selectedTimeframes: prev.selectedTimeframes.includes(timeframe) ? prev.selectedTimeframes.filter(tf => tf !== timeframe) : [...prev.selectedTimeframes, timeframe] })) } const fetchStatus = async () => { try { const response = await fetch('/api/automation/status') const data = await response.json() console.log('Status fetched:', data) // Debug log if (data.success) { setStatus(data.status) } } catch (error) { console.error('Failed to fetch status:', error) } } const fetchBalance = async () => { try { const response = await fetch('/api/drift/balance') const data = await response.json() if (data.success) { setBalance(data) } } catch (error) { console.error('Failed to fetch balance:', error) } } const fetchPositions = async () => { try { const response = await fetch('/api/drift/positions') const data = await response.json() if (data.success) { setPositions(data.positions || []) } } catch (error) { console.error('Failed to fetch positions:', error) } } const handleStart = async () => { console.log('Start button clicked') // Debug log setLoading(true) try { // Ensure we have selectedTimeframes before starting if (config.selectedTimeframes.length === 0) { alert('Please select at least one timeframe for analysis') setLoading(false) return } console.log('Starting automation with config:', { ...config, selectedTimeframes: config.selectedTimeframes }) const response = await fetch('/api/automation/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config) }) const data = await response.json() if (data.success) { fetchStatus() } else { alert('Failed to start automation: ' + data.error) } } catch (error) { console.error('Failed to start automation:', error) alert('Failed to start automation') } finally { setLoading(false) } } const handleStop = async () => { console.log('Stop button clicked') // Debug log setLoading(true) try { const response = await fetch('/api/automation/stop', { method: 'POST' }) const data = await response.json() console.log('Stop response:', data) // Debug log if (data.success) { fetchStatus() } else { alert('Failed to stop automation: ' + data.error) } } catch (error) { console.error('Failed to stop automation:', error) alert('Failed to stop automation') } finally { setLoading(false) } } return (
Drift Protocol - Multi-Timeframe Analysis
Available: ${parseFloat(balance.availableBalance).toFixed(2)} • Using {((config.tradingAmount / balance.availableBalance) * 100).toFixed(1)}% of balance
)} {balance && config.maxLeverage > 1 && (With {config.maxLeverage}x leverage: ${(config.tradingAmount * config.maxLeverage).toFixed(2)} position size
)}Quick calculation based on ${parseFloat(balance.availableBalance).toFixed(2)} balance
)}Loading bot status...
)}