Polymarket Copy Trading Bot
A sophisticated, production-ready copy trading bot for Polymarket that automatically mirrors trades from target wallets in real-time. Built with TypeScript, leveraging WebSocket connections for low-latency trade execution and integrated with Polymarket's CLOB (Central Limit Order Book) API.
๐ฏ Overview
This bot monitors specified wallet addresses on Polymarket and automatically replicates their trading activity with configurable parameters. It provides real-time trade copying, automatic position redemption, risk management, and comprehensive logging for production deployment.
Key Capabilities
- Real-time Trade Mirroring: Monitors target wallets via WebSocket and executes trades within milliseconds
- Automated Redemption: Automatically redeems winning positions from resolved markets
- Risk Management: Configurable size multipliers, maximum order amounts, and negative risk protection
- Order Type Flexibility: Supports FAK (Fill-or-Kill) and FOK (Fill-or-Kill) order types
- Holdings Tracking: Maintains local database of token holdings for efficient redemption
- Multi-market Support: Handles binary and multi-outcome markets seamlessly
๐๏ธ Architecture
Technology Stack
- Runtime: Bun (TypeScript-first runtime)
- Language: TypeScript 5.9+
- Blockchain: Polygon (Ethereum-compatible L2)
- APIs:
- Polymarket CLOB Client (
@polymarket/clob-client) - Polymarket Real-Time Data Client (
@polymarket/real-time-data-client)
- Polymarket CLOB Client (
- Web3: Ethers.js v6 for blockchain interactions
- Logging: Custom logger with structured output
System Architecture
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Real-Time Data Client โ
โ (WebSocket Connection to Polymarket) โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Trade Monitor โ
โ - Filters trades by target wallet address โ
โ - Validates trade payloads โ
โ - Triggers copy trade execution โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Trade Order Builder โ
โ - Converts trade payloads to market orders โ
โ - Applies size multipliers and risk limits โ
โ - Handles order type conversion (FAK/FOK) โ
โ - Manages tick size precision โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ CLOB Client โ
โ - Executes orders on Polymarket โ
โ - Manages allowances and approvals โ
โ - Tracks wallet balances โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Holdings Manager โ
โ - Tracks token positions โ
โ - Maintains local JSON database โ
โ - Enables efficient redemption โ
โโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Redemption Engine โ
โ - Monitors market resolution status โ
โ - Automatically redeems winning positions โ
โ - Supports scheduled and on-demand redemption โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
๐ฆ Installation
Prerequisites
- Bun runtime (v1.0+): Install Bun
- Node.js 18+ (for npm package management)
- Polygon wallet with USDC for trading
- Polymarket API credentials (API key and secret)
Setup
Clone the repository
git clone <repository-url> cd polymarket-copytradingInstall dependencies
bun installConfigure environment variables
cp .env.example .envEdit
.envwith your configuration:# Wallet Configuration PRIVATE_KEY=your_private_key_here TARGET_WALLET=0x... # Wallet address to copy trades from # Trading Configuration SIZE_MULTIPLIER=1.0 MAX_ORDER_AMOUNT=100 ORDER_TYPE=FAK TICK_SIZE=0.01 NEG_RISK=false ENABLE_COPY_TRADING=true # Redemption Configuration REDEEM_DURATION=60 # Minutes between auto-redemptions # API Configuration CHAIN_ID=137 # Polygon mainnet CLOB_API_URL=https://clob.polymarket.comInitialize credentials
bun src/index.tsThe bot will automatically create API credentials on first run.
โ๏ธ Configuration
Environment Variables
| Variable | Type | Default | Description |
|---|---|---|---|
PRIVATE_KEY |
string | required | Private key of trading wallet |
TARGET_WALLET |
string | required | Wallet address to copy trades from |
SIZE_MULTIPLIER |
number | 1.0 |
Multiplier for trade sizes (e.g., 2.0 = 2x size) |
MAX_ORDER_AMOUNT |
number | undefined |
Maximum USDC amount per order |
ORDER_TYPE |
string | FAK |
Order type: FAK or FOK |
TICK_SIZE |
string | 0.01 |
Price precision: 0.1, 0.01, 0.001, 0.0001 |
NEG_RISK |
boolean | false |
Enable negative risk (allow negative balances) |
ENABLE_COPY_TRADING |
boolean | true |
Enable/disable copy trading |
REDEEM_DURATION |
number | null |
Minutes between auto-redemptions (null = disabled) |
CHAIN_ID |
number | 137 |
Blockchain chain ID (137 = Polygon) |
CLOB_API_URL |
string | https://clob.polymarket.com |
CLOB API endpoint |
Trading Parameters
- Size Multiplier: Scales the copied trade size.
1.0= exact copy,2.0= double size,0.5= half size - Max Order Amount: Safety limit to prevent oversized positions. Orders exceeding this amount are rejected
- Order Type:
FAK(Fill-and-Kill): Partial fills allowed, remaining unfilled portion cancelledFOK(Fill-or-Kill): Entire order must fill immediately or cancelled
- Tick Size: Price precision for order placement. Must match market's tick size
- Negative Risk: When enabled, allows orders that may result in negative USDC balance
๐ Usage
Starting the Bot
# Start copy trading bot
bun src/index.ts
# Or using npm script
npm start
The bot will:
- Initialize WebSocket connection to Polymarket
- Subscribe to trade activity feed
- Monitor target wallet for trades
- Automatically copy trades when detected
- Run scheduled redemptions (if enabled)
Manual Redemption
Redeem from Holdings File
# Redeem all resolved markets from token-holding.json
bun src/auto-redeem.ts
# Dry run (preview only)
bun src/auto-redeem.ts --dry-run
# Clear holdings after redemption
bun src/auto-redeem.ts --clear-holdings
Redeem from API
# Fetch all markets from API and redeem winning positions
bun src/auto-redeem.ts --api
# Limit number of markets checked
bun src/auto-redeem.ts --api --max 500
Redeem Specific Market
# Check market status
bun src/redeem.ts --check <conditionId>
# Redeem specific market
bun src/redeem.ts <conditionId>
# Redeem with specific index sets
bun src/redeem.ts <conditionId> 1 2
๐ง Technical Details
Trade Execution Flow
- Trade Detection: WebSocket receives trade activity message
- Wallet Filtering: Validates trade originates from target wallet
- Order Construction: Converts trade payload to market order:
- Applies size multiplier
- Validates against max order amount
- Adjusts price to tick size
- Sets order type (FAK/FOK)
- Balance Validation: Checks sufficient USDC/token balance
- Allowance Management: Ensures proper token approvals
- Order Execution: Submits order to CLOB API
- Holdings Update: Records token positions locally
- Logging: Logs all operations with structured output
Redemption Mechanism
The bot maintains a local JSON database (src/data/token-holding.json) tracking all token positions. When markets resolve:
- Resolution Check: Queries Polymarket API for market status
- Winning Detection: Identifies winning outcome tokens
- Balance Verification: Confirms user holds winning tokens
- Redemption Execution: Calls Polymarket redemption contract
- Holdings Cleanup: Removes redeemed positions from database
Security Features
- Credential Management: Secure API key storage in
src/data/credential.json - Allowance Control: Automatic USDC approval management
- Balance Validation: Pre-order balance checks prevent over-trading
- Error Handling: Comprehensive error handling with graceful degradation
- Private Key Security: Uses environment variables (never hardcoded)
Order Builder Logic
The TradeOrderBuilder class handles complex order construction:
class TradeOrderBuilder {
async copyTrade(options: CopyTradeOptions): Promise<CopyTradeResult> {
// 1. Extract trade parameters
// 2. Apply size multiplier
// 3. Validate against max amount
// 4. Convert to market order format
// 5. Handle buy vs sell logic
// 6. Execute order
// 7. Update holdings
}
}
Buy Orders:
- Validates USDC balance
- Checks allowance and approves if needed
- Places market order
- Records token holdings
Sell Orders:
- Validates token holdings
- Checks available balance (accounting for open orders)
- Places market order
- Updates holdings after execution
๐ Project Structure
polymarket-copytrading/
โโโ src/
โ โโโ index.ts # Main bot entry point
โ โโโ auto-redeem.ts # Automated redemption script
โ โโโ redeem.ts # Manual redemption script
โ โโโ data/ # Data storage
โ โ โโโ credential.json # API credentials (auto-generated)
โ โ โโโ token-holding.json # Token holdings database
โ โโโ order-builder/ # Order construction logic
โ โ โโโ builder.ts # TradeOrderBuilder class
โ โ โโโ helpers.ts # Order conversion utilities
โ โ โโโ types.ts # Type definitions
โ โโโ providers/ # API clients
โ โ โโโ clobclient.ts # CLOB API client
โ โ โโโ wssProvider.ts # WebSocket provider
โ โ โโโ rpcProvider.ts # RPC provider
โ โโโ security/ # Security utilities
โ โ โโโ allowance.ts # Token approval management
โ โ โโโ createCredential.ts # Credential generation
โ โโโ utils/ # Utility functions
โ โโโ balance.ts # Balance checking
โ โโโ holdings.ts # Holdings management
โ โโโ logger.ts # Logging utility
โ โโโ redeem.ts # Redemption logic
โ โโโ types.ts # TypeScript types
โโโ package.json
โโโ tsconfig.json
โโโ README.md
๐ API Integration
Polymarket CLOB Client
The bot uses the official @polymarket/clob-client for order execution:
import { ClobClient, OrderType, Side } from "@polymarket/clob-client";
const client = await getClobClient();
const order = await client.createOrder({
token_id: tokenId,
side: Side.BUY,
price: price,
size: size,
order_type: OrderType.FAK,
});
Real-Time Data Client
WebSocket connection for live trade monitoring:
import { RealTimeDataClient } from "@polymarket/real-time-data-client";
client.subscribe({
subscriptions: [{
topic: "activity",
type: "trades"
}]
});
๐ Monitoring & Logging
The bot provides comprehensive logging:
- Trade Detection: Logs all detected trades from target wallet
- Order Execution: Records order placement and results
- Redemption Activity: Tracks redemption operations
- Error Handling: Detailed error messages with stack traces
- Balance Updates: Displays wallet balances after operations
Log levels:
info: General operational messagessuccess: Successful operationswarning: Non-critical issueserror: Errors requiring attention
โ ๏ธ Risk Considerations
- Market Risk: Copy trading amplifies both gains and losses
- Liquidity Risk: Large orders may not fill completely
- Slippage: Market orders execute at current market price
- Gas Costs: Each transaction incurs Polygon gas fees
- API Limits: Rate limiting may affect order execution
- Network Latency: WebSocket delays may cause missed trades
Recommendations:
- Start with small size multipliers
- Set conservative max order amounts
- Monitor wallet balance regularly
- Use dry-run mode for testing
- Test with small amounts before scaling
๐ ๏ธ Development
Building
# Type checking
bun run tsc --noEmit
# Run in development
bun --watch src/index.ts
Testing
# Test redemption (dry run)
bun src/auto-redeem.ts --dry-run
# Test specific market
bun src/redeem.ts --check <conditionId>
๐ License
ISC
๐ค Contributing
Contributions welcome! Please ensure:
- Code follows TypeScript best practices
- All functions are properly typed
- Error handling is comprehensive
- Logging is informative
- Documentation is updated
๐ Support
For issues, questions, or contributions:
- Open an issue on GitHub
- Review existing documentation
- Check Polymarket API documentation
Disclaimer: This software is provided as-is. Trading cryptocurrencies and prediction markets carries significant risk. Use at your own discretion and never trade more than you can afford to lose.