Skip to content

Commit fa80c7c

Browse files
committed
Version 0.5.0
Fixes issue with incorrect coin to asset mapping for spot
1 parent 80f4893 commit fa80c7c

File tree

6 files changed

+67
-42
lines changed

6 files changed

+67
-42
lines changed

assets/images/coverage.svg

Lines changed: 2 additions & 2 deletions
Loading

examples/basic_spot_order.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import example_utils
55

66

7-
COIN = "PURR/USDC"
7+
PURR = "PURR/USDC"
8+
OTHER_COIN = "@8"
89

910

1011
def main():
@@ -20,7 +21,7 @@ def main():
2021
print("no available token balances")
2122

2223
# Place an order that should rest by setting the price very low
23-
order_result = exchange.order(COIN, True, 1200, 0.01, {"limit": {"tif": "Gtc"}})
24+
order_result = exchange.order(PURR, True, 24, 0.5, {"limit": {"tif": "Gtc"}})
2425
print(order_result)
2526

2627
# Query the order status by oid
@@ -34,7 +35,16 @@ def main():
3435
if order_result["status"] == "ok":
3536
status = order_result["response"]["data"]["statuses"][0]
3637
if "resting" in status:
37-
cancel_result = exchange.cancel(COIN, status["resting"]["oid"])
38+
cancel_result = exchange.cancel(PURR, status["resting"]["oid"])
39+
print(cancel_result)
40+
41+
# For other spot assets other than PURR/USDC use @{index} e.g. on testnet @8 is KORILA/USDC
42+
order_result = exchange.order(OTHER_COIN, True, 1, 12, {"limit": {"tif": "Gtc"}})
43+
print(order_result)
44+
if order_result["status"] == "ok":
45+
status = order_result["response"]["data"]["statuses"][0]
46+
if "resting" in status:
47+
cancel_result = exchange.cancel(OTHER_COIN, status["resting"]["oid"])
3848
print(cancel_result)
3949

4050

hyperliquid/exchange.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def __init__(
5959
self.coin_to_asset = {asset_info["name"]: asset for (asset, asset_info) in enumerate(self.meta["universe"])}
6060

6161
# spot assets start at 10000
62-
for i, spot_pair in enumerate(self.spot_meta["universe"]):
63-
self.coin_to_asset[spot_pair["name"]] = i + 10000
62+
for spot_info in self.spot_meta["universe"]:
63+
self.coin_to_asset[spot_info["name"]] = spot_info["index"] + 10000
6464

6565
def _post_action(self, action, signature, nonce):
6666
payload = {
@@ -82,9 +82,9 @@ def _slippage_price(
8282
if not px:
8383
# Get midprice
8484
px = float(self.info.all_mids()[coin])
85-
85+
8686
# spot assets start at 10000
87-
is_spot = self.coin_to_asset.get(coin) >= 10_000
87+
is_spot = self.coin_to_asset[coin] >= 10_000
8888

8989
# Calculate Slippage
9090
px *= (1 + slippage) if is_buy else (1 - slippage)

hyperliquid/info.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
from hyperliquid.api import API
2-
from hyperliquid.utils.types import Any, Callable, Meta, SpotMeta, SpotMetaAndAssetCtxs, Optional, Subscription, cast, Cloid
2+
from hyperliquid.utils.types import (
3+
Any,
4+
Callable,
5+
Meta,
6+
SpotMeta,
7+
SpotMetaAndAssetCtxs,
8+
Optional,
9+
Subscription,
10+
cast,
11+
Cloid,
12+
)
313
from hyperliquid.websocket_manager import WebsocketManager
414

515

@@ -171,36 +181,36 @@ def meta(self) -> Meta:
171181
}
172182
"""
173183
return cast(Meta, self.post("/info", {"type": "meta"}))
174-
175-
def metaAndAssetCtxs(self) -> Any:
184+
185+
def meta_and_asset_ctxs(self) -> Any:
176186
"""Retrieve exchange MetaAndAssetCtxs
177-
187+
178188
POST /info
179-
189+
180190
Returns:
181191
[
182192
{
183193
universe: [
184194
{
185-
'maxLeverage': int,
186195
'name': str,
187-
'onlyIsolated': bool,
188196
'szDecimals': int
197+
'maxLeverage': int,
198+
'onlyIsolated': bool,
189199
},
190200
...
191201
]
192202
},
193203
[
194204
{
195-
"dayNtlVlm": str,
196-
"funding": str,
197-
"impactPxs": [str, str],
198-
"markPx": str,
199-
"midPx": str,
200-
"openInterest": str,
201-
"oraclePx": str,
202-
"premium": str,
203-
"prevDayPx": str
205+
"dayNtlVlm": float string,
206+
"funding": float string,
207+
"impactPxs": Optional([float string, float string]),
208+
"markPx": Optional(float string),
209+
"midPx": Optional(float string),
210+
"openInterest": float string,
211+
"oraclePx": float string,
212+
"premium": Optional(float string),
213+
"prevDayPx": float string
204214
},
205215
...
206216
]
@@ -216,13 +226,13 @@ def spot_meta(self) -> SpotMeta:
216226
{
217227
universe: [
218228
{
219-
tokens: [int, int],
229+
tokens: [int, int]
220230
name: str,
221231
index: int,
222232
isCanonical: bool
223233
},
224234
...
225-
]
235+
],
226236
tokens: [
227237
{
228238
name: str,
@@ -233,42 +243,45 @@ def spot_meta(self) -> SpotMeta:
233243
isCanonical: bool
234244
},
235245
...
236-
],
246+
]
237247
}
238248
"""
239249
return cast(SpotMeta, self.post("/info", {"type": "spotMeta"}))
240-
250+
241251
def spot_meta_and_asset_ctxs(self) -> SpotMetaAndAssetCtxs:
242252
"""Retrieve exchange spot asset contexts
243-
244253
POST /info
245-
246254
Returns:
247255
[
248256
{
249257
universe: [
250258
{
251-
name: str,
252259
tokens: [int, int]
253-
}
260+
name: str,
261+
index: int,
262+
isCanonical: bool
263+
},
254264
...
255265
],
256266
tokens: [
257267
{
258268
name: str,
259269
szDecimals: int,
260-
weiDecimals int
270+
weiDecimals: int,
271+
index: int,
272+
tokenId: str,
273+
isCanonical: bool
261274
},
262275
...
263276
]
264277
},
265278
[
266279
{
267-
dayNtlVlm: float,
268-
markPx: float,
269-
midPx: float,
270-
prevDayPx: float,
271-
circulatingSupply: float,
280+
dayNtlVlm: float string,
281+
markPx: float string,
282+
midPx: Optional(float string),
283+
prevDayPx: float string,
284+
circulatingSupply: float string,
272285
coin: str
273286
}
274287
...

hyperliquid/utils/types.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
{"name": str, "szDecimals": int, "weiDecimals": int, "index": int, "tokenId": str, "isCanonical": bool},
2929
)
3030
SpotMeta = TypedDict("SpotMeta", {"universe": List[SpotAssetInfo], "tokens": List[SpotTokenInfo]})
31-
32-
SpotAssetCtx = TypedDict("SpotAssetCtx", {"dayNtlVlm": str, "markPx": str, "midPx": Optional[str], "prevDayPx": str, "circulatingSupply": str, "coin": str})
31+
SpotAssetCtx = TypedDict(
32+
"SpotAssetCtx",
33+
{"dayNtlVlm": str, "markPx": str, "midPx": Optional[str], "prevDayPx": str, "circulatingSupply": str, "coin": str},
34+
)
3335
SpotMetaAndAssetCtxs = Tuple[SpotMeta, List[SpotAssetCtx]]
3436

3537
AllMidsSubscription = TypedDict("AllMidsSubscription", {"type": Literal["allMids"]})

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
55

66
[tool.poetry]
77
name = "hyperliquid-python-sdk"
8-
version = "0.4.0"
8+
version = "0.5.0"
99
description = "SDK for Hyperliquid API trading with Python."
1010
readme = "README.md"
1111
authors = ["Hyperliquid <[email protected]>"]

0 commit comments

Comments
 (0)