Output
contract.v1.CtfEventsPackage ref
polymarket-pnl@v0.2.0Run package
CLI
Run map_ctf_events from the command line.
substreams run polymarket-pnl@v0.2.0 map_ctf_events -e polygon --start-block -1substreams auth or directly on thegraph.market (see docs).README
Polymarket P&L Substreams 📊
Real-time Profit & Loss tracking for Polymarket prediction markets on Polygon
🎯 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
-
Conditional Tokens Framework (CTF) -
0x4D97DCd97eC945f40cF65F87097ACe5EA0476045- Position splits, merges, and redemptions
- Condition preparation and resolution
- Token transfers
-
CTF Exchange (Orderbook) -
0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e- Order fills and matches
- Trading fees
- Order cancellations
-
USDC Collateral Token -
0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174- Transfer events
- Approval events
📊 Modules
1. map_ctf_events
Tracks all Conditional Tokens Framework events:
ConditionPreparation- New market conditionsConditionResolution- Market outcomes and payoutsPositionSplit- Position splitting eventsPositionMerge- Position merging eventsPositionRedeem- Position redemption eventsTransferSingle&TransferBatch- Token transfers
2. map_ctf_exchange_events
Tracks CTF Exchange orderbook events:
OrderFilled- Completed tradesOrdersMatched- Order matchingOrderCancelled- Cancelled ordersFeeCharged- Trading feesTokenRegistered- New token registrations
3. map_usdc_events
Tracks USDC collateral token events:
Transfer- USDC transfersApproval- 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
- Substreams CLI
- Authentication token from The Graph Market
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
- Polymarket Trading Substreams - General trading data
- Polymarket P&L Subgraph - Historical P&L data
- Polymarket Documentation - Official Polymarket docs
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🆘 Support
- Documentation: Substreams Docs
- Community: Substreams Discord
- Issues: GitHub Issues
🎉 Acknowledgments
- StreamingFast for Substreams technology
- Polymarket for the prediction market platform
- The Graph for indexing infrastructure
Built with ❤️ for the Polymarket community
Modules
Execution graph
Output
contract.v1.PnLDataOutput
contract.v1.PnLDataOutput
contract.v1.PriceDataOutput
contract.v1.RiskDataOutput
contract.v1.UsdcEventsOutput
contract.v1.UsdcPositionShow 2 dependencies
Output
sf.substreams.ethereum.v1.Events
`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)
Output
sf.substreams.index.v1.Keys
Inputs

