Package Logo
polymarket-pnl
polymarket-pnl@v0.2.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

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

map
map_ctf_exchange_events

89fbf8b1750df4410a7d0231450386469f7571c5
map map_ctf_exchange_events (
)  -> contract.v1.CtfExchangeEvents
substreams gui polymarket-pnl@v0.2.0 map_ctf_exchange_events

map
map_usdc_events

ba6b89a6f85ff90d4cb87357856bcad3496eac0d
map map_usdc_events (
)  -> contract.v1.UsdcEvents
substreams gui polymarket-pnl@v0.2.0 map_usdc_events

map
map_user_positions

4673239322e04d6410a9cf4bb31f1b0218efa47c
map map_user_positions (
)  -> contract.v1.UserPositions
substreams gui polymarket-pnl@v0.2.0 map_user_positions

map
map_pnl_data

636db30b0ba99a9ae9713c28d36c571f1f98ce13
map map_pnl_data (
)  -> contract.v1.PnLData
substreams gui polymarket-pnl@v0.2.0 map_pnl_data

map
map_price_data

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

map
map_usdc_positions

8483539358821e06832cba715a58a9f69461891a
map map_usdc_positions (
)  -> contract.v1.UsdcPosition
substreams gui polymarket-pnl@v0.2.0 map_usdc_positions

map
map_market_metadata

27219a175852ae5cb5b5e846809b44c6488849d9
map map_market_metadata (
)  -> contract.v1.MarketMetadata
substreams gui polymarket-pnl@v0.2.0 map_market_metadata

map
map_enhanced_pnl

146c539474001d9fea62696d29f55084a0faae69
map map_enhanced_pnl (
)  -> contract.v1.PnLData
substreams gui polymarket-pnl@v0.2.0 map_enhanced_pnl

map
map_neg_risk_events

a0f1f6f8aac577526a59ac1c6c31ff04f2614dc7
map map_neg_risk_events (
)  -> contract.v1.NegRiskEvents
substreams gui polymarket-pnl@v0.2.0 map_neg_risk_events

map
map_neg_risk_adapter_events

e876abb97a6804bad3381ca3b9a35563ec31e673
map map_neg_risk_adapter_events (
)  -> contract.v1.NegRiskEvents
substreams gui polymarket-pnl@v0.2.0 map_neg_risk_adapter_events

map
map_neg_risk_collateral_events

fec55bc0494cad00c1f26ba07548075eb6ec0b15
map map_neg_risk_collateral_events (
)  -> contract.v1.NegRiskEvents
substreams gui polymarket-pnl@v0.2.0 map_neg_risk_collateral_events

map
map_risk_management

b169a3e8d6cfbffb940eef73f0794f5713147a6e
map map_risk_management (
)  -> contract.v1.RiskData
substreams gui polymarket-pnl@v0.2.0 map_risk_management

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.2.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.2.0 ethcommon:index_events
Protobuf

Protobuf Docs Explorer

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