4.9 KiB
๐ Frontend-Backend Integration Guide
This guide shows how to connect the frontend dashboard to the backend trading bot.
๐ Current Status
โ
Backend: Fully functional with TypeScript compilation passing
โ
Frontend: Modern Next.js dashboard with proper error handling
โ
Integration: API client with mock data fallbacks for development
โ ๏ธ Configuration: Requires environment setup for full functionality
๐ง Quick Setup
1. Install Dependencies
# Install all dependencies
npm run install:all
# Or install separately
npm install # Backend dependencies
cd frontend && npm install # Frontend dependencies
2. Configure Environment
# Copy example configuration
cp .env.example .env
# Edit .env with your actual values
nano .env # or use your preferred editor
Required Configuration:
WALLET_PRIVATE_KEY- Your wallet private key for tradingOPENROUTER_API_KEY- API key for AI decision making
3. Start Development Servers
# Option 1: Start both frontend and backend together
npm run dev:full
# Option 2: Start separately
npm run dev:backend # Backend API on port 3001
npm run dev:frontend # Frontend on port 3000
4. Access Dashboard
- Frontend: http://localhost:3000
- Backend API: http://localhost:3001
๐ก API Integration
Backend Endpoints Available:
GET /api/status- Bot status and configurationGET /api/wallet/balance- Wallet balance informationGET /api/trades- Trading historyGET /api/stats- Performance statisticsPOST /api/bot/start- Start trading bot (future feature)POST /api/bot/stop- Stop trading bot (future feature)
Frontend Features:
- Real-time Status: Shows bot configuration status
- Wallet Connection: MetaMask integration with network detection
- Smart Fallbacks: Mock data when backend is not configured
- Error Handling: Graceful degradation for missing configurations
๐ Development Workflow
Without Configuration (Demo Mode):
- Start frontend:
npm run dev:frontend - Dashboard shows demo mode with setup instructions
- All components work with mock data
With Configuration (Live Mode):
- Configure
.envfile with your keys - Start both:
npm run dev:full - Dashboard connects to live backend
- Real trading data and wallet integration
๐๏ธ Component Structure
Frontend Components:
frontend/
โโโ app/
โ โโโ layout.tsx # Main app layout
โ โโโ page.tsx # Dashboard homepage
โโโ components/
โ โโโ dashboard/
โ โ โโโ BotStatus.tsx # Bot status and controls โ
โ โ โโโ WalletInfo.tsx # Wallet connection โ
โ โ โโโ SetupGuide.tsx # Configuration guide โ
โ โ โโโ Dashboard.tsx # Main dashboard
โ โ โโโ TradingHistory.tsx
โ โ โโโ PerformanceChart.tsx
โ โโโ layout/
โ โ โโโ Header.tsx
โ โโโ providers/
โ โโโ Web3Provider.tsx # Wallet connection โ
โ โโโ QueryProvider.tsx
โโโ lib/
โโโ api.ts # Backend API client โ
โโโ hooks.ts # React hooks for data โ
Key Features Implemented:
โ
Smart API Client: Automatically handles backend unavailability
โ
Wallet Integration: Full MetaMask support with network switching
โ
Configuration Detection: Shows setup instructions when needed
โ
Responsive Design: Works on desktop and mobile
โ
Error Boundaries: Graceful error handling throughout
๐ Connection Flow
- Frontend loads โ Checks backend availability
- Backend available โ Fetches real data
- Backend unavailable โ Shows demo mode with setup guide
- User configures โ Backend becomes available
- Real-time updates โ Dashboard shows live trading data
๐จ Security Notes
โ ๏ธ Never commit private keys to version control
โ ๏ธ Start with testnet for development
โ ๏ธ Use small amounts for initial testing
โ ๏ธ Validate all transactions before execution
๐ Common Issues
Backend Not Starting:
- Check if all dependencies are installed
- Verify
.envfile exists and has correct format - Check for TypeScript compilation errors:
npm run build
Frontend Not Connecting:
- Ensure backend is running on port 3001
- Check browser console for CORS errors
- Verify API_BASE_URL in frontend configuration
Wallet Not Connecting:
- Install MetaMask or another Web3 wallet
- Switch to correct network (BNB Chain or opBNB)
- Check if wallet has sufficient balance for gas
๐ฏ Next Steps
- Configure environment variables
- Test with small amounts on testnet
- Verify all integrations work
- Scale up for production use
The integration is ready to use - just add your configuration! ๐