pub struct DeribitWebSocketClient { /* private fields */ }Expand description
WebSocket client for connecting to Deribit.
Implementations§
Source§impl DeribitWebSocketClient
impl DeribitWebSocketClient
Sourcepub fn new(
url: Option<String>,
api_key: Option<String>,
api_secret: Option<String>,
heartbeat_interval: u64,
auth_timeout_secs: Option<u64>,
environment: DeribitEnvironment,
transport_backend: TransportBackend,
proxy_url: Option<String>,
) -> Result<Self>
pub fn new( url: Option<String>, api_key: Option<String>, api_secret: Option<String>, heartbeat_interval: u64, auth_timeout_secs: Option<u64>, environment: DeribitEnvironment, transport_backend: TransportBackend, proxy_url: Option<String>, ) -> Result<Self>
Creates a new DeribitWebSocketClient instance.
Falls back to environment variables if credentials are not provided.
§Errors
Returns an error if only one of api_key or api_secret is provided.
Sourcepub fn with_socket_control(self, control: SocketControl) -> Self
pub fn with_socket_control(self, control: SocketControl) -> Self
Configures socket state reporting and reconnect control.
Sourcepub fn new_public(
environment: DeribitEnvironment,
proxy_url: Option<String>,
) -> Result<Self>
pub fn new_public( environment: DeribitEnvironment, proxy_url: Option<String>, ) -> Result<Self>
Creates a new public (unauthenticated) client.
Does NOT fall back to environment variables for credentials.
§Errors
Returns an error if initialization fails.
Sourcepub fn new_unauthenticated(
url: Option<String>,
heartbeat_interval: u64,
environment: DeribitEnvironment,
) -> Result<Self>
pub fn new_unauthenticated( url: Option<String>, heartbeat_interval: u64, environment: DeribitEnvironment, ) -> Result<Self>
Creates an unauthenticated client with a custom URL.
Does NOT fall back to environment variables for credentials. Useful for testing against mock servers.
§Errors
Returns an error if initialization fails.
Sourcepub fn with_credentials(
environment: DeribitEnvironment,
api_key: Option<String>,
api_secret: Option<String>,
auth_timeout_secs: Option<u64>,
proxy_url: Option<String>,
) -> Result<Self>
pub fn with_credentials( environment: DeribitEnvironment, api_key: Option<String>, api_secret: Option<String>, auth_timeout_secs: Option<u64>, proxy_url: Option<String>, ) -> Result<Self>
Creates an authenticated client with credentials.
Resolves each credential from the provided argument first, falling back
to the environment variable for the given environment:
- Testnet:
DERIBIT_TESTNET_API_KEYandDERIBIT_TESTNET_API_SECRET - Mainnet:
DERIBIT_API_KEYandDERIBIT_API_SECRET
§Errors
Returns an error if neither the argument nor the environment variable provides a credential.
Sourcepub fn environment(&self) -> DeribitEnvironment
pub fn environment(&self) -> DeribitEnvironment
Returns the environment for this client.
Sourcepub fn cancel_all_requests(&self)
pub fn cancel_all_requests(&self)
Cancel all pending WebSocket requests.
Sourcepub fn cancellation_token(&self) -> &CancellationToken
pub fn cancellation_token(&self) -> &CancellationToken
Returns the cancellation token for this client.
Sourcepub async fn wait_until_active(&self, timeout_secs: f64) -> DeribitWsResult<()>
pub async fn wait_until_active(&self, timeout_secs: f64) -> DeribitWsResult<()>
Waits until the client is active or timeout expires.
§Errors
Returns an error if the timeout expires before the client becomes active.
Sourcepub async fn wait_for_subscriptions_confirmed(
&self,
timeout_secs: f64,
) -> DeribitWsResult<()>
pub async fn wait_for_subscriptions_confirmed( &self, timeout_secs: f64, ) -> DeribitWsResult<()>
Waits until all pending subscriptions are confirmed by the server.
§Errors
Returns an error if the timeout expires before all subscriptions are confirmed.
Sourcepub fn cache_instruments(&self, instruments: &[InstrumentAny])
pub fn cache_instruments(&self, instruments: &[InstrumentAny])
Caches instruments for use during message parsing.
Sourcepub fn cache_instrument(&self, instrument: InstrumentAny)
pub fn cache_instrument(&self, instrument: InstrumentAny)
Caches a single instrument.
Sourcepub fn set_option_greeks_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
pub fn set_option_greeks_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
Sets the shared option greeks subscription set for handler-side gating.
Sourcepub fn set_mark_price_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
pub fn set_mark_price_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
Sets the shared mark price subscription set for handler-side gating.
Sourcepub fn set_index_price_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
pub fn set_index_price_subs(&mut self, subs: Arc<AtomicSet<InstrumentId>>)
Sets the shared index price subscription set for handler-side gating.
Sourcepub fn add_mark_price_sub(&self, instrument_id: InstrumentId)
pub fn add_mark_price_sub(&self, instrument_id: InstrumentId)
Registers an instrument for mark price emission from ticker messages.
Sourcepub fn remove_mark_price_sub(&self, instrument_id: &InstrumentId)
pub fn remove_mark_price_sub(&self, instrument_id: &InstrumentId)
Unregisters an instrument from mark price emission.
Sourcepub fn add_index_price_sub(&self, instrument_id: InstrumentId)
pub fn add_index_price_sub(&self, instrument_id: InstrumentId)
Registers an instrument for index price emission from ticker messages.
Sourcepub fn remove_index_price_sub(&self, instrument_id: &InstrumentId)
pub fn remove_index_price_sub(&self, instrument_id: &InstrumentId)
Unregisters an instrument from index price emission.
Sourcepub fn add_option_greeks_sub(&self, instrument_id: InstrumentId)
pub fn add_option_greeks_sub(&self, instrument_id: InstrumentId)
Registers an instrument for option greeks emission from ticker messages.
Sourcepub fn remove_option_greeks_sub(&self, instrument_id: &InstrumentId)
pub fn remove_option_greeks_sub(&self, instrument_id: &InstrumentId)
Unregisters an instrument from option greeks emission.
Sourcepub async fn close(&self) -> DeribitWsResult<()>
pub async fn close(&self) -> DeribitWsResult<()>
Sourcepub fn stream(
&mut self,
) -> DeribitWsResult<impl Stream<Item = NautilusWsMessage> + 'static>
pub fn stream( &mut self, ) -> DeribitWsResult<impl Stream<Item = NautilusWsMessage> + 'static>
Returns a stream of WebSocket messages.
§Errors
Returns an error if called before connect() or if called more than once.
Sourcepub fn has_credentials(&self) -> bool
pub fn has_credentials(&self) -> bool
Returns whether the client has credentials configured.
Sourcepub fn is_authenticated(&self) -> bool
pub fn is_authenticated(&self) -> bool
Returns whether the client is authenticated.
Sourcepub async fn authenticate(
&self,
session_name: Option<&str>,
) -> DeribitWsResult<()>
pub async fn authenticate( &self, session_name: Option<&str>, ) -> DeribitWsResult<()>
Authenticates the WebSocket session with Deribit.
Uses the client_signature grant type with HMAC-SHA256 signature.
This must be called before subscribing to raw data streams.
§Arguments
session_name- Optional session name for session-scoped authentication. When provided, usessession:<name>scope which allows skippingaccess_tokenin subsequent private requests. WhenNone, uses defaultconnectionscope. Recommended to use session scope for order execution compatibility.
§Errors
Returns an error if:
- No credentials are configured
- The authentication request fails
- The authentication times out
Sourcepub async fn authenticate_session(
&self,
session_name: &str,
) -> DeribitWsResult<()>
pub async fn authenticate_session( &self, session_name: &str, ) -> DeribitWsResult<()>
Authenticates with session scope using the provided session name.
Use DERIBIT_DATA_SESSION_NAME for data clients and
DERIBIT_EXECUTION_SESSION_NAME for execution clients.
§Errors
Returns an error if authentication fails.
Sourcepub async fn auth_state(&self) -> Option<AuthState>
pub async fn auth_state(&self) -> Option<AuthState>
Returns the current authentication state containing tokens.
Returns None if not authenticated or tokens haven’t been stored yet.
Sourcepub async fn access_token(&self) -> Option<String>
pub async fn access_token(&self) -> Option<String>
Returns the current access token if available.
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 order/fill reports.
Sourcepub fn set_bars_timestamp_on_close(&mut self, value: bool)
pub fn set_bars_timestamp_on_close(&mut self, value: bool)
Sets whether bar timestamps should use the close time.
When true (default), bar ts_event is set to the bar’s close time.
Sourcepub async fn subscribe_trades(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn subscribe_trades( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Sourcepub async fn unsubscribe_trades(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn unsubscribe_trades( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Unsubscribes from trade updates for an instrument.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_book(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn subscribe_book( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Sourcepub async fn unsubscribe_book(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn unsubscribe_book( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Unsubscribes from order book updates for an instrument.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_book_grouped(
&self,
instrument_id: InstrumentId,
group: &str,
depth: u32,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn subscribe_book_grouped( &self, instrument_id: InstrumentId, group: &str, depth: u32, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Subscribes to grouped (depth-limited) order book updates for an instrument.
Uses the Deribit grouped book channel format: book.{instrument}.{group}.{depth}.{interval}
Depth is normalized to Deribit supported values: 1, 10, or 20.
§Errors
Returns an error if subscription fails or raw is requested without authentication.
Sourcepub async fn unsubscribe_book_grouped(
&self,
instrument_id: InstrumentId,
group: &str,
depth: u32,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn unsubscribe_book_grouped( &self, instrument_id: InstrumentId, group: &str, depth: u32, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Unsubscribes from grouped (depth-limited) order book updates for an instrument.
Depth is normalized to Deribit supported values: 1, 10, or 20.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_ticker(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn subscribe_ticker( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Sourcepub async fn unsubscribe_ticker(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn unsubscribe_ticker( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Unsubscribes from ticker updates for an instrument.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn subscribe_quotes( &self, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Subscribes to quote (best bid/ask) updates for an instrument.
Note: Quote channel does not support interval parameter.
§Errors
Returns an error if subscription fails.
Sourcepub async fn unsubscribe_quotes(
&self,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn unsubscribe_quotes( &self, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Unsubscribes from quote updates for an instrument.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_instrument_status(
&self,
kind: &str,
currency: &str,
) -> DeribitWsResult<()>
pub async fn subscribe_instrument_status( &self, kind: &str, currency: &str, ) -> DeribitWsResult<()>
Subscribes to instrument status changes for lifecycle notifications.
Channel format: instrument.state.{kind}.{currency}
§Errors
Returns an error if subscription fails.
Sourcepub async fn unsubscribe_instrument_status(
&self,
kind: &str,
currency: &str,
) -> DeribitWsResult<()>
pub async fn unsubscribe_instrument_status( &self, kind: &str, currency: &str, ) -> DeribitWsResult<()>
Sourcepub async fn subscribe_volatility_index(
&self,
index_name: &str,
) -> DeribitWsResult<()>
pub async fn subscribe_volatility_index( &self, index_name: &str, ) -> DeribitWsResult<()>
Subscribes to volatility index updates for the given index name.
Channel format: deribit_volatility_index.{index_name}
§Errors
Returns an error if subscription fails.
Sourcepub async fn unsubscribe_volatility_index(
&self,
index_name: &str,
) -> DeribitWsResult<()>
pub async fn unsubscribe_volatility_index( &self, index_name: &str, ) -> DeribitWsResult<()>
Unsubscribes from volatility index updates for the given index name.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_perpetual_interests_rates_updates(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn subscribe_perpetual_interests_rates_updates( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Subscribes to perpetual interest rates updates.
Channel format: perpetual.{instrument_name}.{interval}
§Errors
Returns an error if subscription fails.
Sourcepub async fn unsubscribe_perpetual_interest_rates_updates(
&self,
instrument_id: InstrumentId,
interval: Option<DeribitUpdateInterval>,
) -> DeribitWsResult<()>
pub async fn unsubscribe_perpetual_interest_rates_updates( &self, instrument_id: InstrumentId, interval: Option<DeribitUpdateInterval>, ) -> DeribitWsResult<()>
Sourcepub async fn subscribe_chart(
&self,
instrument_id: InstrumentId,
resolution: &str,
) -> DeribitWsResult<()>
pub async fn subscribe_chart( &self, instrument_id: InstrumentId, resolution: &str, ) -> DeribitWsResult<()>
Sourcepub async fn unsubscribe_chart(
&self,
instrument_id: InstrumentId,
resolution: &str,
) -> DeribitWsResult<()>
pub async fn unsubscribe_chart( &self, instrument_id: InstrumentId, resolution: &str, ) -> DeribitWsResult<()>
Sourcepub async fn subscribe_bars(&self, bar_type: BarType) -> DeribitWsResult<()>
pub async fn subscribe_bars(&self, bar_type: BarType) -> DeribitWsResult<()>
Subscribes to bar updates for an instrument using a BarType specification.
Converts the BarType to the nearest supported Deribit resolution and subscribes to the chart channel.
§Errors
Returns an error if the subscription request fails.
Sourcepub async fn unsubscribe_bars(&self, bar_type: BarType) -> DeribitWsResult<()>
pub async fn unsubscribe_bars(&self, bar_type: BarType) -> DeribitWsResult<()>
Unsubscribes from bar updates for an instrument using a BarType specification.
§Errors
Returns an error if the unsubscription request fails.
Sourcepub async fn subscribe_user_orders(&self) -> DeribitWsResult<()>
pub async fn subscribe_user_orders(&self) -> DeribitWsResult<()>
Subscribes to user order updates for all instruments.
Requires authentication. Subscribes to user.orders.any.any.raw channel.
§Errors
Returns an error if client is not authenticated or subscription fails.
Sourcepub async fn unsubscribe_user_orders(&self) -> DeribitWsResult<()>
pub async fn unsubscribe_user_orders(&self) -> DeribitWsResult<()>
Unsubscribes from user order updates for all instruments.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_user_trades(&self) -> DeribitWsResult<()>
pub async fn subscribe_user_trades(&self) -> DeribitWsResult<()>
Subscribes to user trade/fill updates for all instruments.
Requires authentication. Subscribes to user.trades.any.any.raw channel.
§Errors
Returns an error if client is not authenticated or subscription fails.
Sourcepub async fn unsubscribe_user_trades(&self) -> DeribitWsResult<()>
pub async fn unsubscribe_user_trades(&self) -> DeribitWsResult<()>
Unsubscribes from user trade/fill updates for all instruments.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn subscribe_user_portfolio(&self) -> DeribitWsResult<()>
pub async fn subscribe_user_portfolio(&self) -> DeribitWsResult<()>
Subscribes to user portfolio updates for all currencies.
Requires authentication. Subscribes to user.portfolio.any channel which
provides real-time account balance and margin updates for all currencies
(BTC, ETH, USDC, USDT, etc.).
§Errors
Returns an error if client is not authenticated or subscription fails.
Sourcepub async fn unsubscribe_user_portfolio(&self) -> DeribitWsResult<()>
pub async fn unsubscribe_user_portfolio(&self) -> DeribitWsResult<()>
Unsubscribes from user portfolio updates for all currencies.
§Errors
Returns an error if unsubscription fails.
Sourcepub async fn unsubscribe(&self, channels: Vec<String>) -> DeribitWsResult<()>
pub async fn unsubscribe(&self, channels: Vec<String>) -> DeribitWsResult<()>
Sourcepub async fn submit_order(
&self,
order_side: OrderSide,
params: DeribitOrderParams,
client_order_id: ClientOrderId,
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn submit_order( &self, order_side: OrderSide, params: DeribitOrderParams, client_order_id: ClientOrderId, trader_id: TraderId, strategy_id: StrategyId, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Submits an order to Deribit via WebSocket.
Routes to private/buy or private/sell JSON-RPC method based on order side.
Requires authentication (call authenticate_session() first).
§Errors
Returns an error if:
- The client is not authenticated
- The command fails to send
Sourcepub async fn modify_order(
&self,
order_id: &str,
quantity: Quantity,
price: Price,
client_order_id: ClientOrderId,
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn modify_order( &self, order_id: &str, quantity: Quantity, price: Price, client_order_id: ClientOrderId, trader_id: TraderId, strategy_id: StrategyId, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Modifies an existing order on Deribit via WebSocket.
The order parameters are sent using the private/edit JSON-RPC method.
Requires authentication (call authenticate_session() first).
§Errors
Returns an error if:
- The client is not authenticated
- The command fails to send
Sourcepub async fn cancel_order(
&self,
order_id: &str,
client_order_id: ClientOrderId,
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn cancel_order( &self, order_id: &str, client_order_id: ClientOrderId, trader_id: TraderId, strategy_id: StrategyId, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Cancels an existing order on Deribit via WebSocket.
The order is cancelled using the private/cancel JSON-RPC method.
Requires authentication (call authenticate_session() first).
§Errors
Returns an error if:
- The client is not authenticated
- The command fails to send
Sourcepub async fn cancel_all_orders(
&self,
instrument_id: InstrumentId,
order_type: Option<String>,
) -> DeribitWsResult<()>
pub async fn cancel_all_orders( &self, instrument_id: InstrumentId, order_type: Option<String>, ) -> DeribitWsResult<()>
Cancels all orders for a specific instrument on Deribit via WebSocket.
Uses the private/cancel_all_by_instrument JSON-RPC method.
Requires authentication (call authenticate_session() first).
§Errors
Returns an error if:
- The client is not authenticated
- The command fails to send
Sourcepub async fn query_order(
&self,
order_id: &str,
client_order_id: ClientOrderId,
trader_id: TraderId,
strategy_id: StrategyId,
instrument_id: InstrumentId,
) -> DeribitWsResult<()>
pub async fn query_order( &self, order_id: &str, client_order_id: ClientOrderId, trader_id: TraderId, strategy_id: StrategyId, instrument_id: InstrumentId, ) -> DeribitWsResult<()>
Queries the state of an order on Deribit via WebSocket.
Uses the private/get_order_state JSON-RPC method.
Requires authentication (call authenticate_session() first).
§Errors
Returns an error if:
- The client is not authenticated
- The command fails to send
Trait Implementations§
Source§impl Clone for DeribitWebSocketClient
impl Clone for DeribitWebSocketClient
Source§fn clone(&self) -> DeribitWebSocketClient
fn clone(&self) -> DeribitWebSocketClient
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !Freeze for DeribitWebSocketClient
impl !RefUnwindSafe for DeribitWebSocketClient
impl !UnwindSafe for DeribitWebSocketClient
impl Send for DeribitWebSocketClient
impl Sync for DeribitWebSocketClient
impl Unpin for DeribitWebSocketClient
impl UnsafeUnpin for DeribitWebSocketClient
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
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