tommyreid622

Polymarket Copy Trading Bot

Community tommyreid622
Updated

Polymarket trading bot: Polymarket copytrading bot, Polymarket arbitrage bot on Polymarket, Monitor real price on Polymarket and calculate prob and automatically mirror positions with intelligent sizing and safety checks on Polymarket.(copytrading bot & arbitrage bot))

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)
  • 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

  1. Clone the repository

    git clone <repository-url>
    cd polymarket-copytrading
    
  2. Install dependencies

    bun install
    
  3. Configure environment variables

    cp .env.example .env
    

    Edit .env with 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.com
    
  4. Initialize credentials

    bun src/index.ts
    

    The 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 cancelled
    • FOK (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:

  1. Initialize WebSocket connection to Polymarket
  2. Subscribe to trade activity feed
  3. Monitor target wallet for trades
  4. Automatically copy trades when detected
  5. 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

  1. Trade Detection: WebSocket receives trade activity message
  2. Wallet Filtering: Validates trade originates from target wallet
  3. 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)
  4. Balance Validation: Checks sufficient USDC/token balance
  5. Allowance Management: Ensures proper token approvals
  6. Order Execution: Submits order to CLOB API
  7. Holdings Update: Records token positions locally
  8. 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:

  1. Resolution Check: Queries Polymarket API for market status
  2. Winning Detection: Identifies winning outcome tokens
  3. Balance Verification: Confirms user holds winning tokens
  4. Redemption Execution: Calls Polymarket redemption contract
  5. 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 messages
  • success: Successful operations
  • warning: Non-critical issues
  • error: Errors requiring attention

โš ๏ธ Risk Considerations

  1. Market Risk: Copy trading amplifies both gains and losses
  2. Liquidity Risk: Large orders may not fill completely
  3. Slippage: Market orders execute at current market price
  4. Gas Costs: Each transaction incurs Polygon gas fees
  5. API Limits: Rate limiting may affect order execution
  6. 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.

MCP Server ยท Populars

MCP Server ยท New