pub struct HyperliquidWebSocketClient { /* private fields */ }Expand description
Hyperliquid WebSocket client following the BitMEX pattern.
Orchestrates WebSocket connection and subscriptions using a command-based architecture, where the inner FeedHandler owns the WebSocketClient and handles all I/O.
Implementations§
Source§impl HyperliquidWebSocketClient
impl HyperliquidWebSocketClient
Sourcepub fn new(
url: Option<String>,
testnet: bool,
account_id: Option<AccountId>,
) -> Self
pub fn new( url: Option<String>, testnet: bool, account_id: Option<AccountId>, ) -> Self
Creates a new Hyperliquid WebSocket client without connecting.
If url is None, the appropriate URL will be determined based on the testnet flag:
testnet=false:wss://api.hyperliquid.xyz/wstestnet=true:wss://api.hyperliquid-testnet.xyz/ws
The connection will be established when connect() is called.
Sourcepub async fn connect(&mut self) -> Result<()>
pub async fn connect(&mut self) -> Result<()>
Establishes WebSocket connection and spawns the message handler.
Sourcepub fn take_task_handle(&mut self) -> Option<JoinHandle<()>>
pub fn take_task_handle(&mut self) -> Option<JoinHandle<()>>
Takes the handler task handle from this client so that another instance (e.g., the non-clone original) can await it on disconnect.
pub fn set_task_handle(&mut self, handle: JoinHandle<()>)
Sourcepub async fn disconnect(&mut self) -> Result<()>
pub async fn disconnect(&mut self) -> Result<()>
Disconnects the WebSocket connection.
Sourcepub fn cache_instruments(&mut self, instruments: Vec<InstrumentAny>)
pub fn cache_instruments(&mut self, instruments: Vec<InstrumentAny>)
Caches multiple instruments.
Clears the existing cache first, then adds all provided instruments. Instruments are keyed by their raw_symbol which is unique per instrument:
- Perps use base currency (e.g., “BTC”)
- Spot uses @{pair_index} format (e.g., “@107”) or slash format for PURR
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Caches a single instrument.
Any existing instrument with the same raw_symbol will be replaced.
Sourcepub fn instruments_cache(&self) -> Arc<AtomicMap<Ustr, InstrumentAny>>
pub fn instruments_cache(&self) -> Arc<AtomicMap<Ustr, InstrumentAny>>
Returns a shared reference to the instrument cache.
Sourcepub fn cache_spot_fill_coins(&self, mapping: AHashMap<Ustr, Ustr>)
pub fn cache_spot_fill_coins(&self, mapping: AHashMap<Ustr, Ustr>)
Caches spot fill coin mappings for instrument lookup.
Hyperliquid WebSocket fills for spot use @{pair_index} format (e.g., @107),
while instruments are identified by full symbols (e.g., HYPE-USDC-SPOT).
This mapping allows the handler to look up instruments from spot fills.
Sourcepub fn cache_cloid_mapping(&self, cloid: Ustr, client_order_id: ClientOrderId)
pub fn cache_cloid_mapping(&self, cloid: Ustr, client_order_id: ClientOrderId)
Caches a cloid (hex hash) to client_order_id mapping for order/fill resolution.
The cloid is a keccak256 hash of the client_order_id that Hyperliquid uses internally. This mapping allows WebSocket order status and fill reports to be resolved back to the original client_order_id.
This writes directly to a shared cache that the handler reads from, avoiding any race conditions between caching and WebSocket message processing.
Sourcepub fn remove_cloid_mapping(&self, cloid: &Ustr)
pub fn remove_cloid_mapping(&self, cloid: &Ustr)
Removes a cloid mapping from the cache.
Should be called when an order reaches a terminal state (filled, canceled, expired) to prevent unbounded memory growth in long-running sessions.
Sourcepub fn clear_cloid_cache(&self)
pub fn clear_cloid_cache(&self)
Clears all cloid mappings from the cache.
Useful for cleanup during reconnection or shutdown.
Sourcepub fn cloid_cache_len(&self) -> usize
pub fn cloid_cache_len(&self) -> usize
Returns the number of cloid mappings in the cache.
Sourcepub fn get_cloid_mapping(&self, cloid: &Ustr) -> Option<ClientOrderId>
pub fn get_cloid_mapping(&self, cloid: &Ustr) -> Option<ClientOrderId>
Looks up a client_order_id by its cloid hash.
Returns Some(ClientOrderId) if the mapping exists, None otherwise.
Sourcepub fn get_instrument(&self, id: &InstrumentId) -> Option<InstrumentAny>
pub fn get_instrument(&self, id: &InstrumentId) -> Option<InstrumentAny>
Gets an instrument from the cache by ID.
Searches the cache for a matching instrument ID.
Sourcepub fn get_instrument_by_symbol(&self, symbol: &Ustr) -> Option<InstrumentAny>
pub fn get_instrument_by_symbol(&self, symbol: &Ustr) -> Option<InstrumentAny>
Gets an instrument from the cache by raw_symbol (coin).
Sourcepub fn subscription_count(&self) -> usize
pub fn subscription_count(&self) -> usize
Returns the count of confirmed subscriptions.
Sourcepub fn get_bar_type(&self, coin: &str, interval: &str) -> Option<BarType>
pub fn get_bar_type(&self, coin: &str, interval: &str) -> Option<BarType>
Gets a bar type from the cache by coin and interval.
This looks up the subscription key created when subscribing to bars.
Sourcepub async fn subscribe_book(&self, instrument_id: InstrumentId) -> Result<()>
pub async fn subscribe_book(&self, instrument_id: InstrumentId) -> Result<()>
Subscribe to L2 order book for an instrument.
Sourcepub async fn subscribe_quotes(&self, instrument_id: InstrumentId) -> Result<()>
pub async fn subscribe_quotes(&self, instrument_id: InstrumentId) -> Result<()>
Subscribe to best bid/offer (BBO) quotes for an instrument.
Sourcepub async fn subscribe_trades(&self, instrument_id: InstrumentId) -> Result<()>
pub async fn subscribe_trades(&self, instrument_id: InstrumentId) -> Result<()>
Subscribe to trades for an instrument.
Sourcepub async fn subscribe_mark_prices(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn subscribe_mark_prices( &self, instrument_id: InstrumentId, ) -> Result<()>
Subscribe to mark price updates for an instrument.
Sourcepub async fn subscribe_index_prices(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn subscribe_index_prices( &self, instrument_id: InstrumentId, ) -> Result<()>
Subscribe to index/oracle price updates for an instrument.
Sourcepub async fn subscribe_bars(&self, bar_type: BarType) -> Result<()>
pub async fn subscribe_bars(&self, bar_type: BarType) -> Result<()>
Subscribe to candle/bar data for a specific coin and interval.
Sourcepub async fn subscribe_funding_rates(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn subscribe_funding_rates( &self, instrument_id: InstrumentId, ) -> Result<()>
Subscribe to funding rate updates for an instrument.
Sourcepub async fn subscribe_order_updates(&self, user: &str) -> Result<()>
pub async fn subscribe_order_updates(&self, user: &str) -> Result<()>
Subscribe to order updates for a specific user address.
Sourcepub async fn subscribe_user_events(&self, user: &str) -> Result<()>
pub async fn subscribe_user_events(&self, user: &str) -> Result<()>
Subscribe to user events (fills, funding, liquidations) for a specific user address.
Sourcepub async fn subscribe_user_fills(&self, user: &str) -> Result<()>
pub async fn subscribe_user_fills(&self, user: &str) -> Result<()>
Subscribe to user fills for a specific user address.
Note: This channel is redundant with userEvents which already includes fills.
Prefer using subscribe_user_events or subscribe_all_user_channels instead.
Sourcepub async fn subscribe_all_user_channels(&self, user: &str) -> Result<()>
pub async fn subscribe_all_user_channels(&self, user: &str) -> Result<()>
Subscribe to all user channels (order updates + user events) for convenience.
Note: userEvents already includes fills, so we don’t subscribe to userFills
separately to avoid duplicate fill messages.
Sourcepub async fn unsubscribe_book(&self, instrument_id: InstrumentId) -> Result<()>
pub async fn unsubscribe_book(&self, instrument_id: InstrumentId) -> Result<()>
Unsubscribe from L2 order book for an instrument.
Sourcepub async fn unsubscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn unsubscribe_quotes( &self, instrument_id: InstrumentId, ) -> Result<()>
Unsubscribe from quote ticks for an instrument.
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, ) -> Result<()>
Unsubscribe from trades for an instrument.
Sourcepub async fn unsubscribe_mark_prices(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn unsubscribe_mark_prices( &self, instrument_id: InstrumentId, ) -> Result<()>
Unsubscribe from mark price updates for an instrument.
Sourcepub async fn unsubscribe_index_prices(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn unsubscribe_index_prices( &self, instrument_id: InstrumentId, ) -> Result<()>
Unsubscribe from index/oracle price updates for an instrument.
Sourcepub async fn unsubscribe_bars(&self, bar_type: BarType) -> Result<()>
pub async fn unsubscribe_bars(&self, bar_type: BarType) -> Result<()>
Unsubscribe from candle/bar data.
Sourcepub async fn unsubscribe_funding_rates(
&self,
instrument_id: InstrumentId,
) -> Result<()>
pub async fn unsubscribe_funding_rates( &self, instrument_id: InstrumentId, ) -> Result<()>
Unsubscribe from funding rate updates for an instrument.
Sourcepub async fn next_event(&mut self) -> Option<NautilusWsMessage>
pub async fn next_event(&mut self) -> Option<NautilusWsMessage>
Receives the next message from the WebSocket handler.
Returns None if the handler has disconnected or the receiver was already taken.
Trait Implementations§
Source§impl Clone for HyperliquidWebSocketClient
impl Clone for HyperliquidWebSocketClient
Source§impl Debug for HyperliquidWebSocketClient
impl Debug for HyperliquidWebSocketClient
Source§impl<'a, 'py> FromPyObject<'a, 'py> for HyperliquidWebSocketClientwhere
Self: Clone,
impl<'a, 'py> FromPyObject<'a, 'py> for HyperliquidWebSocketClientwhere
Self: Clone,
Source§impl<'py> IntoPyObject<'py> for HyperliquidWebSocketClient
impl<'py> IntoPyObject<'py> for HyperliquidWebSocketClient
Source§type Target = HyperliquidWebSocketClient
type Target = HyperliquidWebSocketClient
Source§type Output = Bound<'py, <HyperliquidWebSocketClient as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <HyperliquidWebSocketClient as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for HyperliquidWebSocketClient
impl PyClass for HyperliquidWebSocketClient
Source§impl PyClassImpl for HyperliquidWebSocketClient
impl PyClassImpl for HyperliquidWebSocketClient
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = /// Hyperliquid WebSocket client following the BitMEX pattern.
///
/// Orchestrates WebSocket connection and subscriptions using a command-based architecture,
/// where the inner FeedHandler owns the WebSocketClient and handles all I/O.
const RAW_DOC: &'static CStr = /// Hyperliquid WebSocket client following the BitMEX pattern. /// /// Orchestrates WebSocket connection and subscriptions using a command-based architecture, /// where the inner FeedHandler owns the WebSocketClient and handles all I/O.
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type Layout = <<HyperliquidWebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<HyperliquidWebSocketClient>
type Layout = <<HyperliquidWebSocketClient as PyClassImpl>::BaseNativeType as PyClassBaseType>::Layout<HyperliquidWebSocketClient>
Source§type ThreadChecker = NoopThreadChecker
type ThreadChecker = NoopThreadChecker
type Inventory = Pyo3MethodsInventoryForHyperliquidWebSocketClient
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
Source§fn dict_offset() -> Option<PyObjectOffset>
fn dict_offset() -> Option<PyObjectOffset>
Source§fn weaklist_offset() -> Option<PyObjectOffset>
fn weaklist_offset() -> Option<PyObjectOffset>
Source§impl PyClassNewTextSignature for HyperliquidWebSocketClient
impl PyClassNewTextSignature for HyperliquidWebSocketClient
const TEXT_SIGNATURE: &'static str = "(url=None, testnet=False, account_id=None)"
Source§impl PyStubType for HyperliquidWebSocketClient
impl PyStubType for HyperliquidWebSocketClient
Source§fn type_output() -> TypeInfo
fn type_output() -> TypeInfo
Source§fn type_input() -> TypeInfo
fn type_input() -> TypeInfo
Source§impl PyTypeInfo for HyperliquidWebSocketClient
impl PyTypeInfo for HyperliquidWebSocketClient
Source§const NAME: &str = <Self as ::pyo3::PyClass>::NAME
const NAME: &str = <Self as ::pyo3::PyClass>::NAME
prefer using ::type_object(py).name() to get the correct runtime value
Source§const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
const MODULE: Option<&str> = <Self as ::pyo3::impl_::pyclass::PyClassImpl>::MODULE
prefer using ::type_object(py).module() to get the correct runtime value
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
Source§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
impl DerefToPyAny for HyperliquidWebSocketClient
Auto Trait Implementations§
impl Freeze for HyperliquidWebSocketClient
impl !RefUnwindSafe for HyperliquidWebSocketClient
impl Send for HyperliquidWebSocketClient
impl Sync for HyperliquidWebSocketClient
impl Unpin for HyperliquidWebSocketClient
impl UnsafeUnpin for HyperliquidWebSocketClient
impl !UnwindSafe for HyperliquidWebSocketClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
Source§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.Source§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
Source§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
Source§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.