pub struct DydxWebSocketClient { /* private fields */ }Expand description
WebSocket client for dYdX v4 market data and account streams.
§Authentication
dYdX v4 does not require traditional API key signatures for WebSocket connections. Public channels work without any credentials. Private channels (subaccounts) only need the wallet address included in the subscription message.
The DydxCredential stored in this client is used for:
- Providing the wallet address for private channel subscriptions
- Transaction signing (when placing orders via the validator node)
It is NOT used for WebSocket message signing or authentication.
§Architecture
This client follows a two-layer architecture:
- Outer client (this struct): Orchestrates connection and maintains Python-accessible state
- Inner handler: Owns WebSocketClient exclusively and processes messages in a dedicated task
Communication uses lock-free channels:
- Commands flow from client → handler via
cmd_tx - Parsed events flow from handler → client via
out_rx
Implementations§
Source§impl DydxWebSocketClient
impl DydxWebSocketClient
Sourcepub fn new_public(url: String, _heartbeat: Option<u64>) -> Self
pub fn new_public(url: String, _heartbeat: Option<u64>) -> Self
Creates a new public WebSocket client for market data.
Sourcepub fn new_private(
url: String,
credential: DydxCredential,
account_id: AccountId,
_heartbeat: Option<u64>,
) -> Self
pub fn new_private( url: String, credential: DydxCredential, account_id: AccountId, _heartbeat: Option<u64>, ) -> Self
Creates a new private WebSocket client for account updates.
Sourcepub fn credential(&self) -> Option<&Arc<DydxCredential>>
pub fn credential(&self) -> Option<&Arc<DydxCredential>>
Returns the credential associated with this client, if any.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Returns true when the client is connected.
Sourcepub fn connection_mode_atomic(&self) -> Arc<ArcSwap<AtomicU8>>
pub fn connection_mode_atomic(&self) -> Arc<ArcSwap<AtomicU8>>
Returns a clone of the connection mode atomic reference.
This is primarily used for Python bindings that need to monitor connection state.
Sourcepub fn set_account_id(&mut self, account_id: AccountId)
pub fn set_account_id(&mut self, account_id: AccountId)
Sets the account ID for account message parsing.
Sourcepub fn account_id(&self) -> Option<AccountId>
pub fn account_id(&self) -> Option<AccountId>
Returns the account ID if set.
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Caches a single instrument.
Any existing instrument with the same ID will be replaced.
Sourcepub fn cache_instruments(&self, instruments: Vec<InstrumentAny>)
pub fn cache_instruments(&self, instruments: Vec<InstrumentAny>)
Caches multiple instruments.
Any existing instruments with the same IDs will be replaced.
Sourcepub fn instruments(&self) -> &Arc<DashMap<Ustr, InstrumentAny>>
pub fn instruments(&self) -> &Arc<DashMap<Ustr, InstrumentAny>>
Returns a reference to the instruments cache.
Sourcepub fn get_instrument(&self, symbol: &Ustr) -> Option<InstrumentAny>
pub fn get_instrument(&self, symbol: &Ustr) -> Option<InstrumentAny>
Retrieves an instrument from the cache by symbol.
Returns None if the instrument is not found.
Sourcepub fn take_receiver(&mut self) -> Option<UnboundedReceiver<NautilusWsMessage>>
pub fn take_receiver(&mut self) -> Option<UnboundedReceiver<NautilusWsMessage>>
Takes ownership of the inbound typed message receiver. Returns None if the receiver has already been taken or not connected.
Sourcepub async fn connect(&mut self) -> DydxWsResult<()>
pub async fn connect(&mut self) -> DydxWsResult<()>
Connects the websocket client in handler mode with automatic reconnection.
Spawns a background handler task that owns the WebSocketClient and processes
raw messages into typed NautilusWsMessage values.
§Errors
Returns an error if the connection cannot be established.
Sourcepub async fn disconnect(&mut self) -> DydxWsResult<()>
pub async fn disconnect(&mut self) -> DydxWsResult<()>
Disconnects the websocket client.
§Errors
Returns an error if the underlying client cannot be accessed.
Sourcepub fn send_command(&self, cmd: HandlerCommand) -> DydxWsResult<()>
pub fn send_command(&self, cmd: HandlerCommand) -> DydxWsResult<()>
Sourcepub async fn subscribe_trades(
&self,
instrument_id: InstrumentId,
) -> DydxWsResult<()>
pub async fn subscribe_trades( &self, instrument_id: InstrumentId, ) -> DydxWsResult<()>
Subscribes to public trade updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#trades-channel
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
) -> DydxWsResult<()>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, ) -> DydxWsResult<()>
Unsubscribes from public trade updates for a specific instrument.
§Errors
Returns an error if the unsubscription request fails.
Sourcepub async fn subscribe_orderbook(
&self,
instrument_id: InstrumentId,
) -> DydxWsResult<()>
pub async fn subscribe_orderbook( &self, instrument_id: InstrumentId, ) -> DydxWsResult<()>
Subscribes to orderbook updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#orderbook-channel
Sourcepub async fn unsubscribe_orderbook(
&self,
instrument_id: InstrumentId,
) -> DydxWsResult<()>
pub async fn unsubscribe_orderbook( &self, instrument_id: InstrumentId, ) -> DydxWsResult<()>
Unsubscribes from orderbook updates for a specific instrument.
§Errors
Returns an error if the unsubscription request fails.
Sourcepub async fn subscribe_candles(
&self,
instrument_id: InstrumentId,
resolution: &str,
) -> DydxWsResult<()>
pub async fn subscribe_candles( &self, instrument_id: InstrumentId, resolution: &str, ) -> DydxWsResult<()>
Subscribes to candle/kline updates for a specific instrument.
§Errors
Returns an error if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#candles-channel
Sourcepub async fn unsubscribe_candles(
&self,
instrument_id: InstrumentId,
resolution: &str,
) -> DydxWsResult<()>
pub async fn unsubscribe_candles( &self, instrument_id: InstrumentId, resolution: &str, ) -> DydxWsResult<()>
Unsubscribes from candle/kline updates for a specific instrument.
§Errors
Returns an error if the unsubscription request fails.
Sourcepub async fn subscribe_markets(&self) -> DydxWsResult<()>
pub async fn subscribe_markets(&self) -> DydxWsResult<()>
Subscribes to market updates for all instruments.
§Errors
Returns an error if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#markets-channel
Sourcepub async fn unsubscribe_markets(&self) -> DydxWsResult<()>
pub async fn unsubscribe_markets(&self) -> DydxWsResult<()>
Sourcepub async fn subscribe_subaccount(
&self,
address: &str,
subaccount_number: u32,
) -> DydxWsResult<()>
pub async fn subscribe_subaccount( &self, address: &str, subaccount_number: u32, ) -> DydxWsResult<()>
Subscribes to subaccount updates (orders, fills, positions, balances).
This requires authentication and will only work for private WebSocket clients
created with Self::new_private.
§Errors
Returns an error if the client was not created with credentials or if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#subaccounts-channel
Sourcepub async fn unsubscribe_subaccount(
&self,
address: &str,
subaccount_number: u32,
) -> DydxWsResult<()>
pub async fn unsubscribe_subaccount( &self, address: &str, subaccount_number: u32, ) -> DydxWsResult<()>
Sourcepub async fn subscribe_block_height(&self) -> DydxWsResult<()>
pub async fn subscribe_block_height(&self) -> DydxWsResult<()>
Subscribes to block height updates.
§Errors
Returns an error if the subscription request fails.
§References
https://docs.dydx.trade/developers/indexer/websockets#block-height-channel
Sourcepub async fn unsubscribe_block_height(&self) -> DydxWsResult<()>
pub async fn unsubscribe_block_height(&self) -> DydxWsResult<()>
Unsubscribes from block height updates.
§Errors
Returns an error if the unsubscription request fails.
Trait Implementations§
Source§impl Clone for DydxWebSocketClient
impl Clone for DydxWebSocketClient
Source§impl Debug for DydxWebSocketClient
impl Debug for DydxWebSocketClient
Source§impl<'py> IntoPyObject<'py> for DydxWebSocketClient
impl<'py> IntoPyObject<'py> for DydxWebSocketClient
Source§type Target = DydxWebSocketClient
type Target = DydxWebSocketClient
Source§type Output = Bound<'py, <DydxWebSocketClient as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <DydxWebSocketClient 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 DydxWebSocketClient
impl PyClass for DydxWebSocketClient
Source§impl PyClassImpl for DydxWebSocketClient
impl PyClassImpl for DydxWebSocketClient
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 = /// WebSocket client for dYdX v4 market data and account streams.
///
/// # Authentication
///
/// dYdX v4 does not require traditional API key signatures for WebSocket connections.
/// Public channels work without any credentials. Private channels (subaccounts) only
/// need the wallet address included in the subscription message.
///
/// The [`DydxCredential`] stored in this client is used for:
/// - Providing the wallet address for private channel subscriptions
/// - Transaction signing (when placing orders via the validator node)
///
/// It is **NOT** used for WebSocket message signing or authentication.
///
/// # Architecture
///
/// This client follows a two-layer architecture:
/// - **Outer client** (this struct): Orchestrates connection and maintains Python-accessible state
/// - **Inner handler**: Owns WebSocketClient exclusively and processes messages in a dedicated task
///
/// Communication uses lock-free channels:
/// - Commands flow from client → handler via `cmd_tx`
/// - Parsed events flow from handler → client via `out_rx`
const RAW_DOC: &'static CStr = /// WebSocket client for dYdX v4 market data and account streams. /// /// # Authentication /// /// dYdX v4 does not require traditional API key signatures for WebSocket connections. /// Public channels work without any credentials. Private channels (subaccounts) only /// need the wallet address included in the subscription message. /// /// The [`DydxCredential`] stored in this client is used for: /// - Providing the wallet address for private channel subscriptions /// - Transaction signing (when placing orders via the validator node) /// /// It is **NOT** used for WebSocket message signing or authentication. /// /// # Architecture /// /// This client follows a two-layer architecture: /// - **Outer client** (this struct): Orchestrates connection and maintains Python-accessible state /// - **Inner handler**: Owns WebSocketClient exclusively and processes messages in a dedicated task /// /// Communication uses lock-free channels: /// - Commands flow from client → handler via `cmd_tx` /// - Parsed events flow from handler → client via `out_rx`
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<DydxWebSocketClient>
type ThreadChecker = SendablePyClass<DydxWebSocketClient>
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>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyMethods<DydxWebSocketClient> for PyClassImplCollector<DydxWebSocketClient>
impl PyMethods<DydxWebSocketClient> for PyClassImplCollector<DydxWebSocketClient>
fn py_methods(self) -> &'static PyClassItems
Source§impl PyTypeInfo for DydxWebSocketClient
impl PyTypeInfo for DydxWebSocketClient
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for DydxWebSocketClient
impl ExtractPyClassWithClone for DydxWebSocketClient
Auto Trait Implementations§
impl Freeze for DydxWebSocketClient
impl !RefUnwindSafe for DydxWebSocketClient
impl Send for DydxWebSocketClient
impl Sync for DydxWebSocketClient
impl Unpin for DydxWebSocketClient
impl !UnwindSafe for DydxWebSocketClient
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,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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 more§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§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.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read moreSource§impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
impl<'py, T> IntoPyObjectNautilusExt<'py> for Twhere
T: IntoPyObjectExt<'py>,
Source§fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
fn into_py_any_unwrap(self, py: Python<'py>) -> Py<PyAny>
§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more