use polyfill_rs::{ClientConfig, ClobClient};
use std::env;
fn build_bootstrap_client(private_key: &str) -> ClobClient {
ClobClient::from_config(ClientConfig {
base_url: "https://clob.polymarket.com".to_string(),
chain: 137,
private_key: Some(private_key.to_string()),
..ClientConfig::default()
})
.expect("failed to build bootstrap client")
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_create_api_key_simple() {
dotenvy::dotenv().ok();
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
println!("Step 1: Creating/deriving API key...");
let result = bootstrap.create_or_derive_api_key(None).await;
match result {
Ok(creds) => {
println!("Successfully created/derived API key");
println!(" API Key created (len={})", creds.api_key.len());
let client = ClobClient::from_config(ClientConfig {
base_url: "https://clob.polymarket.com".to_string(),
chain: 137,
private_key: Some(private_key),
api_credentials: Some(creds),
..ClientConfig::default()
})
.expect("failed to build authenticated client");
println!("\nStep 2: Testing authenticated endpoint (get_orders)...");
let orders_result = client.get_orders(None, None).await;
match orders_result {
Ok(orders) => {
println!("Successfully authenticated! Got {} orders", orders.len());
},
Err(e) => {
let err_str = format!("{:?}", e);
if err_str.contains("401") {
panic!("CRITICAL: 401 Unauthorized - HMAC authentication is BROKEN!");
} else {
println!("Authentication successful (non-401 error): {:?}", e);
}
},
}
},
Err(e) => {
panic!("Failed to create/derive API key: {:?}", e);
},
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_api_keys() {
dotenvy::dotenv().ok();
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
let creds = bootstrap
.create_or_derive_api_key(None)
.await
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
base_url: "https://clob.polymarket.com".to_string(),
chain: 137,
private_key: Some(private_key),
api_credentials: Some(creds),
..ClientConfig::default()
})
.expect("failed to build authenticated client");
println!("Testing get_api_keys (requires HMAC auth)...");
let result = client.get_api_keys().await;
match result {
Ok(keys) => {
println!("Authentication successful! Found {} keys", keys.len());
},
Err(e) => {
let err_str = format!("{:?}", e);
if err_str.contains("401") {
panic!("CRITICAL: 401 Unauthorized - HMAC authentication is BROKEN!");
} else {
panic!("Failed with non-401 error: {:?}", e);
}
},
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_trades() {
dotenvy::dotenv().ok();
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
let creds = bootstrap
.create_or_derive_api_key(None)
.await
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
base_url: "https://clob.polymarket.com".to_string(),
chain: 137,
private_key: Some(private_key),
api_credentials: Some(creds),
..ClientConfig::default()
})
.expect("failed to build authenticated client");
println!("Testing get_trades (requires HMAC auth)...");
let result = client.get_trades(None, None).await;
match result {
Ok(_) => {
println!("Authentication successful!");
},
Err(e) => {
let err_str = format!("{:?}", e);
if err_str.contains("401") {
panic!("CRITICAL: 401 Unauthorized - HMAC authentication is BROKEN!");
} else {
println!("Authentication successful (got non-401 error): {:?}", e);
}
},
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_notifications() {
dotenvy::dotenv().ok();
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
let creds = bootstrap
.create_or_derive_api_key(None)
.await
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
base_url: "https://clob.polymarket.com".to_string(),
chain: 137,
private_key: Some(private_key),
api_credentials: Some(creds),
..ClientConfig::default()
})
.expect("failed to build authenticated client");
println!("Testing get_notifications (requires HMAC auth)...");
let result = client.get_notifications().await;
match result {
Ok(notifs) => {
let count = notifs.as_array().map(|arr| arr.len());
match count {
Some(n) => println!("Authentication successful! Notifications: {n}"),
None => println!("Authentication successful! Notifications received"),
}
},
Err(e) => {
let err_str = format!("{:?}", e);
if err_str.contains("401") {
panic!("CRITICAL: 401 Unauthorized - HMAC authentication is BROKEN!");
} else {
println!("Authentication successful (got non-401 error): {:?}", e);
}
},
}
}