Retrieve all market configurations and return them as a JSON object.
Dictionary containing markets 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"
>>> markets = await getMarkets()
>>> print(len(markets.get("markets", [])))
3
async def getMarkets() -> Optional[Dict[str, Any]]:
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:
markets = await client.get_markets()
if markets is not None:
markets_list = [MessageToDict(market, always_print_fields_with_no_presence=True) for market in markets]
return {'markets': markets_list}
return None
except Exception as e:
print(f'Error getting markets: {e}')
print('\nTroubleshooting:')
print(' - Is the server running?')
print(' - Are there markets configured in the database?')
return None