polymarket 0.1.0

Rust SDK for Polymarket prediction market - CLOB trading, on-chain operations, and WebSocket streaming
Documentation
use alloy::primitives::{FixedBytes, U256};

/// USDC has 6 decimals on Polygon
pub const USDC_DECIMALS: u8 = 6;

/// USDCe (bridged) also has 6 decimals
pub const USDCE_DECIMALS: u8 = 6;

/// CTF tokens have 6 decimals (same as collateral)
pub const CTF_DECIMALS: u8 = 6;

/// Zero bytes32 - used as parent collection ID for base positions
pub const ZERO_BYTES32: FixedBytes<32> = FixedBytes::<32>::ZERO;

/// Binary partition for standard YES/NO markets: [1, 2]
pub const BINARY_PARTITION: [u8; 2] = [1, 2];

/// Maximum uint256 value - used for unlimited approvals
pub fn max_uint256() -> U256 {
    U256::MAX
}

/// Convert binary partition to U256 vector
pub fn binary_partition_u256() -> Vec<U256> {
    vec![U256::from(1), U256::from(2)]
}

/// Proxy transaction type codes
pub mod proxy_tx_type {
    /// Regular contract call
    pub const CALL: u8 = 1;

    /// Delegate call
    pub const DELEGATECALL: u8 = 2;
}

/// Safe transaction operation types
pub mod safe_operation {
    /// Regular call
    pub const CALL: u8 = 0;

    /// Delegate call
    pub const DELEGATECALL: u8 = 1;
}

/// Gas limits for common operations (in gas units)
pub mod gas_limits {
    use alloy::primitives::U256;

    /// ERC20 approval
    pub fn approve() -> U256 {
        U256::from(50_000)
    }

    /// ERC20 transfer
    pub fn transfer() -> U256 {
        U256::from(65_000)
    }

    /// CTF split position
    pub fn split() -> U256 {
        U256::from(300_000)
    }

    /// CTF merge positions
    pub fn merge() -> U256 {
        U256::from(250_000)
    }

    /// CTF redeem positions
    pub fn redeem() -> U256 {
        U256::from(200_000)
    }

    /// Proxy wallet transaction
    pub fn proxy() -> U256 {
        U256::from(500_000)
    }

    /// Safe transaction
    pub fn safe() -> U256 {
        U256::from(400_000)
    }
}

/// Standard gas prices for Polygon (in gwei)
pub mod gas_prices {
    use alloy::primitives::U256;

    /// Convert gwei to wei
    fn gwei(amount: u64) -> U256 {
        U256::from(amount) * U256::from(1_000_000_000)
    }

    /// Low priority (30 gwei)
    pub fn low() -> U256 {
        gwei(30)
    }

    /// Standard priority (50 gwei)
    pub fn standard() -> U256 {
        gwei(50)
    }

    /// High priority (100 gwei)
    pub fn high() -> U256 {
        gwei(100)
    }

    /// Urgent (200 gwei)
    pub fn urgent() -> U256 {
        gwei(200)
    }
}

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

    #[test]
    fn test_decimals() {
        assert_eq!(USDC_DECIMALS, 6);
        assert_eq!(USDCE_DECIMALS, 6);
        assert_eq!(CTF_DECIMALS, 6);
    }

    #[test]
    fn test_binary_partition() {
        assert_eq!(BINARY_PARTITION, [1, 2]);
        let partition = binary_partition_u256();
        assert_eq!(partition.len(), 2);
        assert_eq!(partition[0], U256::from(1));
        assert_eq!(partition[1], U256::from(2));
    }

    #[test]
    fn test_zero_bytes32() {
        assert_eq!(ZERO_BYTES32, FixedBytes::<32>::ZERO);
    }

    #[test]
    fn test_max_uint256() {
        assert_eq!(max_uint256(), U256::MAX);
    }

    #[test]
    fn test_proxy_tx_types() {
        assert_eq!(proxy_tx_type::CALL, 1);
        assert_eq!(proxy_tx_type::DELEGATECALL, 2);
    }

    #[test]
    fn test_safe_operations() {
        assert_eq!(safe_operation::CALL, 0);
        assert_eq!(safe_operation::DELEGATECALL, 1);
    }

    #[test]
    fn test_gas_limits() {
        assert_eq!(gas_limits::approve(), U256::from(50_000));
        assert_eq!(gas_limits::split(), U256::from(300_000));
    }

    #[test]
    fn test_gas_prices() {
        assert_eq!(gas_prices::low(), U256::from(30_000_000_000u64));
        assert_eq!(gas_prices::standard(), U256::from(50_000_000_000u64));
        assert_eq!(gas_prices::high(), U256::from(100_000_000_000u64));
    }
}