Package Logo
polymarket-pnl
polymarket-pnl@v0.3.1
Total Downloads
6
Published
8 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_exchange_token_registered

9b9dc899f98b15743326ae2898611d3f6e5812d6
map map_ctf_exchange_token_registered (
blocksf.ethereum.type.v2.Block
)  -> contract.v1.TokenRegisteredEvents
substreams gui polymarket-pnl@v0.3.1 map_ctf_exchange_token_registered

map
map_neg_risk_ctf_exchange_token_registered

69e3d64ebb54f71f604835a29e2c45bf7aea419d
map map_neg_risk_ctf_exchange_token_registered (
)  -> contract.v1.NegRiskTokenRegisteredEvents
substreams gui polymarket-pnl@v0.3.1 map_neg_risk_ctf_exchange_token_registered

map
map_fpmm_factory_creation

85d01e4fc0ec8bc7b60b15b807ddfe2a6b297dba
map map_fpmm_factory_creation (
)  -> contract.v1.FpmmFactoryEvents
substreams gui polymarket-pnl@v0.3.1 map_fpmm_factory_creation

map
map_ctf_exchange_order_filled

4cb7f61f0b494fd7c96ea66400e825c6b56cb454
map map_ctf_exchange_order_filled (
)  -> contract.v1.OrderFilledEvents
substreams gui polymarket-pnl@v0.3.1 map_ctf_exchange_order_filled

map
map_neg_risk_ctf_exchange_order_filled

5603ad5bfca16c4a50fc4ee701db1d2abe3c1cb9
map map_neg_risk_ctf_exchange_order_filled (
)  -> contract.v1.NegRiskOrderFilledEvents
substreams gui polymarket-pnl@v0.3.1 map_neg_risk_ctf_exchange_order_filled

map
map_erc1155_transfer_single

2a23964774822eacab327e0c1e7bc08ce0b0e79b
map map_erc1155_transfer_single (
)  -> contract.v1.Erc1155TransferSingleEvents
substreams gui polymarket-pnl@v0.3.1 map_erc1155_transfer_single

map
map_erc1155_transfer_batch

c5e5fecce01f0f919c6e65dadf29752e834a1df5
map map_erc1155_transfer_batch (
)  -> contract.v1.Erc1155TransferBatchEvents
substreams gui polymarket-pnl@v0.3.1 map_erc1155_transfer_batch

map
map_erc20_transfer

9972c7b3b6b8c6dc38ef1f21648a32ca0ea092f5
substreams gui polymarket-pnl@v0.3.1 map_erc20_transfer

map
map_uma_merkle_distributor

a3125cc397f68f5906cacb9e4615328baa2edaab
map map_uma_merkle_distributor (
)  -> contract.v1.MerkleDistributorEvents
substreams gui polymarket-pnl@v0.3.1 map_uma_merkle_distributor

map
map_usdc_merkle_distributor

9db92aba757cdbcffa045e4345868c29b5e8743c
map map_usdc_merkle_distributor (
)  -> contract.v1.MerkleDistributorEvents
substreams gui polymarket-pnl@v0.3.1 map_usdc_merkle_distributor

map
map_ctf_events

f3d46fbfaeb3b9d3a53ca7253070cd6cf559e84b
map map_ctf_events (
)  -> contract.v1.CtfEvents
substreams gui polymarket-pnl@v0.3.1 map_ctf_events

map
map_usdc_events

29f3a39333e017aa8c06ecc0fc30baaba13d125d
map map_usdc_events (
)  -> contract.v1.UsdcEvents
substreams gui polymarket-pnl@v0.3.1 map_usdc_events

map
map_neg_risk_market_analysis

fe213d813c8461be0b5856f4990ad1055d0d4ab0
map map_neg_risk_market_analysis (
)  -> contract.v1.NegRiskMarketAnalysis
substreams gui polymarket-pnl@v0.3.1 map_neg_risk_market_analysis

map
map_enhanced_pnl_with_neg_risk

7a8a4cdb2aa328379c2d67b2d800c740921e6380
map map_enhanced_pnl_with_neg_risk (
)  -> contract.v1.DuneCompatiblePnL
substreams gui polymarket-pnl@v0.3.1 map_enhanced_pnl_with_neg_risk

map
map_risk_management

fa43e418998b3d9a6ec1c88c72371bb039b93e31
map map_risk_management (
)  -> contract.v1.RiskData
substreams gui polymarket-pnl@v0.3.1 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.3.1 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.3.1 ethcommon:index_events
Protobuf

Protobuf Docs Explorer

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