Skip to main content
async def getMarketConfig(market_id: Union[str, int]) -> Optional[Dict[str, Any]]:
    print("Getting market config for market ID:", market_id)
    """
    Retrieve market configuration by market ID and return it as a JSON object.

    Args:
        market_id: The market identifier

    Returns:
        Dictionary containing market config data if found, None otherwise

    Raises:
        ValueError: If API_SERVER_URL environment variable is not set

    Example:
        >>> import os
        >>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
        >>> market_config = await getMarketConfig(market_id=1)
        >>> print(market_config["marketName"])
        BTC-PERP
    """
    base_url = os.getenv("API_SERVER_URL")
    if not base_url:
        raise ValueError("API_SERVER_URL environment variable not set")

    async with httpx.AsyncClient() as session:
        client = PerpetualsClient(base_url=base_url, session=session)

        try:
            market_config = await client.get_market_config(market_id)
            if market_config:
                # Convert protobuf message to dictionary with camelCase keys
                market_config_dict = MessageToDict(
                    market_config, always_print_fields_with_no_presence=True
                )
                return market_config_dict
            return None

        except Exception as e:
            print(f"Error getting market config: {e}")
            print("\nTroubleshooting:")
            print("  - Is the server running?")
            print("  - Is this a valid market ID in the database?")
            return None