154 lines
4.9 KiB
Markdown
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! ๐
|