Skip to content

Commit d09e538

Browse files
authored
Add activeAssetData websocket subscription (#168)
1 parent a4280d0 commit d09e538

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

examples/basic_ws.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def main():
2020
info.subscribe({"type": "bbo", "coin": "ETH"}, print)
2121
info.subscribe({"type": "activeAssetCtx", "coin": "BTC"}, print) # Perp
2222
info.subscribe({"type": "activeAssetCtx", "coin": "@1"}, print) # Spot
23+
info.subscribe({"type": "activeAssetData", "user": address, "coin": "BTC"}, print) # Perp only
2324

2425

2526
if __name__ == "__main__":

hyperliquid/utils/types.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
)
5151
WebData2Subscription = TypedDict("WebData2Subscription", {"type": Literal["webData2"], "user": str})
5252
ActiveAssetCtxSubscription = TypedDict("ActiveAssetCtxSubscription", {"type": Literal["activeAssetCtx"], "coin": str})
53+
ActiveAssetDataSubscription = TypedDict(
54+
"ActiveAssetDataSubscription", {"type": Literal["activeAssetData"], "user": str, "coin": str}
55+
)
5356
# If adding new subscription types that contain coin's don't forget to handle automatically rewrite name to coin in info.subscribe
5457
Subscription = Union[
5558
AllMidsSubscription,
@@ -64,6 +67,7 @@
6467
UserNonFundingLedgerUpdatesSubscription,
6568
WebData2Subscription,
6669
ActiveAssetCtxSubscription,
70+
ActiveAssetDataSubscription,
6771
]
6872

6973
AllMidsData = TypedDict("AllMidsData", {"mids": Dict[str, str]})
@@ -75,6 +79,22 @@
7579
BboMsg = TypedDict("BboMsg", {"channel": Literal["bbo"], "data": BboData})
7680
PongMsg = TypedDict("PongMsg", {"channel": Literal["pong"]})
7781
Trade = TypedDict("Trade", {"coin": str, "side": Side, "px": str, "sz": int, "hash": str, "time": int})
82+
CrossLeverage = TypedDict(
83+
"CrossLeverage",
84+
{
85+
"type": Literal["cross"],
86+
"value": int,
87+
},
88+
)
89+
IsolatedLeverage = TypedDict(
90+
"IsolatedLeverage",
91+
{
92+
"type": Literal["isolated"],
93+
"value": int,
94+
"rawUsd": str,
95+
},
96+
)
97+
Leverage = Union[CrossLeverage, IsolatedLeverage]
7898
TradesMsg = TypedDict("TradesMsg", {"channel": Literal["trades"], "data": List[Trade]})
7999
PerpAssetCtx = TypedDict(
80100
"PerpAssetCtx",
@@ -97,6 +117,18 @@
97117
ActiveSpotAssetCtxMsg = TypedDict(
98118
"ActiveSpotAssetCtxMsg", {"channel": Literal["activeSpotAssetCtx"], "data": ActiveSpotAssetCtx}
99119
)
120+
ActiveAssetData = TypedDict(
121+
"ActiveAssetData",
122+
{
123+
"user": str,
124+
"coin": str,
125+
"leverage": Leverage,
126+
"maxTradeSzs": Tuple[str, str],
127+
"availableToTrade": Tuple[str, str],
128+
"markPx": str,
129+
},
130+
)
131+
ActiveAssetDataMsg = TypedDict("ActiveAssetDataMsg", {"channel": Literal["activeAssetData"], "data": ActiveAssetData})
100132
Fill = TypedDict(
101133
"Fill",
102134
{
@@ -146,6 +178,7 @@
146178
OtherWsMsg,
147179
ActiveAssetCtxMsg,
148180
ActiveSpotAssetCtxMsg,
181+
ActiveAssetDataMsg,
149182
]
150183

151184
# b is the public address of the builder, f is the amount of the fee in tenths of basis points. e.g. 10 means 1 basis point

hyperliquid/websocket_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ def subscription_to_identifier(subscription: Subscription) -> str:
3535
return f'bbo:{subscription["coin"].lower()}'
3636
elif subscription["type"] == "activeAssetCtx":
3737
return f'activeAssetCtx:{subscription["coin"].lower()}'
38+
elif subscription["type"] == "activeAssetData":
39+
return f'activeAssetData:{subscription["coin"].lower()},{subscription["user"].lower()}'
3840

3941

4042
def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
@@ -68,6 +70,8 @@ def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
6870
return f'bbo:{ws_msg["data"]["coin"].lower()}'
6971
elif ws_msg["channel"] == "activeAssetCtx" or ws_msg["channel"] == "activeSpotAssetCtx":
7072
return f'activeAssetCtx:{ws_msg["data"]["coin"].lower()}'
73+
elif ws_msg["channel"] == "activeAssetData":
74+
return f'activeAssetData:{ws_msg["data"]["coin"].lower()},{ws_msg["data"]["user"].lower()}'
7175

7276

7377
class WebsocketManager(threading.Thread):

0 commit comments

Comments
 (0)