nautilus_deribit/common/rpc.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2025 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! JSON-RPC 2.0 protocol structures shared by HTTP and WebSocket interfaces.
17
18use serde::{Deserialize, Serialize};
19
20/// JSON-RPC 2.0 request envelope.
21///
22/// Used by both HTTP API calls and WebSocket method invocations.
23#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct DeribitJsonRpcRequest<T> {
25 /// JSON-RPC version (always "2.0").
26 pub jsonrpc: &'static str,
27 /// Request ID for correlation.
28 pub id: u64,
29 /// JSON-RPC method name.
30 pub method: String,
31 /// Method-specific parameters.
32 pub params: T,
33}
34
35impl<T> DeribitJsonRpcRequest<T> {
36 /// Creates a new JSON-RPC request.
37 #[must_use]
38 pub fn new(id: u64, method: impl Into<String>, params: T) -> Self {
39 Self {
40 jsonrpc: "2.0",
41 id,
42 method: method.into(),
43 params,
44 }
45 }
46}
47
48/// JSON-RPC 2.0 response envelope.
49///
50/// Used by both HTTP API responses and WebSocket method responses.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct DeribitJsonRpcResponse<T> {
53 /// JSON-RPC version.
54 pub jsonrpc: String,
55 /// Request ID (present for request responses).
56 #[serde(skip_serializing_if = "Option::is_none")]
57 pub id: Option<u64>,
58 /// Success result (mutually exclusive with error).
59 #[serde(skip_serializing_if = "Option::is_none")]
60 pub result: Option<T>,
61 /// Error details (mutually exclusive with result).
62 #[serde(skip_serializing_if = "Option::is_none")]
63 pub error: Option<DeribitJsonRpcError>,
64 /// Whether this is from testnet.
65 #[serde(default)]
66 pub testnet: bool,
67 /// Server receive timestamp (microseconds).
68 #[serde(rename = "usIn")]
69 pub us_in: Option<u64>,
70 /// Server send timestamp (microseconds).
71 #[serde(rename = "usOut")]
72 pub us_out: Option<u64>,
73 /// Processing time difference (microseconds).
74 #[serde(rename = "usDiff")]
75 pub us_diff: Option<u64>,
76}
77
78/// JSON-RPC 2.0 error object.
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct DeribitJsonRpcError {
81 /// Error code.
82 pub code: i64,
83 /// Error message.
84 pub message: String,
85 /// Additional error data.
86 #[serde(default)]
87 pub data: Option<serde_json::Value>,
88}