polymarket 0.1.0

Rust SDK for Polymarket prediction market - CLOB trading, on-chain operations, and WebSocket streaming
Documentation
const POLYGON_MAINNET_CHAIN_ID: u64 = 137;
const POLYGON_AMOY_TESTNET_CHAIN_ID: u64 = 80002;
/// Contract configuration for different chains and risk modes
#[derive(Debug, Clone)]
pub struct ContractConfig {
    pub exchange: String,
    pub collateral: String,
    pub conditional_tokens: String,
}

/// Get contract configuration for a given chain ID and risk mode
///
/// # Arguments
/// * `chain_id` - Chain ID (137 for Polygon mainnet, 80002 for Amoy testnet)
/// * `neg_risk` - Whether to use neg-risk contracts
///
/// # Returns
/// Contract configuration or None if chain ID is invalid
pub fn get_contract_config(chain_id: u64, neg_risk: bool) -> Option<ContractConfig> {
    match neg_risk {
        true => {
            // Neg-risk contract configuration
            if chain_id == POLYGON_MAINNET_CHAIN_ID {
                Some(ContractConfig {
                    exchange: "0xC5d563A36AE78145C45a50134d48A1215220f80a".to_owned(),
                    collateral: "0x2791bca1f2de4661ed88a30c99a7a9449aa84174".to_owned(),
                    conditional_tokens: "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045".to_owned(),
                })
            } else if chain_id == POLYGON_AMOY_TESTNET_CHAIN_ID {
                Some(ContractConfig {
                    exchange: "0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296".to_owned(),
                    collateral: "0x9c4e1703476e875070ee25b56a58b008cfb8fa78".to_owned(),
                    conditional_tokens: "0x69308FB512518e39F9b16112fA8d994F4e2Bf8bB".to_owned(),
                })
            } else {
                None
            }
        }
        false => {
            // Regular contract configuration
            if chain_id == POLYGON_MAINNET_CHAIN_ID {
                Some(ContractConfig {
                    exchange: "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E".to_owned(),
                    collateral: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174".to_owned(),
                    conditional_tokens: "0x4D97DCd97eC945f40cF65F87097ACe5EA0476045".to_owned(),
                })
            } else if chain_id == POLYGON_AMOY_TESTNET_CHAIN_ID {
                Some(ContractConfig {
                    exchange: "0xdFE02Eb6733538f8Ea35D585af8DE5958AD99E40".to_owned(),
                    collateral: "0x9c4e1703476e875070ee25b56a58b008cfb8fa78".to_owned(),
                    conditional_tokens: "0x69308FB512518e39F9b16112fA8d994F4e2Bf8bB".to_owned(),
                })
            } else {
                None
            }
        }
    }
}

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

    #[test]
    fn test_polygon_mainnet_regular() {
        let config = get_contract_config(137, false).unwrap();
        assert_eq!(config.exchange, "0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E");
        assert_eq!(config.collateral, "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174");
    }

    #[test]
    fn test_polygon_mainnet_neg_risk() {
        let config = get_contract_config(137, true).unwrap();
        assert_eq!(config.exchange, "0xC5d563A36AE78145C45a50134d48A1215220f80a");
    }

    #[test]
    fn test_amoy_testnet_regular() {
        let config = get_contract_config(80002, false).unwrap();
        assert_eq!(config.exchange, "0xdFE02Eb6733538f8Ea35D585af8DE5958AD99E40");
    }

    #[test]
    fn test_amoy_testnet_neg_risk() {
        let config = get_contract_config(80002, true).unwrap();
        assert_eq!(config.exchange, "0xd91E80cF2E7be2e162c6513ceD06f1dD0dA35296");
    }

    #[test]
    fn test_invalid_chain_id() {
        assert!(get_contract_config(999, false).is_none());
        assert!(get_contract_config(999, true).is_none());
    }
}