Real-time Profit & Loss tracking for Polymarket prediction markets on Polygon
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.
Conditional Tokens Framework (CTF) - 0x4D97DCd97eC945f40cF65F87097ACe5EA0476045
CTF Exchange (Orderbook) - 0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e
USDC Collateral Token - 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174
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 transfersmap_ctf_exchange_events
Tracks CTF Exchange orderbook events:
OrderFilled
- Completed tradesOrdersMatched
- Order matchingOrderCancelled
- Cancelled ordersFeeCharged
- Trading feesTokenRegistered
- New token registrationsmap_usdc_events
Tracks USDC collateral token events:
Transfer
- USDC transfersApproval
- USDC approvalsmap_user_positions
Real-time user position tracking:
map_pnl_data
Comprehensive P&L calculations:
# 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
substreams run substreams.yaml map_ctf_events --start-block 4023686
substreams run substreams.yaml map_user_positions --start-block 4023686
substreams run substreams.yaml map_pnl_data --start-block 4023686
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;
}
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;
}
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;
}
Build live dashboards showing user profits/losses as they trade.
Monitor user positions and exposure in real-time.
Track market volumes, fees, and trading patterns.
Individual user portfolio performance and history.
Automated P&L reporting for tax and regulatory purposes.
substreams build
# 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
# Login to registry
substreams registry login
# Publish package
substreams registry publish
This project is licensed under the MIT License - see the LICENSE file for details.
Built with ❤️ for the Polymarket community
substreams gui polymarket-pnl@v0.1.0 map_ctf_events
substreams gui polymarket-pnl@v0.1.0 map_ctf_exchange_events
substreams gui polymarket-pnl@v0.1.0 map_usdc_events
substreams gui polymarket-pnl@v0.1.0 map_user_positions
substreams gui polymarket-pnl@v0.1.0 map_pnl_data
substreams gui polymarket-pnl@v0.1.0 map_price_data
substreams gui polymarket-pnl@v0.1.0 map_usdc_positions
substreams gui polymarket-pnl@v0.1.0 map_market_metadata
substreams gui polymarket-pnl@v0.1.0 map_enhanced_pnl
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
index_events
sets the following keys on the block:
substreams gui polymarket-pnl@v0.1.0 ethcommon:index_events