gmx.synthetic_tokens
Documentation for eth_defi.gmx.synthetic_tokens Python module.
GMX Synthetic token details fetching and caching.
Fetch GMX synthetic token data from APIs and cache results for efficient access.
Functions
|
Fetch GMX synthetic token details from API with caching and retry logic. |
|
Get a specific GMX token by address on a given chain. |
|
Get a specific GMX token by symbol on a given chain. |
Get list of chain IDs that support GMX synthetic tokens. |
|
Reset the default GMX token cache. |
Classes
GMX Synthetic token Python representation. |
Exceptions
Exception raised when GMX token fetching fails. |
- DEFAULT_GMX_TOKEN_CACHE = LRUCache({}, maxsize=512, currsize=0)
Default cache for GMX token details
- GMXTokenAddress
GMX token address type alias
- class GMXSyntheticTokenDetails
Bases:
objectGMX Synthetic token Python representation.
A helper class to work with GMX synthetic tokens from their API. Similar to TokenDetails but designed for GMX API data structure.
Example usage:
# Fetch all GMX tokens for Arbitrum tokens = fetch_gmx_synthetic_tokens(chain_id=42161) usdc_token = next(t for t in tokens if t.symbol == "USDC") print(f"USDC address on Arbitrum: {usdc_token.address}")
Key differences from ERC-20 TokenDetails: - No web3 contract instance needed - Data comes from API, not blockchain calls - Simpler structure (no name or total_supply from API)
- address: eth_typing.evm.HexAddress
Token contract address
- convert_to_decimals(raw_amount)
Convert raw token units to decimal representation.
- Parameters
raw_amount (int) – Raw token amount as integer
- Returns
Decimal representation of the amount
- Return type
- Example:
If token has 6 decimals, converts 1000000 -> 1.0
- convert_to_raw(decimal_amount)
Convert decimal token amount to raw integer units.
- Parameters
decimal_amount (decimal.Decimal) – Decimal amount
- Returns
Raw token amount as integer
- Return type
- Example:
If token has 6 decimals, converts 1.0 -> 1000000
- static generate_cache_key(chain_id, symbol)
Generate cache key for GMX token.
We cache by (chain_id, symbol) since GMX API gives us symbol-based data. This is different from ERC-20 caching which uses address.
- export()
Export token details as serializable dictionary.
Useful for saving to disk cache or API responses.
- exception GMXTokenFetchError
Bases:
ExceptionException raised when GMX token fetching fails.
- __init__(*args, **kwargs)
- __new__(**kwargs)
- add_note()
Exception.add_note(note) – add a note to the exception
- with_traceback()
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- fetch_gmx_synthetic_tokens(chain_id, cache=LRUCache({}, maxsize=512, currsize=0), timeout=10.0, force_refresh=False, max_retries=2, retry_delay=0.1)
Fetch GMX synthetic token details from API with caching and retry logic.
This function fetches all available GMX synthetic tokens for a given chain and caches the results to avoid repeated API calls. It implements retry logic with exponential backoff and automatic failover to backup API endpoints.
- Parameters
chain_id (int) – Blockchain chain ID (42161 for Arbitrum, 43114 for Avalanche)
cache (Optional[cachetools.Cache]) – Cache instance to use. Set to None to disable caching
timeout (float) – HTTP request timeout in seconds
force_refresh (bool) – If True, bypass cache and fetch fresh data
max_retries (int) – Maximum number of retry attempts per endpoint (default: 2)
retry_delay (float) – Initial delay between retries in seconds with exponential backoff (default: 0.1s, resulting in 0.1s, 0.2s delays)
- Returns
list of GMXSyntheticTokenDetails objects
- Raises
GMXTokenFetchError – If API request fails on both primary and backup
ValueError – If chain_id is not supported
- Return type
list[eth_defi.gmx.synthetic_tokens.GMXSyntheticTokenDetails]
Example:
# Fetch Arbitrum GMX tokens with automatic retry and failover tokens = fetch_gmx_synthetic_tokens(chain_id=42161)
- get_gmx_synthetic_token_by_symbol(chain_id, symbol, cache=LRUCache({}, maxsize=512, currsize=0))
Get a specific GMX token by symbol on a given chain.
This is a convenience function that fetches all tokens and filters by symbol. More efficient than fetching tokens repeatedly when you need just one.
- Parameters
- Returns
GMXSyntheticTokenDetails if found, None otherwise
- Return type
Optional[eth_defi.gmx.synthetic_tokens.GMXSyntheticTokenDetails]
Example:
# Get USDC token on Arbitrum usdc = get_gmx_synthetic_token_by_symbol(42161, "USDC") if usdc: print(f"USDC decimals: {usdc.decimals}")
- get_gmx_synthetic_token_by_address(chain_id, address, cache=LRUCache({}, maxsize=512, currsize=0))
Get a specific GMX token by address on a given chain.
- Parameters
chain_id (int) – Blockchain chain ID
address (eth_typing.evm.HexAddress) – Token contract address
cache (Optional[cachetools.Cache]) – Cache instance to use
- Returns
GMXSyntheticTokenDetails if found, None otherwise
- Return type
Optional[eth_defi.gmx.synthetic_tokens.GMXSyntheticTokenDetails]
- reset_gmx_token_cache()
Reset the default GMX token cache.
Useful for testing or when you want to force fresh API calls.