polyfill_rs/
lib.rs

1//! Polyfill-rs: High-performance Rust client for Polymarket
2//!
3//! # Features
4//!
5//! - **High-performance order book management** with optimized data structures
6//! - **Real-time market data streaming** with WebSocket support
7//! - **Trade execution simulation** with slippage protection
8//! - **Comprehensive error handling** with specific error types
9//! - **Rate limiting and retry logic** for robust API interactions
10//! - **Ethereum integration** with EIP-712 signing support
11//! - **Benchmarking tools** for performance analysis
12//!
13//! # Quick Start
14//!
15//! ```rust,no_run
16//! use polyfill_rs::{ClobClient, OrderArgs, Side};
17//! use rust_decimal::Decimal;
18//! use std::str::FromStr;
19//!
20//! #[tokio::main]
21//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
22//!     // Create client (compatible with polymarket-rs-client)
23//!     let mut client = ClobClient::with_l1_headers(
24//!         "https://clob.polymarket.com",
25//!         "your_private_key",
26//!         137,
27//!     );
28//!
29//!     // Get API credentials
30//!     let api_creds = client.create_or_derive_api_key(None).await.unwrap();
31//!     client.set_api_creds(api_creds);
32//!
33//!     // Create and post order
34//!     let order_args = OrderArgs::new(
35//!         "token_id",
36//!         Decimal::from_str("0.75").unwrap(),
37//!         Decimal::from_str("100.0").unwrap(),
38//!         Side::BUY,
39//!     );
40//!
41//!     let result = client.create_and_post_order(&order_args).await.unwrap();
42//!     println!("Order posted: {:?}", result);
43//!
44//!     Ok(())
45//! }
46//! ```
47//!
48//! # Advanced Usage
49//!
50//! ```rust,no_run
51//! use polyfill_rs::{ClobClient, OrderBookImpl};
52//! use rust_decimal::Decimal;
53//!
54//! #[tokio::main]
55//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
56//!     // Create a basic client
57//!     let client = ClobClient::new("https://clob.polymarket.com");
58//!
59//!     // Get market data
60//!     let markets = client.get_sampling_markets(None).await.unwrap();
61//!     println!("Found {} markets", markets.data.len());
62//!
63//!     // Create an order book for high-performance operations
64//!     let mut book = OrderBookImpl::new("token_id".to_string(), 100); // 100 levels depth
65//!     println!("Order book created for token: {}", book.token_id);
66//!
67//!     Ok(())
68//! }
69//! ```
70
71use tracing::info;
72
73// Global constants
74pub const DEFAULT_CHAIN_ID: u64 = 137; // Polygon
75pub const DEFAULT_BASE_URL: &str = "https://clob.polymarket.com";
76pub const DEFAULT_TIMEOUT_SECS: u64 = 30;
77pub const DEFAULT_MAX_RETRIES: u32 = 3;
78pub const DEFAULT_RATE_LIMIT_RPS: u32 = 100;
79
80// Initialize logging
81pub fn init() {
82    tracing_subscriber::fmt::init();
83    info!("Polyfill-rs initialized");
84}
85
86// Re-export main types
87pub use crate::types::{
88    ApiCredentials,
89    // Additional compatibility types
90    ApiKeysResponse,
91    AssetType,
92    Balance,
93    BalanceAllowance,
94    BalanceAllowanceParams,
95    BatchMidpointRequest,
96    BatchMidpointResponse,
97    BatchPriceRequest,
98    BatchPriceResponse,
99    BookParams,
100    ClientConfig,
101    ClientResult,
102    FillEvent,
103    Market,
104    MarketSnapshot,
105    MarketsResponse,
106    MidpointResponse,
107    NegRiskResponse,
108    NotificationParams,
109    OpenOrder,
110    OpenOrderParams,
111    Order,
112    OrderBook,
113    OrderBookSummary,
114    OrderDelta,
115    OrderRequest,
116    OrderStatus,
117    OrderSummary,
118    OrderType,
119    PriceResponse,
120    Rewards,
121    Side,
122    SimplifiedMarket,
123    SimplifiedMarketsResponse,
124    SpreadResponse,
125    StreamMessage,
126    TickSizeResponse,
127    Token,
128    TokenPrice,
129    TradeParams,
130    WssAuth,
131    WssChannelType,
132    WssSubscription,
133};
134
135// Re-export client
136pub use crate::client::{ClobClient, PolyfillClient};
137
138// Re-export compatibility types (for easy migration from polymarket-rs-client)
139pub use crate::client::OrderArgs;
140
141// Re-export error types
142pub use crate::errors::{PolyfillError, Result};
143
144// Re-export advanced components
145pub use crate::book::{OrderBook as OrderBookImpl, OrderBookManager};
146pub use crate::decode::Decoder;
147pub use crate::fill::{FillEngine, FillResult};
148pub use crate::stream::{MarketStream, StreamManager, WebSocketStream};
149
150// Re-export utilities
151pub use crate::utils::{crypto, math, rate_limit, retry, time, url};
152
153// Module declarations
154pub mod auth;
155pub mod book;
156pub mod client;
157pub mod decode;
158pub mod errors;
159pub mod fill;
160pub mod http_config;
161pub mod orders;
162pub mod stream;
163pub mod types;
164pub mod utils;
165
166// Benchmarks
167#[cfg(test)]
168mod benches {
169    use crate::{OrderBookManager, OrderDelta, Side};
170    use chrono::Utc;
171    use criterion::{criterion_group, criterion_main};
172    use rust_decimal::Decimal;
173    use std::str::FromStr;
174
175    #[allow(dead_code)]
176    fn order_book_benchmark(c: &mut criterion::Criterion) {
177        let book_manager = OrderBookManager::new(100);
178
179        c.bench_function("apply_order_delta", |b| {
180            b.iter(|| {
181                let delta = OrderDelta {
182                    token_id: "test_token".to_string(),
183                    timestamp: Utc::now(),
184                    side: Side::BUY,
185                    price: Decimal::from_str("0.75").unwrap(),
186                    size: Decimal::from_str("100.0").unwrap(),
187                    sequence: 1,
188                };
189
190                let _ = book_manager.apply_delta(delta);
191            });
192        });
193    }
194
195    criterion_group!(benches, order_book_benchmark);
196    criterion_main!(benches);
197}
198
199#[cfg(test)]
200mod tests {
201    use super::*;
202    use rust_decimal::Decimal;
203    use std::str::FromStr;
204
205    #[test]
206    fn test_client_creation() {
207        let _client = ClobClient::new("https://test.example.com");
208        // Test that the client was created successfully
209        // We can't test private fields, but we can verify the client exists
210        // Client creation successful
211    }
212
213    #[test]
214    fn test_order_args_creation() {
215        let args = OrderArgs::new(
216            "test_token",
217            Decimal::from_str("0.75").unwrap(),
218            Decimal::from_str("100.0").unwrap(),
219            Side::BUY,
220        );
221
222        assert_eq!(args.token_id, "test_token");
223        assert_eq!(args.side, Side::BUY);
224    }
225
226    #[test]
227    fn test_order_args_default() {
228        let args = OrderArgs::default();
229        assert_eq!(args.token_id, "");
230        assert_eq!(args.price, Decimal::ZERO);
231        assert_eq!(args.size, Decimal::ZERO);
232        assert_eq!(args.side, Side::BUY);
233    }
234}