Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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 only


if __name__ == "__main__":
Expand Down
33 changes: 33 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,22 @@
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})
CrossLeverage = TypedDict(
"CrossLeverage",
{
"type": Literal["cross"],
"value": int,
},
)
IsolatedLeverage = TypedDict(
"IsolatedLeverage",
{
"type": Literal["isolated"],
"value": int,
"rawUsd": str,
},
)
Leverage = Union[CrossLeverage, IsolatedLeverage]
TradesMsg = TypedDict("TradesMsg", {"channel": Literal["trades"], "data": List[Trade]})
PerpAssetCtx = TypedDict(
"PerpAssetCtx",
Expand All @@ -97,6 +117,18 @@
ActiveSpotAssetCtxMsg = TypedDict(
"ActiveSpotAssetCtxMsg", {"channel": Literal["activeSpotAssetCtx"], "data": ActiveSpotAssetCtx}
)
ActiveAssetData = TypedDict(
"ActiveAssetData",
{
"user": str,
"coin": str,
"leverage": Leverage,
"maxTradeSzs": Tuple[str, str],
"availableToTrade": Tuple[str, str],
"markPx": str,
},
)
ActiveAssetDataMsg = TypedDict("ActiveAssetDataMsg", {"channel": Literal["activeAssetData"], "data": ActiveAssetData})
Fill = TypedDict(
"Fill",
{
Expand Down Expand Up @@ -146,6 +178,7 @@
OtherWsMsg,
ActiveAssetCtxMsg,
ActiveSpotAssetCtxMsg,
ActiveAssetDataMsg,
]

# 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
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()},{subscription["user"]}'


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()},{ws_msg["data"]["user"]}'


class WebsocketManager(threading.Thread):
Expand Down