From c424456301693efd98814dc9032767609e86933b Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Tue, 2 Jun 2026 12:44:06 -0700 Subject: [PATCH] fix(info): skip spot markets with out-of-range token indices Signed-off-by: Sai Asish Y --- hyperliquid/info.py | 6 ++++-- tests/info_test.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/hyperliquid/info.py b/hyperliquid/info.py index 360d0588..92ec72b6 100644 --- a/hyperliquid/info.py +++ b/hyperliquid/info.py @@ -47,8 +47,10 @@ def __init__( self.coin_to_asset[spot_info["name"]] = asset self.name_to_coin[spot_info["name"]] = spot_info["name"] base, quote = spot_info["tokens"] - base_info = token_by_index[base] - quote_info = token_by_index[quote] + base_info = token_by_index.get(base) + quote_info = token_by_index.get(quote) + if base_info is None or quote_info is None: + continue self.asset_to_sz_decimals[asset] = base_info["szDecimals"] name = f'{base_info["name"]}/{quote_info["name"]}' if name not in self.name_to_coin: diff --git a/tests/info_test.py b/tests/info_test.py index 79e146c9..b85bfe32 100644 --- a/tests/info_test.py +++ b/tests/info_test.py @@ -257,3 +257,38 @@ def test_extra_agents(): assert "name" in agent, "Each agent should have a 'name' field" assert "address" in agent, "Each agent should have an 'address' field" assert "validUntil" in agent, "Each agent should have a 'validUntil' field" + + +def test_spot_universe_with_out_of_range_token_index_is_skipped(): + spot_meta: SpotMeta = { + "universe": [ + {"index": 0, "name": "PURR/USDC", "tokens": [1, 0], "isCanonical": True}, + {"index": 1, "name": "@999", "tokens": [479, 0], "isCanonical": False}, + ], + "tokens": [ + { + "name": "USDC", + "szDecimals": 8, + "weiDecimals": 8, + "index": 0, + "tokenId": "0x" + "0" * 64, + "isCanonical": True, + "evmContract": None, + "fullName": None, + }, + { + "name": "PURR", + "szDecimals": 0, + "weiDecimals": 5, + "index": 1, + "tokenId": "0x" + "1" * 64, + "isCanonical": True, + "evmContract": None, + "fullName": None, + }, + ], + } + info = Info(skip_ws=True, meta=TEST_META, spot_meta=spot_meta) + assert info.coin_to_asset["PURR/USDC"] == 10000 + assert "PURR/USDC" in info.name_to_coin + assert "@999" in info.coin_to_asset