| Crates.io | rs-clob-client |
| lib.rs | rs-clob-client |
| version | 0.1.5 |
| created_at | 2025-12-02 09:39:56.395916+00 |
| updated_at | 2026-01-19 10:13:30.209278+00 |
| description | Rust client for Polymarket's CLOB (Central Limit Order Book) |
| homepage | https://github.com/tdergouzi/rs-clob-client |
| repository | https://github.com/tdergouzi/rs-clob-client |
| max_upload_size | |
| id | 1961447 |
| size | 309,900 |
🦀 Rust client for Polymarket's CLOB (Central Limit Order Book) API with full EIP-712 signing support.
This library provides a complete Rust port of the TypeScript @polymarket/clob-client, enabling you to interact with Polymarket's trading infrastructure for creating orders, managing API keys, fetching market data, and more.
Add this to your Cargo.toml:
[dependencies]
rs-clob-client = "0.1"
alloy-primitives = "0.8"
alloy-signer-local = "0.5"
tokio = { version = "1.0", features = ["full"] }
use rs_clob_client::{ClobClient, Chain, Side, OrderType, UserOrder};
use alloy_signer_local::PrivateKeySigner;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a wallet from private key
let private_key = "your_private_key_here";
let wallet = private_key.parse::<PrivateKeySigner>()?;
// Initialize the CLOB client
let host = "https://clob.polymarket.com".to_string();
let gamma_host = "https://gamma-api.polymarket.com".to_string();
// Create or derive API credentials
let temp_client = ClobClient::new(
host.clone(), gamma_host.clone(), Chain::Polygon,
Some(wallet.clone()), None, None, None, None, false, None, None,
)?;
let creds = temp_client.create_or_derive_api_key().await?;
// Create the main client with credentials
let client = ClobClient::new(
host, gamma_host, Chain::Polygon,
Some(wallet), Some(creds),
Some(1), // Signature type: 0 = EOA, 1 = Poly Proxy, 2 = EIP-1271
None, None, false, None, None,
)?;
// Place a limit order
let order = UserOrder {
token_id: "your_token_id".to_string(),
price: 0.52,
size: 10.0,
side: Side::Buy,
fee_rate_bps: None,
nonce: None,
expiration: None,
};
let result = client.create_order(order, OrderType::Gtc).await?;
println!("Order created: {:?}", result);
Ok(())
}
use rs_clob_client::{ClobClient, Chain};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Public endpoints don't require authentication
let client = ClobClient::new(
"https://clob.polymarket.com".to_string(),
"https://gamma-api.polymarket.com".to_string(),
Chain::Polygon,
None, None, None, None, None, false, None, None,
)?;
// Get orderbook for a token
let token_id = "your_token_id";
let orderbook = client.get_orderbook(token_id).await?;
println!("Orderbook: {:?}", orderbook);
// Get current price
let price = client.get_price(token_id, rs_clob_client::Side::Buy).await?;
println!("Best buy price: {:?}", price);
// Get spread
let spread = client.get_spread(token_id).await?;
println!("Spread: {:?}", spread);
// Get recent trades
let trades = client.get_trades(token_id).await?;
println!("Recent trades: {:?}", trades);
Ok(())
}
// Cancel a specific order
let order_id = "your_order_id";
client.cancel_order(order_id).await?;
// Cancel all orders for a market
let token_id = "your_token_id";
client.cancel_market_orders(
rs_clob_client::CancelMarketOrdersParams {
asset_id: token_id.to_string(),
}
).await?;
// Cancel all orders
client.cancel_all().await?;
// Get your open orders
let open_orders = client.get_orders().await?;
println!("Open orders: {:?}", open_orders);
use rs_clob_client::{UserMarketOrder, Side};
// Execute a market buy
let market_order = UserMarketOrder {
token_id: "your_token_id".to_string(),
amount: 100.0, // Amount in USDC
price: None, // Optional price limit
fee_rate_bps: None,
nonce: None,
};
let result = client.post_order(market_order, Side::Buy).await?;
println!("Market order executed: {:?}", result);
use rs_builder_signing_sdk::BuilderConfig;
// Create builder configuration
let builder_config = BuilderConfig::new(
"your_builder_api_key".to_string(),
"your_builder_secret".to_string(),
);
// Create client with builder support
let client = ClobClient::new(
"https://clob.polymarket.com".to_string(),
"https://gamma-api.polymarket.com".to_string(),
Chain::Polygon,
Some(wallet), Some(creds), Some(0), None, None, false,
Some(builder_config), None,
)?;
// Now you can use builder-specific endpoints
let builder_trades = client.get_builder_trades(None, None).await?;
create_or_derive_api_key() instead of creating new keys repeatedlyThe client supports HTTP/HTTPS/SOCKS5 proxy for all API requests. Pass the proxy URL as the last parameter:
let client = ClobClient::new(
host, gamma_host, chain_id,
Some(wallet), Some(creds), Some(1), None, None, false, None,
Some("http://127.0.0.1:7890".to_string()), // proxy_url
)?;
Supported formats: http://host:port, https://host:port, socks5://host:port (with optional user:pass@)
Chain::Polygon (137)Chain::Amoy (80002)# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test with output
cargo test test_name -- --exact --nocapture
# Build the library
cargo build --release
See the TypeScript examples in the original repository for reference. Rust equivalents can be created following the patterns shown above.
⚠️ AI-Generated Code: This library was generated with AI assistance as a port from the official TypeScript implementation. While it aims for feature parity and has been structured following Rust best practices, users should:
Contributions are welcome! This is a community-maintained port of the official TypeScript client.
MIT