Files

191 lines
6.2 KiB
Rust
Raw Permalink Normal View History

2025-12-18 13:52:33 -05:00
// Simple authentication test to verify HMAC works
use polyfill_rs::{ClientConfig, ClobClient};
2025-12-18 13:52:33 -05:00
use std::env;
fn build_bootstrap_client(private_key: &str) -> ClobClient {
ClobClient::from_config(ClientConfig {
2026-04-28 09:46:06 -03:00
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")
}
2025-12-18 13:52:33 -05:00
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_create_api_key_simple() {
dotenvy::dotenv().ok();
2026-01-04 11:24:35 -05:00
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("Step 1: Creating/deriving API key...");
let result = bootstrap.create_or_derive_api_key(None).await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match result {
Ok(creds) => {
println!("Successfully created/derived API key");
2026-01-30 21:10:15 -05:00
println!(" API Key created (len={})", creds.api_key.len());
let client = ClobClient::from_config(ClientConfig {
2026-04-28 09:46:06 -03:00
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");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// Now try to get orders (requires auth)
println!("\nStep 2: Testing authenticated endpoint (get_orders)...");
let orders_result = client.get_orders(None, None).await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match orders_result {
Ok(orders) => {
println!("Successfully authenticated! Got {} orders", orders.len());
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
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);
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
Err(e) => {
panic!("Failed to create/derive API key: {:?}", e);
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_api_keys() {
dotenvy::dotenv().ok();
2026-01-04 11:24:35 -05:00
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
2026-01-04 11:24:35 -05:00
let creds = bootstrap
2026-01-04 11:24:35 -05:00
.create_or_derive_api_key(None)
.await
2025-12-18 13:52:33 -05:00
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
2026-04-28 09:46:06 -03:00
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");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("Testing get_api_keys (requires HMAC auth)...");
let result = client.get_api_keys().await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match result {
Ok(keys) => {
println!("Authentication successful! Found {} keys", keys.len());
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
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);
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_trades() {
dotenvy::dotenv().ok();
2026-01-04 11:24:35 -05:00
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
2026-01-04 11:24:35 -05:00
let creds = bootstrap
2026-01-04 11:24:35 -05:00
.create_or_derive_api_key(None)
.await
2025-12-18 13:52:33 -05:00
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
2026-04-28 09:46:06 -03:00
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");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("Testing get_trades (requires HMAC auth)...");
let result = client.get_trades(None, None).await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match result {
Ok(_) => {
println!("Authentication successful!");
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
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);
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
}
}
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_get_notifications() {
dotenvy::dotenv().ok();
2026-01-04 11:24:35 -05:00
let private_key =
env::var("POLYMARKET_PRIVATE_KEY").expect("POLYMARKET_PRIVATE_KEY must be set in .env");
let bootstrap = build_bootstrap_client(&private_key);
2026-01-04 11:24:35 -05:00
let creds = bootstrap
2026-01-04 11:24:35 -05:00
.create_or_derive_api_key(None)
.await
2025-12-18 13:52:33 -05:00
.expect("Failed to create API key");
let client = ClobClient::from_config(ClientConfig {
2026-04-28 09:46:06 -03:00
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");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("Testing get_notifications (requires HMAC auth)...");
let result = client.get_notifications().await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match result {
Ok(notifs) => {
2026-01-30 21:10:15 -05:00
let count = notifs.as_array().map(|arr| arr.len());
match count {
Some(n) => println!("Authentication successful! Notifications: {n}"),
None => println!("Authentication successful! Notifications received"),
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
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);
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
}
}