View Source Hyperliquid.Cache (hyperliquid v0.2.2)
Application cache for storing asset lists and exchange meta information.
This module provides functions to initialize and manage a cache for Hyperliquid-related data, including asset information, exchange metadata, mid prices, and utility functions for retrieving and manipulating cached data.
The cache is implemented using Cachex and stores:
- Exchange metadata (perps and spot)
- Asset mappings (coin name -> asset index)
- Decimal precision information
- Current mid prices (from allMids)
- Token information
Usage
# Initialize cache with API data
Hyperliquid.Cache.init()
# Get mid price for a coin
Hyperliquid.Cache.get_mid("BTC")
# => 43250.5
# Get asset index for a coin
Hyperliquid.Cache.asset_from_coin("BTC")
# => 0
# Update mid prices (typically from WebSocket subscription)
Hyperliquid.Cache.update_mids(%{"BTC" => "43250.5", "ETH" => "2250.0"})
# Subscribe to live mid price updates via WebSocket
Hyperliquid.Cache.subscribe_to_mids()
Summary
Functions
Get all mid prices as string-keyed map (original API format)
Get the asset index for a coin symbol.
Get asset name -> asset index map
Get asset index -> price decimals map
Get asset index -> size decimals map
Clear all entries in the cache.
Get the coin symbol for an asset index (reverse lookup).
Get perp asset contexts
Get asset name -> decimal precision map
Delete a key from the cache.
Get DEX name to asset offset mapping
Check if a key exists in the cache.
Get a value from the cache by key.
Get margin table by ID.
Get the mid price for a coin.
Get perp info by asset index.
Get spot pair info by asset index.
Get token info by index.
Get token info by name.
Get token key in format "NAME:token_id".
Initializes the cache with API information.
Initializes the cache with partial failure handling.
Get margin tables (id => table)
Get perpetuals metadata (base DEX)
Get perpetuals metadata by DEX name
Get list of perp universe entries
Get price decimals by asset index.
Put a key-value pair into the cache.
Query perp or spot info by name or asset index.
Schedule periodic refresh of cache metadata.
Get spot asset contexts
Get spot market metadata
Get size decimals for a spot pair in BASE/QUOTE format.
Get the spot pair ID for info endpoints and subscriptions.
Get list of spot pair entries
Subscribe to live mid price updates via WebSocket.
Get size decimals by asset index.
Get list of token entries
Unsubscribe from live mid price updates.
Update a single mid price.
Update mid prices in cache.
Functions
Get all mid prices as string-keyed map (original API format)
Get the asset index for a coin symbol.
Supports multiple formats:
- Perp: "BTC" → 0
- Spot pair: "HYPE/USDC" → 10107
- Plain coin name: "BTC" → 0
Parameters
coin: Coin symbol (e.g., "BTC", "HYPE/USDC")
Returns
- Asset index as integer, or nil if not found
Example
Hyperliquid.Cache.asset_from_coin("BTC")
# => 0
Hyperliquid.Cache.asset_from_coin("HYPE/USDC")
# => 10107
Get asset name -> asset index map
Get asset index -> price decimals map
Get asset index -> size decimals map
Clear all entries in the cache.
Get the coin symbol for an asset index (reverse lookup).
Parameters
asset: Asset index as integer
Returns
- Coin symbol as string, or nil if not found
Example
Hyperliquid.Cache.coin_from_asset(0)
# => "BTC"
Get perp asset contexts
Get asset name -> decimal precision map
Delete a key from the cache.
Get DEX name to asset offset mapping
Check if a key exists in the cache.
Get a value from the cache by key.
Get margin table by ID.
Example
Hyperliquid.Cache.get_margin_table(56)
# => %{"description" => "tiered 40x", ...}
Get the mid price for a coin.
Parameters
coin: Coin symbol as string (e.g., "BTC", "ETH")
Returns
- Float mid price, or nil if not found
Example
Hyperliquid.Cache.get_mid("BTC")
# => 43250.5
Get perp info by asset index.
Returns the perp universe entry for a given asset index. Searches across all DEXs.
Get spot pair info by asset index.
Returns the spot universe entry for a given asset index.
Get token info by index.
Get token info by name.
Get token key in format "NAME:token_id".
Initializes the cache with API information.
Fetches metadata for perps and spot markets, asset mappings, and current mid prices. This should be called at application startup.
This is a backwards-compatible wrapper around init_with_partial_success/0. Partial successes are treated as full successes for callers that don't need to know which keys failed.
Initializes the cache with partial failure handling.
Fetches all 4 data sources (meta, spot_meta, mids, perp_dexs) and stores only the successful results. Failed fetches are SKIPPED, not stored.
Returns
:ok- All 4 sources fetched successfully{:ok, :partial, failed_keys}- Some sources failed,failed_keysis a list of atoms like[:mids, :perp_dexs]indicating which fetches failed{:error, :all_failed}- All 4 sources failed to fetch
Example
Hyperliquid.Cache.init_with_partial_success()
# => :ok
# If mids API is down:
Hyperliquid.Cache.init_with_partial_success()
# => {:ok, :partial, [:mids]}
Get margin tables (id => table)
Get perpetuals metadata (base DEX)
Get perpetuals metadata by DEX name
Get list of perp universe entries
Get price decimals by asset index.
Put a key-value pair into the cache.
Query perp or spot info by name or asset index.
Examples
Hyperliquid.Cache.query_asset("BTC")
# => %{type: :perp, asset: 0, name: "BTC", sz_decimals: 5, ...}
Hyperliquid.Cache.query_asset("HYPE/USDC")
# => %{type: :spot, asset: 10107, name: "@107", tokens: [107, 0], ...}
Hyperliquid.Cache.query_asset(0)
# => %{type: :perp, asset: 0, name: "BTC", sz_decimals: 5, ...}
Schedule periodic refresh of cache metadata.
This sets up a timer to periodically refresh exchange metadata (perps, spots, tokens) to pick up any new listings.
Options
:interval- Refresh interval in milliseconds (default: 300000ms)
Returns
{:ok, timer_ref}- Timer scheduled successfully
Get spot asset contexts
Get spot market metadata
Get size decimals for a spot pair in BASE/QUOTE format.
Example
Hyperliquid.Cache.spot_pair_decimals("HYPE/USDC")
# => 2
Get the spot pair ID for info endpoints and subscriptions.
Accepts spot markets in "BASE/QUOTE" format (e.g., "HYPE/USDC"). Returns the pair ID used in l2book, trades, etc.
Example
Hyperliquid.Cache.spot_pair_id("HFUN/USDC")
# => "@2"
Hyperliquid.Cache.spot_pair_id("PURR/USDC")
# => "PURR/USDC"
Get list of spot pair entries
Subscribe to live mid price updates via WebSocket.
This creates a WebSocket subscription to the allMids channel which provides real-time price updates for all assets. The cache will be automatically updated as new prices arrive.
Returns
{:ok, subscription_id}- Subscription created successfully{:error, reason}- Failed to subscribe
Example
{:ok, sub_id} = Hyperliquid.Cache.subscribe_to_mids()
Get size decimals by asset index.
Get list of token entries
Unsubscribe from live mid price updates.
Parameters
subscription_id: The subscription ID returned from subscribe_to_mids/0
Update a single mid price.
Parameters
coin: Coin symbol as stringprice: Price as string or number
Update mid prices in cache.
Parameters
mids: Map of coin symbol to price (can have string or atom keys)
Example
Hyperliquid.Cache.update_mids(%{"BTC" => "43250.5", "ETH" => "2250.0"})