polymarket 0.1.0

Rust SDK for Polymarket prediction market - CLOB trading, on-chain operations, and WebSocket streaming
Documentation
use crate::clob::constants::{AMOY, POLYGON};

/// CLOB API hosts
pub const CLOB_API_URL: &str = "https://clob.polymarket.com";
pub const CLOB_TESTNET_API_URL: &str = "https://clob-testnet.polymarket.com";

/// Configuration for the CLOB client
#[derive(Debug, Clone)]
pub struct ClobConfig {
    /// The base URL for the CLOB API
    pub host: String,

    /// The chain ID (POLYGON for mainnet, AMOY for testnet)
    pub chain_id: u64,

    /// Optional funder address for neg risk markets
    pub funder: Option<String>,

    /// Signature type (0 = EOA, 1 = POLY_PROXY, 2 = POLY_GNOSIS_SAFE)
    pub signature_type: u8,
}

impl ClobConfig {
    /// Create a new mainnet configuration
    pub fn new_mainnet() -> Self {
        Self {
            host: CLOB_API_URL.to_string(),
            chain_id: POLYGON,
            funder: None,
            signature_type: 0,
        }
    }

    /// Create a new testnet configuration
    pub fn new_testnet() -> Self {
        Self {
            host: CLOB_TESTNET_API_URL.to_string(),
            chain_id: AMOY,
            funder: None,
            signature_type: 0,
        }
    }

    /// Create a custom configuration
    ///
    /// # Arguments
    /// * `host` - The CLOB API host URL
    /// * `chain_id` - The chain ID
    pub fn new(host: impl Into<String>, chain_id: u64) -> Self {
        Self {
            host: host.into(),
            chain_id,
            funder: None,
            signature_type: 0,
        }
    }

    /// Set the funder address
    pub fn with_funder(mut self, funder: impl Into<String>) -> Self {
        self.funder = Some(funder.into());
        self
    }

    /// Set the signature type
    pub fn with_signature_type(mut self, signature_type: u8) -> Self {
        self.signature_type = signature_type;
        self
    }
}

impl Default for ClobConfig {
    fn default() -> Self {
        Self::new_mainnet()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mainnet_config() {
        let config = ClobConfig::new_mainnet();
        assert_eq!(config.host, CLOB_API_URL);
        assert_eq!(config.chain_id, POLYGON);
        assert_eq!(config.signature_type, 0);
        assert!(config.funder.is_none());
    }

    #[test]
    fn test_testnet_config() {
        let config = ClobConfig::new_testnet();
        assert_eq!(config.host, CLOB_TESTNET_API_URL);
        assert_eq!(config.chain_id, AMOY);
        assert_eq!(config.signature_type, 0);
        assert!(config.funder.is_none());
    }

    #[test]
    fn test_custom_config() {
        let config = ClobConfig::new("https://custom.api", 1337);
        assert_eq!(config.host, "https://custom.api");
        assert_eq!(config.chain_id, 1337);
    }

    #[test]
    fn test_with_funder() {
        let config = ClobConfig::new_mainnet()
            .with_funder("0x1234567890123456789012345678901234567890");
        assert_eq!(config.funder.as_ref().unwrap(), "0x1234567890123456789012345678901234567890");
    }

    #[test]
    fn test_with_signature_type() {
        let config = ClobConfig::new_mainnet().with_signature_type(1);
        assert_eq!(config.signature_type, 1);
    }

    #[test]
    fn test_default_config() {
        let config = ClobConfig::default();
        assert_eq!(config.host, CLOB_API_URL);
        assert_eq!(config.chain_id, POLYGON);
    }
}