Files
polyfill-rs/tests/order_posting_test.rs
T

109 lines
4.0 KiB
Rust
Raw Normal View History

2025-12-18 13:52:33 -05:00
// Test order posting - the critical endpoint that had the 401 bug
use polyfill_rs::{ClientConfig, ClobClient, OrderArgs, Side};
2025-12-18 13:52:33 -05:00
use rust_decimal::Decimal;
use std::env;
use std::str::FromStr;
#[tokio::test(flavor = "multi_thread")]
#[ignore]
async fn test_post_order_authentication() {
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 = 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.clone()),
..ClientConfig::default()
})
.expect("failed to build bootstrap client");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("Step 1: Creating API credentials...");
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");
2025-12-18 13:52:33 -05:00
println!("API credentials set");
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// Use a well-known token ID (we'll use an extreme price so it won't fill)
let token_id = "21742633143463906290569050155826241533067272736897614950488156847949938836455"; // Example token
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
println!("\nStep 2: Attempting to post order (testing authentication)...");
let order_args = OrderArgs {
token_id: token_id.to_string(),
price: Decimal::from_str("0.01").unwrap(), // Very low price, won't fill
size: Decimal::from_str("1.0").unwrap(),
side: Side::BUY,
expiration: None,
builder_code: None,
metadata: None,
2025-12-18 13:52:33 -05:00
};
2026-01-04 11:24:35 -05:00
let result = client.create_and_post_order(&order_args, None, None).await;
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
match result {
Ok(response) => {
println!("AUTHENTICATION SUCCESSFUL! Order was accepted by API");
println!(" Response: {:?}", response);
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// Try to cancel it if we got an order ID
if !response.order_id.is_empty() {
2025-12-18 13:52:33 -05:00
println!("\nStep 3: Canceling order...");
match client.cancel(&response.order_id).await {
2025-12-18 13:52:33 -05:00
Ok(_) => println!("Order canceled successfully"),
Err(e) => println!("Cancel failed (order might have expired): {:?}", e),
}
}
2026-01-04 11:24:35 -05:00
},
2025-12-18 13:52:33 -05:00
Err(e) => {
let err_str = format!("{:?}", e);
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// The critical test: Is it a 401 error?
if err_str.contains("401") {
2026-01-04 11:24:35 -05:00
panic!(
"CRITICAL FAILURE: 401 Unauthorized!\n\
2025-12-18 13:52:33 -05:00
The HMAC authentication bug is NOT fixed!\n\
2026-01-04 11:24:35 -05:00
Error: {:?}",
e
);
2025-12-18 13:52:33 -05:00
}
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// If it's a 400 error with validation issues, that's actually GOOD
// It means authentication worked, but there's an issue with the order parameters
if err_str.contains("400") {
println!("AUTHENTICATION SUCCESSFUL!");
println!(" (Got 400 validation error, which means auth passed)");
println!(" Error details: {}", err_str);
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// These are expected validation errors when auth works
2026-01-04 11:24:35 -05:00
if err_str.contains("insufficient")
|| err_str.contains("balance")
|| err_str.contains("allowance")
|| err_str.contains("POLY_AMOUNT_TOO_SMALL")
|| err_str.contains("invalid")
|| err_str.contains("market")
{
2025-12-18 13:52:33 -05:00
println!(" This is an expected validation error - authentication is working!");
return;
}
}
2026-01-04 11:24:35 -05:00
2025-12-18 13:52:33 -05:00
// Any other error type
2026-01-04 11:24:35 -05:00
println!(
"Got unexpected error (not 401, so auth might be OK): {:?}",
e
);
},
2025-12-18 13:52:33 -05:00
}
}