Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/basic_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def main():
info.subscribe({"type": "bbo", "coin": "ETH"}, print)
info.subscribe({"type": "activeAssetCtx", "coin": "BTC"}, print) # Perp
info.subscribe({"type": "activeAssetCtx", "coin": "@1"}, print) # Spot
info.subscribe({"type": "activeAssetData", "user": address, "coin": "BTC"}, print) # Perp


if __name__ == "__main__":
Expand Down
24 changes: 24 additions & 0 deletions hyperliquid/utils/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
)
WebData2Subscription = TypedDict("WebData2Subscription", {"type": Literal["webData2"], "user": str})
ActiveAssetCtxSubscription = TypedDict("ActiveAssetCtxSubscription", {"type": Literal["activeAssetCtx"], "coin": str})
ActiveAssetDataSubscription = TypedDict(
"ActiveAssetDataSubscription", {"type": Literal["activeAssetData"], "user": str, "coin": str}
)
# If adding new subscription types that contain coin's don't forget to handle automatically rewrite name to coin in info.subscribe
Subscription = Union[
AllMidsSubscription,
Expand All @@ -64,6 +67,7 @@
UserNonFundingLedgerUpdatesSubscription,
WebData2Subscription,
ActiveAssetCtxSubscription,
ActiveAssetDataSubscription,
]

AllMidsData = TypedDict("AllMidsData", {"mids": Dict[str, str]})
Expand All @@ -75,6 +79,14 @@
BboMsg = TypedDict("BboMsg", {"channel": Literal["bbo"], "data": BboData})
PongMsg = TypedDict("PongMsg", {"channel": Literal["pong"]})
Trade = TypedDict("Trade", {"coin": str, "side": Side, "px": str, "sz": int, "hash": str, "time": int})
Leverage = TypedDict(
"Leverage",
{
"type": Union[Literal["cross"], Literal["isolated"]],
"value": int,
"rawUsd": Optional[str],
},
)
TradesMsg = TypedDict("TradesMsg", {"channel": Literal["trades"], "data": List[Trade]})
PerpAssetCtx = TypedDict(
"PerpAssetCtx",
Expand All @@ -97,6 +109,18 @@
ActiveSpotAssetCtxMsg = TypedDict(
"ActiveSpotAssetCtxMsg", {"channel": Literal["activeSpotAssetCtx"], "data": ActiveSpotAssetCtx}
)
ActiveAssetData = TypedDict(
"ActiveAssetData",
{
"user": str,
"coin": str,
"leverage": Leverage,
"maxTradeSzs": List[str],
"availableToTrade": List[str],
"markPx": str,
},
)
ActiveAssetDataMsg = TypedDict("ActiveAssetDataMsg", {"channel": Literal["activeAssetData"], "data": ActiveAssetData})
Fill = TypedDict(
"Fill",
{
Expand Down
4 changes: 4 additions & 0 deletions hyperliquid/websocket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def subscription_to_identifier(subscription: Subscription) -> str:
return f'bbo:{subscription["coin"].lower()}'
elif subscription["type"] == "activeAssetCtx":
return f'activeAssetCtx:{subscription["coin"].lower()}'
elif subscription["type"] == "activeAssetData":
return f'activeAssetData:{subscription["coin"].lower()}'


def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
Expand Down Expand Up @@ -68,6 +70,8 @@ def ws_msg_to_identifier(ws_msg: WsMsg) -> Optional[str]:
return f'bbo:{ws_msg["data"]["coin"].lower()}'
elif ws_msg["channel"] == "activeAssetCtx" or ws_msg["channel"] == "activeSpotAssetCtx":
return f'activeAssetCtx:{ws_msg["data"]["coin"].lower()}'
elif ws_msg["channel"] == "activeAssetData":
return f'activeAssetData:{ws_msg["data"]["coin"].lower()}'


class WebsocketManager(threading.Thread):
Expand Down
Loading