Skip to content

Commit 7588c0a

Browse files
committed
Add active asset data helper
1 parent 7ee976d commit 7588c0a

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

hyperliquid/info.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from hyperliquid.api import API
22
from hyperliquid.utils.types import (
3+
ActiveAssetData,
34
Any,
45
Callable,
56
Cloid,
@@ -127,6 +128,32 @@ def user_state(self, address: str, dex: str = "") -> Any:
127128
"""
128129
return self.post("/info", {"type": "clearinghouseState", "user": address, "dex": dex})
129130

131+
def active_asset_data(self, user: str, coin: str) -> ActiveAssetData:
132+
"""Retrieve a user's active data for a specific asset.
133+
134+
POST /info
135+
136+
Args:
137+
user (str): Onchain address in 42-character hexadecimal format;
138+
e.g. 0x0000000000000000000000000000000000000000.
139+
coin (str): Perp coin name, e.g. "ETH".
140+
141+
Returns:
142+
{
143+
user: str,
144+
coin: str,
145+
leverage: {
146+
type: "cross" | "isolated",
147+
value: int,
148+
rawUsd: float string # only if type is "isolated"
149+
},
150+
maxTradeSzs: [float string, float string],
151+
availableToTrade: [float string, float string],
152+
markPx: float string,
153+
}
154+
"""
155+
return cast(ActiveAssetData, self.post("/info", {"type": "activeAssetData", "user": user, "coin": coin}))
156+
130157
def spot_user_state(self, address: str) -> Any:
131158
return self.post("/info", {"type": "spotClearinghouseState", "user": address})
132159

tests/info_test.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ def test_get_user_state():
1515
assert response["marginSummary"]["accountValue"] == "1182.312496"
1616

1717

18+
def test_active_asset_data_posts_expected_payload(monkeypatch):
19+
info = Info(skip_ws=True, meta=TEST_META, spot_meta=TEST_SPOT_META)
20+
user = "0x0000000000000000000000000000000000000000"
21+
expected_response = {
22+
"user": user,
23+
"coin": "ETH",
24+
"leverage": {"type": "cross", "value": 20},
25+
"maxTradeSzs": ("1.0", "2.0"),
26+
"availableToTrade": ("3.0", "4.0"),
27+
"markPx": "2000.0",
28+
}
29+
posted = {}
30+
31+
def fake_post(url_path, payload):
32+
posted["url_path"] = url_path
33+
posted["payload"] = payload
34+
return expected_response
35+
36+
monkeypatch.setattr(info, "post", fake_post)
37+
38+
response = info.active_asset_data(user, "ETH")
39+
40+
assert response == expected_response
41+
assert posted == {"url_path": "/info", "payload": {"type": "activeAssetData", "user": user, "coin": "ETH"}}
42+
43+
1844
@pytest.mark.vcr()
1945
def test_get_open_orders():
2046
info = Info(skip_ws=True, meta=TEST_META, spot_meta=TEST_SPOT_META)

0 commit comments

Comments
 (0)