Package Logo
polymarket-pnl
polymarket-pnl@v0.1.0
Total Downloads
6
Published
11 hours ago
Network
polygon polygon
Publisher
User Avatar PaulieB14

Readme

Polymarket P&L Substreams 📊

Real-time Profit & Loss tracking for Polymarket prediction markets on Polygon

Substreams Network License

🎯 Overview

This Substreams package provides comprehensive real-time P&L tracking for Polymarket prediction markets, monitoring all core contracts and calculating user positions, profits, and losses as they happen.

🏗️ Architecture

Core Contracts Tracked

  1. Conditional Tokens Framework (CTF) - 0x4D97DCd97eC945f40cF65F87097ACe5EA0476045

    • Position splits, merges, and redemptions
    • Condition preparation and resolution
    • Token transfers
  2. CTF Exchange (Orderbook) - 0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e

    • Order fills and matches
    • Trading fees
    • Order cancellations
  3. USDC Collateral Token - 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174

    • Transfer events
    • Approval events

📊 Modules

1. map_ctf_events

Tracks all Conditional Tokens Framework events:

  • ConditionPreparation - New market conditions
  • ConditionResolution - Market outcomes and payouts
  • PositionSplit - Position splitting events
  • PositionMerge - Position merging events
  • PositionRedeem - Position redemption events
  • TransferSingle & TransferBatch - Token transfers

2. map_ctf_exchange_events

Tracks CTF Exchange orderbook events:

  • OrderFilled - Completed trades
  • OrdersMatched - Order matching
  • OrderCancelled - Cancelled orders
  • FeeCharged - Trading fees
  • TokenRegistered - New token registrations

3. map_usdc_events

Tracks USDC collateral token events:

  • Transfer - USDC transfers
  • Approval - USDC approvals

4. map_user_positions

Real-time user position tracking:

  • Current token holdings per user
  • Buy/sell transaction history
  • Average prices and realized P&L
  • Position updates and changes

5. map_pnl_data

Comprehensive P&L calculations:

  • User-level P&L metrics
  • Market-level P&L data
  • Global platform statistics
  • Volume and trade counts

🚀 Quick Start

Prerequisites

Installation

# Clone the repository
git clone https://github.com/PaulieB14/Polymarket-PnL-Substreams.git
cd Polymarket-PnL-Substreams

# Build the package
substreams build

# Authenticate
export SUBSTREAMS_API_TOKEN="your_jwt_token_here"

# Run live streaming
substreams gui

Usage Examples

Track CTF Events

substreams run substreams.yaml map_ctf_events --start-block 4023686

Monitor User Positions

substreams run substreams.yaml map_user_positions --start-block 4023686

Get P&L Data

substreams run substreams.yaml map_pnl_data --start-block 4023686

📈 Data Schema

User Position

message UserPosition {
    string user_address = 1;
    string token_id = 2;
    string condition_id = 3;
    string outcome_index = 4;
    string amount_held = 5;
    string average_price = 6;
    string total_bought = 7;
    string total_sold = 8;
    string realized_pnl = 9;
    string unrealized_pnl = 10;
    google.protobuf.Timestamp first_seen = 11;
    google.protobuf.Timestamp last_updated = 12;
}

User P&L

message UserPnL {
    string user_address = 1;
    string total_realized_pnl = 2;
    string total_unrealized_pnl = 3;
    string total_volume = 4;
    string total_trades = 5;
    string winning_trades = 6;
    string losing_trades = 7;
    string win_rate = 8;
    google.protobuf.Timestamp last_activity = 9;
}

Market P&L

message MarketPnL {
    string condition_id = 1;
    string question_id = 2;
    string total_volume = 3;
    string total_trades = 4;
    string total_fees = 5;
    string winning_outcome = 6;
    string resolution_price = 7;
    google.protobuf.Timestamp created_at = 8;
    google.protobuf.Timestamp resolved_at = 9;
}

🔧 Configuration

Network

  • Chain: Polygon
  • Start Block: 4023686 (CTF contract deployment)
  • Exchange Start Block: 33605403 (CTF Exchange deployment)

Performance

  • Parallel Processing: Yes
  • Block Filtering: Optimized for specific contract addresses
  • Memory Efficient: Streaming data processing

📊 Use Cases

1. Real-time P&L Dashboard

Build live dashboards showing user profits/losses as they trade.

2. Risk Management

Monitor user positions and exposure in real-time.

3. Market Analysis

Track market volumes, fees, and trading patterns.

4. Portfolio Tracking

Individual user portfolio performance and history.

5. Compliance & Reporting

Automated P&L reporting for tax and regulatory purposes.

🛠️ Development

Building

substreams build

Testing

# Test with live data
substreams run substreams.yaml map_pnl_data --start-block 4023686

# Test specific module
substreams run substreams.yaml map_user_positions --start-block 4023686

Publishing

# Login to registry
substreams registry login

# Publish package
substreams registry publish

📚 Related Projects

🤝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🆘 Support

🎉 Acknowledgments


Built with ❤️ for the Polymarket community

Documentation

Modules

Maps icon
Maps

map
map_ctf_events

b2dcb1eefb2ad3885a3e0f8857dca4a9b154271d
map map_ctf_events (
blocksf.ethereum.type.v2.Block
)  -> contract.v1.CtfEvents
substreams gui polymarket-pnl@v0.1.0 map_ctf_events

map
map_ctf_exchange_events

8d6be9ffe725981f80709a8770a68b6b51f881b4
map map_ctf_exchange_events (
)  -> contract.v1.CtfExchangeEvents
substreams gui polymarket-pnl@v0.1.0 map_ctf_exchange_events

map
map_usdc_events

2a4057bf0716eb84dd277dc51d85737c11f1673c
map map_usdc_events (
)  -> contract.v1.UsdcEvents
substreams gui polymarket-pnl@v0.1.0 map_usdc_events

map
map_user_positions

ea3f41b7a56d8eb8bb30888294628dcbe2d2d793
map map_user_positions (
)  -> contract.v1.UserPositions
substreams gui polymarket-pnl@v0.1.0 map_user_positions

map
map_pnl_data

d2a36da3ce0ce9432bd05205efc8b20925be5baf
map map_pnl_data (
)  -> contract.v1.PnLData
substreams gui polymarket-pnl@v0.1.0 map_pnl_data

map
map_price_data

ddc893fe1ac7693f89714116f34f75524700e6af
map map_price_data (
)  -> contract.v1.PriceData
substreams gui polymarket-pnl@v0.1.0 map_price_data

map
map_usdc_positions

d23177af1a8bccaebf056bff95e39e0af05a90a1
map map_usdc_positions (
)  -> contract.v1.UsdcPosition
substreams gui polymarket-pnl@v0.1.0 map_usdc_positions

map
map_market_metadata

887ea81b60343920ebcc903c90d1e3616b140d8e
map map_market_metadata (
)  -> contract.v1.MarketMetadata
substreams gui polymarket-pnl@v0.1.0 map_market_metadata

map
map_enhanced_pnl

508fc1aae0b955306bbe1674eb0258e3a8f0e185
map map_enhanced_pnl (
)  -> contract.v1.PnLData
substreams gui polymarket-pnl@v0.1.0 map_enhanced_pnl

map
ethcommon:all_events

963652a247fd23d0823dde62d21ae54c783b6073
map ethcommon:all_events (
)  -> sf.substreams.ethereum.v1.Events

all_events gives you all the events in a block (from successful transactions), with basic block hash/number/timestamp and transaction hash

substreams gui polymarket-pnl@v0.1.0 ethcommon:all_events
Block Indexes icon
Block Indexes

blockIndex
ethcommon:index_events

87255243f80f5d4755cd826ec57bf70696a4d7b6

index_events sets the following keys on the block:

  • Event signatures evt_sig:0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (signature in hex, prefixed by 0x)
  • Event address evt_addr:0x0123456789abcdef0123456789abcdef01234567 (address in hex, prefixed by 0x)
substreams gui polymarket-pnl@v0.1.0 ethcommon:index_events
Protobuf

Protobuf Docs Explorer

contract.v1
sf.ethereum.type.v2
sf.ethereum.substreams.v1