Skip to content

Commit a4280d0

Browse files
authored
Added activeAssetCtx websocket subscription (#165)
1 parent a8edca1 commit a4280d0

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

examples/basic_ws.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def main():
1818
info.subscribe({"type": "userNonFundingLedgerUpdates", "user": address}, print)
1919
info.subscribe({"type": "webData2", "user": address}, print)
2020
info.subscribe({"type": "bbo", "coin": "ETH"}, print)
21+
info.subscribe({"type": "activeAssetCtx", "coin": "BTC"}, print) # Perp
22+
info.subscribe({"type": "activeAssetCtx", "coin": "@1"}, print) # Spot
2123

2224

2325
if __name__ == "__main__":

hyperliquid/info.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ def _remap_coin_subscription(self, subscription: Subscription) -> None:
604604
or subscription["type"] == "trades"
605605
or subscription["type"] == "candle"
606606
or subscription["type"] == "bbo"
607+
or subscription["type"] == "activeAssetCtx"
607608
):
608609
subscription["coin"] = self.name_to_coin[subscription["coin"]]
609610

hyperliquid/utils/types.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"UserNonFundingLedgerUpdatesSubscription", {"type": Literal["userNonFundingLedgerUpdates"], "user": str}
5050
)
5151
WebData2Subscription = TypedDict("WebData2Subscription", {"type": Literal["webData2"], "user": str})
52+
ActiveAssetCtxSubscription = TypedDict("ActiveAssetCtxSubscription", {"type": Literal["activeAssetCtx"], "coin": str})
5253
# If adding new subscription types that contain coin's don't forget to handle automatically rewrite name to coin in info.subscribe
5354
Subscription = Union[
5455
AllMidsSubscription,
@@ -62,6 +63,7 @@
6263
UserFundingsSubscription,
6364
UserNonFundingLedgerUpdatesSubscription,
6465
WebData2Subscription,
66+
ActiveAssetCtxSubscription,
6567
]
6668

6769
AllMidsData = TypedDict("AllMidsData", {"mids": Dict[str, str]})
@@ -74,6 +76,27 @@
7476
PongMsg = TypedDict("PongMsg", {"channel": Literal["pong"]})
7577
Trade = TypedDict("Trade", {"coin": str, "side": Side, "px": str, "sz": int, "hash": str, "time": int})
7678
TradesMsg = TypedDict("TradesMsg", {"channel": Literal["trades"], "data": List[Trade]})
79+
PerpAssetCtx = TypedDict(
80+
"PerpAssetCtx",
81+
{
82+
"funding": str,
83+
"openInterest": str,
84+
"prevDayPx": str,
85+
"dayNtlVlm": str,
86+
"premium": str,
87+
"oraclePx": str,
88+
"markPx": str,
89+
"midPx": Optional[str],
90+
"impactPxs": Optional[Tuple[str, str]],
91+
"dayBaseVlm": str,
92+
},
93+
)
94+
ActiveAssetCtx = TypedDict("ActiveAssetCtx", {"coin": str, "ctx": PerpAssetCtx})
95+
ActiveSpotAssetCtx = TypedDict("ActiveSpotAssetCtx", {"coin": str, "ctx": SpotAssetCtx})
96+
ActiveAssetCtxMsg = TypedDict("ActiveAssetCtxMsg", {"channel": Literal["activeAssetCtx"], "data": ActiveAssetCtx})
97+
ActiveSpotAssetCtxMsg = TypedDict(
98+
"ActiveSpotAssetCtxMsg", {"channel": Literal["activeSpotAssetCtx"], "data": ActiveSpotAssetCtx}
99+
)
77100
Fill = TypedDict(
78101
"Fill",
79102
{
@@ -112,7 +135,18 @@
112135
},
113136
total=False,
114137
)
115-
WsMsg = Union[AllMidsMsg, BboMsg, L2BookMsg, TradesMsg, UserEventsMsg, PongMsg, UserFillsMsg, OtherWsMsg]
138+
WsMsg = Union[
139+
AllMidsMsg,
140+
BboMsg,
141+
L2BookMsg,
142+
TradesMsg,
143+
UserEventsMsg,
144+
PongMsg,
145+
UserFillsMsg,
146+
OtherWsMsg,
147+
ActiveAssetCtxMsg,
148+
ActiveSpotAssetCtxMsg,
149+
]
116150

117151
# 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
118152
BuilderInfo = TypedDict("BuilderInfo", {"b": str, "f": int})

hyperliquid/websocket_manager.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ def subscription_to_identifier(subscription: Subscription) -> str:
3333
return f'webData2:{subscription["user"].lower()}'
3434
elif subscription["type"] == "bbo":
3535
return f'bbo:{subscription["coin"].lower()}'
36+
elif subscription["type"] == "activeAssetCtx":
37+
return f'activeAssetCtx:{subscription["coin"].lower()}'
3638

3739

3840
def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
@@ -64,6 +66,8 @@ def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
6466
return f'webData2:{ws_msg["data"]["user"].lower()}'
6567
elif ws_msg["channel"] == "bbo":
6668
return f'bbo:{ws_msg["data"]["coin"].lower()}'
69+
elif ws_msg["channel"] == "activeAssetCtx" or ws_msg["channel"] == "activeSpotAssetCtx":
70+
return f'activeAssetCtx:{ws_msg["data"]["coin"].lower()}'
6771

6872

6973
class WebsocketManager(threading.Thread):

0 commit comments

Comments
 (0)