Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
"your_private_key",
137,
None,
);

// Same API calls
Expand Down
28 changes: 26 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ impl Default for OrderArgs {
}
}

/// HTTP client configuration type
#[derive(Debug, Clone, Copy)]
pub enum ClientType {
Colocated,
Internet,
Optimized,
}

impl Default for ClientType {
fn default() -> Self {
ClientType::Colocated
}
}

/// Main client for interacting with Polymarket API
pub struct ClobClient {
pub http_client: Client,
Expand Down Expand Up @@ -183,14 +197,23 @@ impl ClobClient {
}

/// Create a client with L1 headers (for authentication)
pub fn with_l1_headers(host: &str, private_key: &str, chain_id: u64) -> Self {
pub fn with_l1_headers(
host: &str,
private_key: &str,
chain_id: u64,
client_type: Option<ClientType>,
) -> Self {
let signer = private_key
.parse::<PrivateKeySigner>()
.expect("Invalid private key");

let order_builder = crate::orders::OrderBuilder::new(signer.clone(), None, None);

let http_client = create_optimized_client().unwrap_or_else(|_| Client::new());
let http_client = match client_type.unwrap_or_default() {
ClientType::Colocated => create_colocated_client().unwrap_or_else(|_| Client::new()),
ClientType::Internet => create_internet_client().unwrap_or_else(|_| Client::new()),
ClientType::Optimized => create_optimized_client().unwrap_or_else(|_| Client::new()),
};

// Initialize infrastructure modules
let dns_cache = None; // Skip DNS cache for simplicity in this constructor
Expand Down Expand Up @@ -1749,6 +1772,7 @@ mod tests {
base_url,
"0x1234567890123456789012345678901234567890123456789012345678901234",
137,
None,
)
}

Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//! "https://clob.polymarket.com",
//! "your_private_key",
//! 137,
//! None,
//! );
//!
//! // Get API credentials
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl TestConfig {
let private_key = self.private_key.as_ref()
.ok_or_else(|| polyfill_rs::PolyfillError::auth("No private key provided", polyfill_rs::errors::AuthErrorKind::InvalidCredentials))?;

Ok(ClobClient::with_l1_headers(&self.host, private_key, self.chain_id))
Ok(ClobClient::with_l1_headers(&self.host, private_key, self.chain_id, None))
}

/// Print test configuration (without sensitive data)
Expand Down
18 changes: 9 additions & 9 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn load_env_vars() -> (String, Option<String>, Option<String>, Option<String>) {
async fn test_real_api_create_derive_api_key() {
let (private_key, _, _, _) = load_env_vars();

let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);

// Test creating/deriving API key
let result = client.create_or_derive_api_key(None).await;
Expand All @@ -46,7 +46,7 @@ async fn test_real_api_authenticated_order_flow() {
let (private_key, _, _, _) = load_env_vars();

// Initialize client with L1 headers
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);

// Step 1: Create/derive API credentials
println!("Step 1: Creating/deriving API credentials...");
Expand Down Expand Up @@ -135,7 +135,7 @@ async fn test_real_api_authenticated_order_flow() {
async fn test_real_api_get_orders() {
let (private_key, _, _, _) = load_env_vars();

let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);
let api_creds = client.create_or_derive_api_key(None).await
.expect("Failed to create/derive API key");
client.set_api_creds(api_creds);
Expand Down Expand Up @@ -163,7 +163,7 @@ async fn test_real_api_get_orders() {
async fn test_real_api_get_trades() {
let (private_key, _, _, _) = load_env_vars();

let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);
let api_creds = client.create_or_derive_api_key(None).await
.expect("Failed to create/derive API key");
client.set_api_creds(api_creds);
Expand All @@ -190,7 +190,7 @@ async fn test_real_api_get_trades() {
async fn test_real_api_get_balance_allowance() {
let (private_key, _, _, _) = load_env_vars();

let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);
let api_creds = client.create_or_derive_api_key(None).await
.expect("Failed to create/derive API key");
client.set_api_creds(api_creds);
Expand Down Expand Up @@ -231,7 +231,7 @@ async fn test_real_api_get_balance_allowance() {
async fn test_real_api_get_api_keys() {
let (private_key, _, _, _) = load_env_vars();

let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);
let api_creds = client.create_or_derive_api_key(None).await
.expect("Failed to create/derive API key");
client.set_api_creds(api_creds);
Expand Down Expand Up @@ -259,7 +259,7 @@ async fn test_real_api_get_api_keys() {
async fn test_real_api_get_notifications() {
let (private_key, _, _, _) = load_env_vars();

let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let mut client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);
let api_creds = client.create_or_derive_api_key(None).await
.expect("Failed to create/derive API key");
client.set_api_creds(api_creds);
Expand Down Expand Up @@ -287,7 +287,7 @@ async fn test_real_api_get_notifications() {
async fn test_real_api_market_data_endpoints() {
let (private_key, _, _, _) = load_env_vars();

let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);

println!("Testing market data endpoints (no auth required)...");

Expand Down Expand Up @@ -336,7 +336,7 @@ async fn test_real_api_market_data_endpoints() {
async fn test_real_api_batch_endpoints() {
let (private_key, _, _, _) = load_env_vars();

let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID);
let client = ClobClient::with_l1_headers(HOST, &private_key, CHAIN_ID, None);

println!("Testing batch endpoints...");

Expand Down
3 changes: 2 additions & 1 deletion tests/order_posting_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ async fn test_post_order_authentication() {
let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
&private_key,
137
137,
None
);

println!("Step 1: Creating API credentials...");
Expand Down
12 changes: 8 additions & 4 deletions tests/simple_auth_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ async fn test_create_api_key_simple() {
let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
&private_key,
137
137,
None
);

println!("Step 1: Creating/deriving API key...");
Expand Down Expand Up @@ -60,7 +61,8 @@ async fn test_get_api_keys() {
let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
&private_key,
137
137,
None
);

let creds = client.create_or_derive_api_key(None).await
Expand Down Expand Up @@ -96,7 +98,8 @@ async fn test_get_trades() {
let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
&private_key,
137
137,
None
);

let creds = client.create_or_derive_api_key(None).await
Expand Down Expand Up @@ -132,7 +135,8 @@ async fn test_get_notifications() {
let mut client = ClobClient::with_l1_headers(
"https://clob.polymarket.com",
&private_key,
137
137,
None
);

let creds = client.create_or_derive_api_key(None).await
Expand Down