immortal-bnb/FRONTEND_BACKEND_INTEGRATION.md
2025-11-08 03:43:07 +03:00

154 lines
4.9 KiB
Markdown

# ๐Ÿš€ 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
```bash
# Install all dependencies
npm run install:all
# Or install separately
npm install # Backend dependencies
cd frontend && npm install # Frontend dependencies
```
### 2. Configure Environment
```bash
# 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 trading
- `OPENROUTER_API_KEY` - API key for AI decision making
### 3. Start Development Servers
```bash
# 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 configuration
- `GET /api/wallet/balance` - Wallet balance information
- `GET /api/trades` - Trading history
- `GET /api/stats` - Performance statistics
- `POST /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):
1. Start frontend: `npm run dev:frontend`
2. Dashboard shows demo mode with setup instructions
3. All components work with mock data
### With Configuration (Live Mode):
1. Configure `.env` file with your keys
2. Start both: `npm run dev:full`
3. Dashboard connects to live backend
4. 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
1. **Frontend loads** โ†’ Checks backend availability
2. **Backend available** โ†’ Fetches real data
3. **Backend unavailable** โ†’ Shows demo mode with setup guide
4. **User configures** โ†’ Backend becomes available
5. **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 `.env` file 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
1. **Configure environment variables**
2. **Test with small amounts on testnet**
3. **Verify all integrations work**
4. **Scale up for production use**
The integration is ready to use - just add your configuration! ๐Ÿš€