pub struct DeribitWebSocketClient {
pub config: Arc<WebSocketConfig>,
pub session: Arc<WebSocketSession>,
/* private fields */
}Expand description
WebSocket client for Deribit
Fields§
§config: Arc<WebSocketConfig>WebSocket configuration
session: Arc<WebSocketSession>WebSocket session
Implementations§
Source§impl DeribitWebSocketClient
impl DeribitWebSocketClient
Sourcepub fn new(config: &WebSocketConfig) -> Result<Self, WebSocketError>
pub fn new(config: &WebSocketConfig) -> Result<Self, WebSocketError>
Create a new WebSocket client
Sourcepub fn new_with_url(ws_url: String) -> Result<Self, WebSocketError>
pub fn new_with_url(ws_url: String) -> Result<Self, WebSocketError>
Create a new WebSocket client with default configuration
Sourcepub fn new_testnet() -> Result<Self, WebSocketError>
pub fn new_testnet() -> Result<Self, WebSocketError>
Create a new WebSocket client for testnet
Sourcepub fn new_production() -> Result<Self, WebSocketError>
pub fn new_production() -> Result<Self, WebSocketError>
Create a new WebSocket client for production
Sourcepub async fn connect(&self) -> Result<(), WebSocketError>
pub async fn connect(&self) -> Result<(), WebSocketError>
Connect to the WebSocket server
Sourcepub async fn disconnect(&self) -> Result<(), WebSocketError>
pub async fn disconnect(&self) -> Result<(), WebSocketError>
Disconnect from the WebSocket server
Sourcepub async fn is_connected(&self) -> bool
pub async fn is_connected(&self) -> bool
Check if connected
Sourcepub async fn authenticate(
&self,
client_id: &str,
client_secret: &str,
) -> Result<AuthResponse, WebSocketError>
pub async fn authenticate( &self, client_id: &str, client_secret: &str, ) -> Result<AuthResponse, WebSocketError>
Authenticate with the server
Authenticates the connection using API credentials and returns authentication details including access token and refresh token.
§Arguments
client_id- API client IDclient_secret- API client secret
§Returns
Returns AuthResponse containing access token, token type, expiration, and scope
§Errors
Returns an error if authentication fails or credentials are invalid
Sourcepub async fn subscribe(
&self,
channels: Vec<String>,
) -> Result<JsonRpcResponse, WebSocketError>
pub async fn subscribe( &self, channels: Vec<String>, ) -> Result<JsonRpcResponse, WebSocketError>
Subscribe to channels
Sourcepub async fn unsubscribe(
&self,
channels: Vec<String>,
) -> Result<JsonRpcResponse, WebSocketError>
pub async fn unsubscribe( &self, channels: Vec<String>, ) -> Result<JsonRpcResponse, WebSocketError>
Unsubscribe from channels
Sourcepub async fn public_unsubscribe_all(&self) -> Result<String, WebSocketError>
pub async fn public_unsubscribe_all(&self) -> Result<String, WebSocketError>
Sourcepub async fn private_unsubscribe_all(&self) -> Result<String, WebSocketError>
pub async fn private_unsubscribe_all(&self) -> Result<String, WebSocketError>
Sourcepub async fn send_request(
&self,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse, WebSocketError>
pub async fn send_request( &self, request: JsonRpcRequest, ) -> Result<JsonRpcResponse, WebSocketError>
Send a JSON-RPC request
Sourcepub async fn send_message(&self, message: String) -> Result<(), WebSocketError>
pub async fn send_message(&self, message: String) -> Result<(), WebSocketError>
Send a raw message
Sourcepub async fn receive_message(&self) -> Result<String, WebSocketError>
pub async fn receive_message(&self) -> Result<String, WebSocketError>
Receive a message
Sourcepub async fn get_subscriptions(&self) -> Vec<String>
pub async fn get_subscriptions(&self) -> Vec<String>
Get active subscriptions
Sourcepub async fn test_connection(&self) -> Result<TestResponse, WebSocketError>
pub async fn test_connection(&self) -> Result<TestResponse, WebSocketError>
Sourcepub async fn get_time(&self) -> Result<u64, WebSocketError>
pub async fn get_time(&self) -> Result<u64, WebSocketError>
Sourcepub async fn set_heartbeat(
&self,
interval: u64,
) -> Result<String, WebSocketError>
pub async fn set_heartbeat( &self, interval: u64, ) -> Result<String, WebSocketError>
Enable heartbeat with specified interval
The server will send a heartbeat message every interval seconds.
If heartbeat is enabled, the server will also send test_request notifications
which the client should respond to with public/test to keep the connection alive.
§Arguments
interval- Heartbeat interval in seconds (10-3600)
§Returns
Returns "ok" on success
§Errors
Returns an error if the request fails or the interval is invalid
Sourcepub async fn disable_heartbeat(&self) -> Result<String, WebSocketError>
pub async fn disable_heartbeat(&self) -> Result<String, WebSocketError>
Sourcepub async fn hello(
&self,
client_name: &str,
client_version: &str,
) -> Result<HelloResponse, WebSocketError>
pub async fn hello( &self, client_name: &str, client_version: &str, ) -> Result<HelloResponse, WebSocketError>
Send client identification to the server
This method identifies the client to the server with its name and version. It’s recommended to call this after connecting to provide debugging information.
§Arguments
client_name- Name of the client applicationclient_version- Version of the client application
§Returns
Returns HelloResponse containing the API version information
§Errors
Returns an error if the request fails
Sourcepub async fn enable_cancel_on_disconnect(
&self,
) -> Result<String, WebSocketError>
pub async fn enable_cancel_on_disconnect( &self, ) -> Result<String, WebSocketError>
Enable automatic order cancellation on disconnect
When enabled, all open orders will be automatically cancelled if the WebSocket connection is lost. This is a safety feature to prevent unintended order execution when the client loses connectivity.
§Returns
Returns "ok" on success
§Errors
Returns an error if the request fails or requires authentication
Sourcepub async fn disable_cancel_on_disconnect(
&self,
) -> Result<String, WebSocketError>
pub async fn disable_cancel_on_disconnect( &self, ) -> Result<String, WebSocketError>
Sourcepub async fn get_cancel_on_disconnect(&self) -> Result<bool, WebSocketError>
pub async fn get_cancel_on_disconnect(&self) -> Result<bool, WebSocketError>
Sourcepub async fn mass_quote(
&self,
request: MassQuoteRequest,
) -> Result<MassQuoteResult, WebSocketError>
pub async fn mass_quote( &self, request: MassQuoteRequest, ) -> Result<MassQuoteResult, WebSocketError>
Place mass quotes
Sourcepub async fn cancel_quotes(
&self,
request: CancelQuotesRequest,
) -> Result<CancelQuotesResponse, WebSocketError>
pub async fn cancel_quotes( &self, request: CancelQuotesRequest, ) -> Result<CancelQuotesResponse, WebSocketError>
Cancel quotes
Sourcepub async fn set_mmp_config(
&self,
config: MmpGroupConfig,
) -> Result<(), WebSocketError>
pub async fn set_mmp_config( &self, config: MmpGroupConfig, ) -> Result<(), WebSocketError>
Set MMP group configuration
Sourcepub async fn get_mmp_config(
&self,
mmp_group: Option<String>,
) -> Result<Vec<MmpGroupConfig>, WebSocketError>
pub async fn get_mmp_config( &self, mmp_group: Option<String>, ) -> Result<Vec<MmpGroupConfig>, WebSocketError>
Get MMP group configuration
Sourcepub async fn reset_mmp(
&self,
mmp_group: Option<String>,
) -> Result<(), WebSocketError>
pub async fn reset_mmp( &self, mmp_group: Option<String>, ) -> Result<(), WebSocketError>
Reset MMP group
Sourcepub async fn get_open_orders(
&self,
currency: Option<String>,
kind: Option<String>,
type_filter: Option<String>,
) -> Result<Vec<QuoteInfo>, WebSocketError>
pub async fn get_open_orders( &self, currency: Option<String>, kind: Option<String>, type_filter: Option<String>, ) -> Result<Vec<QuoteInfo>, WebSocketError>
Get open orders (including quotes)
Sourcepub async fn buy(
&self,
request: OrderRequest,
) -> Result<OrderResponse, WebSocketError>
pub async fn buy( &self, request: OrderRequest, ) -> Result<OrderResponse, WebSocketError>
Sourcepub async fn sell(
&self,
request: OrderRequest,
) -> Result<OrderResponse, WebSocketError>
pub async fn sell( &self, request: OrderRequest, ) -> Result<OrderResponse, WebSocketError>
Sourcepub async fn cancel_all(&self) -> Result<u32, WebSocketError>
pub async fn cancel_all(&self) -> Result<u32, WebSocketError>
Sourcepub async fn cancel_all_by_currency(
&self,
currency: &str,
) -> Result<u32, WebSocketError>
pub async fn cancel_all_by_currency( &self, currency: &str, ) -> Result<u32, WebSocketError>
Sourcepub async fn cancel_all_by_instrument(
&self,
instrument_name: &str,
) -> Result<u32, WebSocketError>
pub async fn cancel_all_by_instrument( &self, instrument_name: &str, ) -> Result<u32, WebSocketError>
Sourcepub async fn edit(
&self,
request: EditOrderRequest,
) -> Result<OrderResponse, WebSocketError>
pub async fn edit( &self, request: EditOrderRequest, ) -> Result<OrderResponse, WebSocketError>
Sourcepub async fn get_positions(
&self,
currency: Option<&str>,
kind: Option<&str>,
) -> Result<Vec<Position>, WebSocketError>
pub async fn get_positions( &self, currency: Option<&str>, kind: Option<&str>, ) -> Result<Vec<Position>, WebSocketError>
Get positions for the specified currency and kind
Retrieves user positions filtered by currency and/or instrument kind.
§Arguments
currency- Currency filter (BTC, ETH, USDC, etc.) - optionalkind- Kind filter (future, option, spot, etc.) - optional
§Returns
A vector of positions matching the filter criteria
§Errors
Returns an error if the request fails or the response cannot be parsed
Sourcepub async fn get_account_summary(
&self,
currency: &str,
extended: Option<bool>,
) -> Result<AccountSummary, WebSocketError>
pub async fn get_account_summary( &self, currency: &str, extended: Option<bool>, ) -> Result<AccountSummary, WebSocketError>
Get account summary for the specified currency
Retrieves account summary information including balance, margin, and other account details.
§Arguments
currency- Currency to get summary for (BTC, ETH, USDC, etc.)extended- Whether to include extended information
§Returns
Account summary for the specified currency
§Errors
Returns an error if the request fails or the response cannot be parsed
Sourcepub async fn get_order_state(
&self,
order_id: &str,
) -> Result<OrderInfo, WebSocketError>
pub async fn get_order_state( &self, order_id: &str, ) -> Result<OrderInfo, WebSocketError>
Sourcepub async fn get_order_history_by_currency(
&self,
currency: &str,
kind: Option<&str>,
count: Option<u32>,
) -> Result<Vec<OrderInfo>, WebSocketError>
pub async fn get_order_history_by_currency( &self, currency: &str, kind: Option<&str>, count: Option<u32>, ) -> Result<Vec<OrderInfo>, WebSocketError>
Get order history by currency
Retrieves historical orders for the specified currency.
§Arguments
currency- Currency to get order history forkind- Kind filter (future, option, spot, etc.) - optionalcount- Number of items to return - optional
§Returns
A vector of historical orders matching the filter criteria
§Errors
Returns an error if the request fails or the response cannot be parsed
Sourcepub async fn close_position(
&self,
instrument_name: &str,
order_type: &str,
price: Option<f64>,
) -> Result<ClosePositionResponse, WebSocketError>
pub async fn close_position( &self, instrument_name: &str, order_type: &str, price: Option<f64>, ) -> Result<ClosePositionResponse, WebSocketError>
Close an existing position
Places a reduce-only order to close an existing position.
§Arguments
instrument_name- The instrument to close position fororder_type- Order type: “limit” or “market”price- Price for limit orders (required if order_type is “limit”)
§Returns
Response containing the order and any trades executed
§Errors
Returns an error if the request fails or the response cannot be parsed
Sourcepub async fn move_positions(
&self,
currency: &str,
source_uid: u64,
target_uid: u64,
trades: &[MovePositionTrade],
) -> Result<Vec<MovePositionResult>, WebSocketError>
pub async fn move_positions( &self, currency: &str, source_uid: u64, target_uid: u64, trades: &[MovePositionTrade], ) -> Result<Vec<MovePositionResult>, WebSocketError>
Move positions between subaccounts
Transfers positions from one subaccount to another within the same main account.
§Arguments
currency- Currency for the positions (BTC, ETH, etc.)source_uid- Source subaccount IDtarget_uid- Target subaccount IDtrades- List of positions to move
§Returns
A vector of results for each position moved
§Errors
Returns an error if the request fails or the response cannot be parsed
Sourcepub fn set_message_handler<F, E>(
&mut self,
message_callback: F,
error_callback: E,
)
pub fn set_message_handler<F, E>( &mut self, message_callback: F, error_callback: E, )
Set message handler with callbacks The message_callback processes each incoming message and returns Result<(), Error> The error_callback is called only when message_callback returns an error
Sourcepub fn set_message_handler_builder(&mut self, handler: MessageHandler)
pub fn set_message_handler_builder(&mut self, handler: MessageHandler)
Set message handler using builder pattern
Sourcepub fn clear_message_handler(&mut self)
pub fn clear_message_handler(&mut self)
Remove the current message handler
Sourcepub fn has_message_handler(&self) -> bool
pub fn has_message_handler(&self) -> bool
Check if message handler is set
Sourcepub async fn receive_and_process_message(&self) -> Result<(), WebSocketError>
pub async fn receive_and_process_message(&self) -> Result<(), WebSocketError>
Receive and process a message using the registered callbacks This method will:
- Receive a message from the WebSocket
- Call the primary callback with the message
- If primary callback returns error, call error callback with message and error
Sourcepub async fn start_message_processing_loop(&self) -> Result<(), WebSocketError>
pub async fn start_message_processing_loop(&self) -> Result<(), WebSocketError>
Start message processing loop with callbacks This will continuously receive messages and process them using the registered callbacks The loop will continue until an error occurs or the connection is closed