Skip to main content

polymarket_client/
lib.rs

1//! Unified Polymarket Rust SDK for prediction markets on Polygon.
2//!
3//! # Clients
4//!
5//! - [`PublicClient`] — read-only discovery, market data, and (with `account`) portfolio reads
6//! - [`SecureClient`] — authenticated trading, notifications, rewards, CTF wallet ops, and user websockets
7//!
8//! # Feature flags
9//!
10//! | Feature | Enables |
11//! |---------|---------|
12//! | *(default)* | HTTP discovery + CLOB market data |
13//! | `account` | Data API reads on [`PublicClient`] |
14//! | `websockets` | Realtime `subscribe()` on [`PublicClient`] |
15//! | `secure` | `account` + `websockets` + [`SecureClient`] trading and wallet ops |
16//!
17//! # Quickstart
18//!
19//! ```no_run
20//! use polymarket_client::{Environment, PublicClient};
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//!     let client = PublicClient::new(Environment::production());
25//!     let mut paginator = client.list_markets(polymarket_client::ListMarketsRequest {
26//!         closed: Some(false),
27//!         page_size: Some(5),
28//!         ..Default::default()
29//!     })?;
30//!     let page = paginator.first_page().await?;
31//!     for market in &page.items {
32//!         println!("{}: {}", market.id, market.question.as_deref().unwrap_or(""));
33//!     }
34//!     Ok(())
35//! }
36//! ```
37//!
38//! # Realtime (websockets)
39//!
40//! ```no_run
41//! # #[cfg(feature = "websockets")]
42//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
43//! use futures::StreamExt as _;
44//! use polymarket_client::{Environment, MarketSubscription, PublicClient, SubscriptionSpec};
45//!
46//! let client = PublicClient::new(Environment::production());
47//! let mut handle = client.subscribe(vec![SubscriptionSpec::Market(MarketSubscription {
48//!     token_ids: vec!["123".into()],
49//!     custom_feature_enabled: false,
50//! })])?;
51//! if let Some(Ok(event)) = handle.next().await {
52//!     println!("{event:?}");
53//! }
54//! handle.close();
55//! # Ok(())
56//! # }
57//! ```
58
59#![deny(unsafe_code)]
60
61mod environment;
62mod error;
63mod http;
64mod pagination;
65mod params;
66mod public_client;
67
68#[cfg(feature = "account")]
69pub(crate) mod account;
70#[cfg(feature = "account")]
71pub(crate) mod account_client;
72
73#[cfg(feature = "secure")]
74mod secure;
75
76#[cfg(feature = "websockets")]
77mod subscriptions;
78
79pub use environment::Environment;
80pub use error::{
81    unexpected_response, user_input, Error, FetchMarketError, FetchMidpointError,
82    FetchOrderBookError, ListEventsError, ListMarketsError, RateLimitError, RequestRejectedError,
83    TransportError, UnexpectedResponseError, UserInputError,
84};
85pub use pagination::{Page, Paginator};
86pub use polymarket_bindings::clob::{OrderBook, OrderBookLevel};
87pub use polymarket_bindings::gamma::{Event, Market};
88pub use polymarket_bindings::{OrderSide, OrderType};
89pub use polymarket_types::{
90    CtfConditionId, DecimalString, EventId, EvmAddress, MarketId, PaginationCursor, TokenId,
91};
92pub use public_client::{
93    FetchMarketRequest, FetchMidpointRequest, FetchOrderBookRequest, ListEventsRequest,
94    ListMarketsRequest, PublicClient, PublicClientBuilder,
95};
96
97#[cfg(feature = "account")]
98pub use account::{
99    Activity, FetchPortfolioValueError, FetchPortfolioValueRequest, ListActivityError,
100    ListActivityRequest, ListPositionsError, ListPositionsRequest, PortfolioValue, Position,
101};
102
103#[cfg(feature = "account")]
104pub use account_client::{ListActivityPaginator, ListPositionsPaginator};
105
106#[cfg(feature = "secure")]
107pub use polymarket_client_sdk_v2::PRIVATE_KEY_VAR;
108
109#[cfg(feature = "secure")]
110pub use secure::{
111    AccountTrade, ApiCredentials, BuildSecureClientError, CancelMarketOrdersRequest,
112    CancelOrderError, CancelOrderRequest, CancelOrderResponse, CurrentReward,
113    FetchNotificationsError, FetchOrderError, FetchOrderRequest, FetchOrderScoringError,
114    FetchOrderScoringRequest, ListAccountTradesError, ListAccountTradesRequest,
115    ListCurrentRewardsError, ListOpenOrdersError, ListOpenOrdersRequest, MarketOrderType,
116    MergePositionsRequest, Notification, OpenOrder, PlaceLimitOrderRequest,
117    PlaceMarketOrderRequest, PlaceOrderError, PlaceOrderResponse, RedeemPositionsRequest,
118    SecureClient, SecureClientBuilder, SetupTradingApprovalsError, SplitPositionRequest,
119    TransactionOutcome, WalletOperationError,
120};
121
122#[cfg(feature = "websockets")]
123pub use subscriptions::{
124    CommentsSubscription, CryptoPricesSubscription, EquityPricesSubscription, MarketStreamEvent,
125    MarketSubscription, SportsStreamEvent, StreamEvent, SubscribeError, SubscriptionHandle,
126    SubscriptionSpec, UserStreamEvent, UserSubscription,
127};