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

Readme

Polymarket Substreams

A high-performance Substreams package for extracting and processing events from Polymarket contracts on Polygon. This package provides real-time data streaming for prediction market activities, trading events, and market maker operations.

Overview

Polymarket is a decentralized prediction market platform built on Ethereum and Polygon. This Substreams package indexes key events from Polymarket's smart contracts to provide:

  • Trading Events: Order fills, matches, and cancellations
  • Market Data: Condition preparation and resolution
  • Liquidity Events: Market maker funding and removal
  • User Activity: Trading patterns and volume metrics

Contracts Indexed

CTF Exchange Contract

  • Address: 0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e
  • Events: OrderFilled, OrdersMatched, OrderCancelled, TokenRegistered

Conditional Tokens Framework

  • Address: 0x4D97DCd97eC945f40cF65F87097ACe5EA0476045
  • Events: ConditionPreparation, ConditionResolution, PositionSplit, PositionMerge, PositionRedeem

USDC Collateral Token

  • Address: 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174
  • Events: Transfer, Approval

Modules

map_ctf_exchange_events

Extracts and processes events from the CTF Exchange contract, including:

  • Order fills with maker/taker information
  • Order matches with asset details
  • Fee charging events
  • Token registration events

map_ctf_events

Processes Conditional Tokens Framework events:

  • Condition preparation and resolution
  • Position splits, merges, and redemptions
  • Oracle and question ID tracking

map_usdc_events

Tracks USDC token transfers and approvals for collateral management.

map_trading_data

Combines data from all modules to create enriched trading information:

  • Trade events with calculated metrics
  • Market data aggregation
  • User activity tracking
  • Volume and fee calculations

Quick Start

Prerequisites

  1. Install the Substreams CLI
  2. Get authentication token from The Graph Market

Installation

  1. Clone this repository:
git clone https://github.com/Polymarket/polymarket-substreams.git
cd polymarket-substreams
  1. Build the Substreams:
substreams build
  1. Authenticate with The Graph:
substreams auth
  1. Run the Substreams:
# Run CTF Exchange events
substreams gui substreams.yaml map_ctf_exchange_events

# Run trading data aggregation
substreams gui substreams.yaml map_trading_data

Usage Examples

Extract Order Fills

substreams run -e polygon.streamingfast.io:443 substreams.yaml map_ctf_exchange_events --start-block 50000000 --stop-block 50000100

Monitor Trading Activity

substreams gui substreams.yaml map_trading_data --start-block -1000

Data Schema

Trade Event

message Trade {
    string transaction_hash = 1;
    uint32 log_index = 2;
    google.protobuf.Timestamp timestamp = 3;
    uint64 block_number = 4;
    string trader = 5;
    string market_id = 6;
    string outcome_token = 7;
    string amount = 8;
    string price = 9;
    string fee = 10;
    string trade_type = 11; // "buy" or "sell"
}

Market Event

message Market {
    string market_id = 1;
    string condition_id = 2;
    string question_id = 3;
    string oracle = 4;
    uint32 outcome_slot_count = 5;
    google.protobuf.Timestamp creation_timestamp = 6;
    string status = 7; // "open", "closed", "resolved"
}

Performance Benefits

This Substreams implementation provides significant performance improvements over traditional subgraphs:

  • 100x Faster Syncing: Parallel processing of blockchain data
  • Real-time Streaming: Continuous data flow instead of batch processing
  • Efficient Resource Usage: Optimized memory and CPU utilization
  • Scalable Architecture: Handles high-volume trading activity

Development

Adding New Contracts

To add support for additional Polymarket contracts:

  1. Add contract address to src/lib.rs
  2. Update substreams.yaml with new module
  3. Add corresponding protobuf messages
  4. Implement event extraction logic

Custom Transformations

The map_trading_data module can be extended to include:

  • Price calculations
  • Volume aggregations
  • User behavior analysis
  • Market sentiment metrics

Publishing

To publish this Substreams package:

  1. Package the Substreams:
substreams pack
  1. Publish to the registry:
substreams registry login
substreams registry publish polymarket-v0.1.0.spkg

Contributing

Contributions are welcome! Please see our Contributing Guidelines for details.

License

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

Support

Acknowledgments

Documentation

Modules

Maps icon
Maps

map
map_ctf_exchange_events

9e78bae9a98d4defd10115bcd3545bd4e4eb58cf
map map_ctf_exchange_events (
blocksf.ethereum.type.v2.Block
)  -> contract.v1.CtfExchangeEvents
substreams gui polymarket@v0.1.0 map_ctf_exchange_events

map
map_ctf_events

70a2f8866673467ee2ca3380f54684c21037df5d
map map_ctf_events (
)  -> contract.v1.CtfEvents
substreams gui polymarket@v0.1.0 map_ctf_events

map
map_usdc_events

472f8c45323801e3729ac5fa238dbec103ae34ac
map map_usdc_events (
)  -> contract.v1.UsdcEvents
substreams gui polymarket@v0.1.0 map_usdc_events

map
map_trading_data

e0edc71393e40a286a83ff5c91318ecb45e25c56
map map_trading_data (
)  -> contract.v1.TradingData
substreams gui polymarket@v0.1.0 map_trading_data

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@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@v0.1.0 ethcommon:index_events
Protobuf

Protobuf Docs Explorer

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