| Crates.io | polycode-sdk |
| lib.rs | polycode-sdk |
| version | 1.0.0 |
| created_at | 2025-12-08 15:05:22.623606+00 |
| updated_at | 2025-12-08 15:05:22.623606+00 |
| description | Rust SDK for PolyCode - prediction markets resolved with code |
| homepage | |
| repository | https://github.com/PolyCodeOne/sdk-rust |
| max_upload_size | |
| id | 1973708 |
| size | 73,435 |
A comprehensive Rust SDK for interacting with the PolyCode API. PolyCode allows you to create prediction markets that are resolved with code.
Add this to your Cargo.toml:
[dependencies]
polycode-sdk = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
use polycode_sdk::{PolyCodeConfig, PolyCodeClient};
use polycode_sdk::api::{MarketsApi, BetsApi};
use polycode_sdk::models::{CreateMarketRequest, PlaceBetRequest, MarketStatus};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Configure the client
let config = PolyCodeConfig::new()
.with_base_url("https://api.polycode.com")?
.with_api_key("your-api-key")
.with_timeout(30);
let client = PolyCodeClient::new(config)?;
// Work with markets
let markets = MarketsApi::new(client.clone());
// Create a market
let market_request = CreateMarketRequest {
title: "Will it rain tomorrow?".to_string(),
description: "Predicting weather for tomorrow".to_string(),
resolution_code: "check_weather_api()".to_string(),
code_language: Some("python".to_string()),
outcomes: vec!["Yes".to_string(), "No".to_string()],
metadata: None,
};
let market = markets.create_market(&market_request).await?;
println!("Created market: {}", market.id);
// List open markets
let response = markets.list_markets(Some(MarketStatus::Open), Some(10), None).await?;
println!("Found {} markets", response.markets.len());
// Get a specific market
let market = markets.get_market(&market.id).await?;
println!("Market status: {:?}", market.status);
// Work with bets
let bets = BetsApi::new(client);
// Place a bet
let bet_request = PlaceBetRequest {
market_id: market.id.clone(),
outcome: "Yes".to_string(),
amount: 100.0,
currency: Some("USD".to_string()),
};
let bet = bets.place_bet(&bet_request).await?;
println!("Placed bet: {}", bet.id);
// List bets for a market
let bet_response = bets.list_bets(Some(&market.id), None, Some(10), None).await?;
println!("Found {} bets", bet_response.bets.len());
Ok(())
}
The SDK supports flexible configuration through the PolyCodeConfig builder:
use polycode_sdk::PolyCodeConfig;
let config = PolyCodeConfig::new()
.with_base_url("https://api.polycode.com")?
.with_api_key("your-api-key")
.with_timeout(60)
.with_header("X-Custom-Header", "value");
The SDK provides comprehensive error handling:
use polycode_sdk::{PolyCodeError, Result};
async fn example() -> Result<()> {
match markets.create_market(&request).await {
Ok(market) => println!("Success: {}", market.id),
Err(PolyCodeError::ApiError { status, message }) => {
eprintln!("API error {}: {}", status, message);
}
Err(PolyCodeError::AuthenticationError(msg)) => {
eprintln!("Auth error: {}", msg);
}
Err(e) => eprintln!("Error: {}", e),
}
Ok(())
}
create_market(request) - Create a new prediction marketget_market(id) - Get market by IDlist_markets(status, limit, cursor) - List markets with optional filtersresolve_market(id, request) - Resolve a market with code execution resultget_market_resolution(id) - Get resolution details for a marketplace_bet(request) - Place a bet on a market outcomeget_bet(id) - Get bet by IDlist_bets(market_id, user_id, limit, cursor) - List bets with optional filterscancel_bet(id) - Cancel a bet (if allowed)Represents a prediction market with the following fields:
id: Unique identifiertitle: Market titledescription: Market descriptionstatus: Current status (Open, Resolved, Cancelled, Pending)resolution_code: Code that will be executed to resolve the marketcode_language: Language/runtime for the resolution codeoutcomes: Possible outcomescreated_at: Creation timestampresolved_at: Resolution timestamp (if resolved)resolved_outcome: The resolved outcome (if resolved)Represents a bet placed on a market:
id: Unique identifiermarket_id: ID of the marketoutcome: The outcome being bet onamount: Bet amountcurrency: Currency (optional)user_id: User who placed the betcreated_at: Creation timestampstatus: Bet status (Active, Cancelled, Settled)MIT OR Apache-2.0
Contributions are welcome! Please feel free to submit a Pull Request.