Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,18 @@ RETRY_DELAY_MS=2000
# Delay before copying a trade in milliseconds
COPY_DELAY_MS=0

# === TELEGRAM NOTIFICATIONS ===
# Get token from @BotFather on Telegram
# === TELEGRAM BOT ===
# Get token from @BotFather on Telegram (REQUIRED for multi-user mode)
TELEGRAM_BOT_TOKEN=

# Get your chat ID from @userinfobot on Telegram
# Get your chat ID from @userinfobot on Telegram (single-user mode only)
TELEGRAM_CHAT_ID=

# === MULTI-USER ENCRYPTION ===
# Master key for encrypting user private keys (recommended: generate a random 32+ char string)
# If not set, falls back to TELEGRAM_BOT_TOKEN (less secure)
ENCRYPTION_KEY=

# === HEALTH CHECK ENDPOINT ===
# Enable HTTP health check server
HEALTH_CHECK_ENABLED=false
Expand Down
62 changes: 62 additions & 0 deletions PLAN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Polybot Multi-User Telegram Bot - Implementation Plan

## Overview
Transform Polybot from a single-user CLI bot into a multi-user Telegram bot where each user can:
- Connect their Polymarket wallet (private key + wallet address)
- Configure their own trading settings via Telegram buttons
- Follow different trader wallets independently
- Manage risk settings (slippage, caps, stop-loss, take-profit, etc.)

## Architecture Changes

### 1. User Database (`src/db/userDb.ts`)
- SQLite database (via `better-sqlite3`) for persistent multi-user storage
- Tables: `users`, `user_wallets`, `user_settings`, `user_tracked_wallets`, `user_positions`, `user_trades`
- Each user identified by Telegram chat ID
- Encrypted private key storage (AES-256-GCM with master key from env)

### 2. Telegram Bot Rewrite (`src/telegram/`)
- **bot.ts** - Main Telegram bot with callback query routing
- **menus.ts** - Inline keyboard menu builders
- **handlers/** - Command and callback handlers:
- `start.ts` - /start welcome + main menu
- `wallet.ts` - Connect/disconnect/view wallets
- `follow.ts` - Add/remove/list tracked wallets
- `settings.ts` - Trading settings (slippage, caps, sizing, risk)
- `positions.ts` - View positions, manual sell
- `stats.ts` - Trading stats and P&L
- `trading.ts` - Start/stop/pause trading

### 3. Per-User Bot Instances (`src/services/userBotManager.ts`)
- Manages a lightweight bot instance per active user
- Each instance runs its own TradeMonitor, Trader, RiskManager
- Lazy initialization (only when user starts trading)
- Graceful shutdown per user

### 4. Menu Flow
```
/start → Main Menu
├── 🔑 Wallets → Connect Wallet | View Wallets | Disconnect
├── 👁 Follow Traders → Add Wallet | List | Remove
├── ⚙️ Settings
│ ├── Position Sizing → Account Size | Max Position | Min Trade | Max %
│ ├── Risk Management → Stop Loss | Take Profit | Trailing Stop | Daily Loss Limit
│ ├── Trade Filters → Min/Max Probability | Blacklist | Whitelist
│ ├── Execution → Slippage | Copy Sells | Conflict Strategy
│ └── Time Exits → Max Hold Time
├── 📊 Positions → List | Sell Position | Sell All
├── 📈 Stats → Overview | Per-Trader | Reset
├── ▶️ Start Trading / ⏸ Pause / ⏹ Stop
└── ❓ Help
```

## Implementation Steps

1. Add dependencies (better-sqlite3, encryption)
2. Create database schema and UserDb class
3. Build Telegram menu system with inline keyboards
4. Implement wallet connection flow (encrypted storage)
5. Implement settings management via Telegram
6. Create UserBotManager for per-user bot instances
7. Wire everything together
8. Update config for multi-user mode
Loading
Loading