Skip to main content

nautilus_deribit/common/
enums.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 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//! Common enumerations for the Deribit adapter.
17
18use std::fmt::Display;
19
20use nautilus_model::enums::{MarketStatusAction, TimeInForce, TriggerType};
21use serde::{Deserialize, Serialize};
22use strum::{AsRefStr, Display as StrumDisplay, EnumIter, EnumString};
23
24/// Deribit product type.
25#[derive(
26    Clone,
27    Copy,
28    Debug,
29    PartialEq,
30    Eq,
31    Hash,
32    AsRefStr,
33    StrumDisplay,
34    EnumIter,
35    EnumString,
36    Serialize,
37    Deserialize,
38)]
39#[serde(rename_all = "snake_case")]
40#[strum(serialize_all = "snake_case")]
41#[cfg_attr(
42    feature = "python",
43    pyo3::pyclass(
44        eq,
45        eq_int,
46        module = "nautilus_trader.adapters.deribit",
47        from_py_object,
48        rename_all = "SCREAMING_SNAKE_CASE",
49    )
50)]
51#[cfg_attr(
52    feature = "python",
53    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.adapters.deribit")
54)]
55pub enum DeribitProductType {
56    /// Future contract
57    Future,
58    /// Option contract
59    Option,
60    /// Spot market
61    Spot,
62    /// Future combo
63    #[serde(rename = "future_combo")]
64    FutureCombo,
65    /// Option combo
66    #[serde(rename = "option_combo")]
67    OptionCombo,
68}
69
70/// Deribit currency.
71#[derive(
72    Clone, Copy, Debug, PartialEq, Eq, Hash, AsRefStr, EnumIter, EnumString, Serialize, Deserialize,
73)]
74#[serde(rename_all = "UPPERCASE")]
75#[strum(serialize_all = "UPPERCASE")]
76#[cfg_attr(
77    feature = "python",
78    pyo3::pyclass(
79        eq,
80        eq_int,
81        module = "nautilus_trader.adapters.deribit",
82        from_py_object,
83        rename_all = "SCREAMING_SNAKE_CASE",
84    )
85)]
86#[cfg_attr(
87    feature = "python",
88    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.adapters.deribit")
89)]
90pub enum DeribitCurrency {
91    /// Bitcoin
92    BTC,
93    /// Ethereum
94    ETH,
95    /// USD Coin
96    USDC,
97    /// Tether
98    USDT,
99    /// Euro stablecoin
100    EURR,
101    /// All currencies
102    #[serde(rename = "any")]
103    ANY,
104}
105
106impl DeribitCurrency {
107    /// Returns the currency as a string.
108    #[must_use]
109    pub fn as_str(&self) -> &'static str {
110        match self {
111            Self::BTC => "BTC",
112            Self::ETH => "ETH",
113            Self::USDC => "USDC",
114            Self::USDT => "USDT",
115            Self::EURR => "EURR",
116            Self::ANY => "any",
117        }
118    }
119}
120
121impl Display for DeribitCurrency {
122    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123        write!(f, "{}", self.as_str())
124    }
125}
126
127/// Deribit option type.
128#[derive(
129    Clone,
130    Copy,
131    Debug,
132    PartialEq,
133    Eq,
134    Hash,
135    AsRefStr,
136    StrumDisplay,
137    EnumIter,
138    EnumString,
139    Serialize,
140    Deserialize,
141)]
142#[serde(rename_all = "lowercase")]
143#[strum(serialize_all = "lowercase")]
144pub enum DeribitOptionType {
145    /// Call option
146    Call,
147    /// Put option
148    Put,
149}
150
151/// Deribit instrument lifecycle state.
152#[derive(
153    Clone,
154    Copy,
155    Debug,
156    PartialEq,
157    Eq,
158    Hash,
159    AsRefStr,
160    StrumDisplay,
161    EnumIter,
162    EnumString,
163    Serialize,
164    Deserialize,
165)]
166#[serde(rename_all = "snake_case")]
167#[strum(serialize_all = "snake_case")]
168pub enum DeribitInstrumentState {
169    /// Instrument has been created but not yet active.
170    Created,
171    /// Instrument is active and trading.
172    Started,
173    /// Instrument has been settled (options/futures at expiry).
174    Settled,
175    /// Instrument is closed for trading.
176    Closed,
177    /// Instrument has been terminated.
178    Terminated,
179}
180
181impl From<DeribitInstrumentState> for MarketStatusAction {
182    fn from(state: DeribitInstrumentState) -> Self {
183        match state {
184            DeribitInstrumentState::Created => Self::PreOpen,
185            DeribitInstrumentState::Started => Self::Trading,
186            DeribitInstrumentState::Settled => Self::Close,
187            DeribitInstrumentState::Closed => Self::Close,
188            DeribitInstrumentState::Terminated => Self::NotAvailableForTrading,
189        }
190    }
191}
192
193/// Deribit time in force values for order execution.
194#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
195#[serde(rename_all = "snake_case")]
196pub enum DeribitTimeInForce {
197    /// Good till cancelled.
198    #[serde(rename = "good_til_cancelled")]
199    GoodTilCancelled,
200    /// Good till day (expires at end of trading day).
201    #[serde(rename = "good_til_day")]
202    GoodTilDay,
203    /// Immediate or cancel.
204    #[serde(rename = "immediate_or_cancel")]
205    ImmediateOrCancel,
206    /// Fill or kill.
207    #[serde(rename = "fill_or_kill")]
208    FillOrKill,
209}
210
211impl DeribitTimeInForce {
212    /// Returns the time in force as a Deribit API string.
213    #[must_use]
214    pub fn as_str(&self) -> &'static str {
215        match self {
216            Self::GoodTilCancelled => "good_til_cancelled",
217            Self::GoodTilDay => "good_til_day",
218            Self::ImmediateOrCancel => "immediate_or_cancel",
219            Self::FillOrKill => "fill_or_kill",
220        }
221    }
222}
223
224impl TryFrom<TimeInForce> for DeribitTimeInForce {
225    type Error = String;
226
227    fn try_from(tif: TimeInForce) -> Result<Self, Self::Error> {
228        match tif {
229            TimeInForce::Gtc => Ok(Self::GoodTilCancelled),
230            TimeInForce::Ioc => Ok(Self::ImmediateOrCancel),
231            TimeInForce::Fok => Ok(Self::FillOrKill),
232            TimeInForce::Gtd => Ok(Self::GoodTilDay),
233            _ => Err(format!(
234                "TimeInForce::{tif} is not supported on Deribit (valid: GTC, IOC, FOK, GTD)"
235            )),
236        }
237    }
238}
239
240/// Deribit API environment.
241#[derive(
242    Copy,
243    Clone,
244    Debug,
245    Default,
246    StrumDisplay,
247    PartialEq,
248    Eq,
249    Hash,
250    AsRefStr,
251    EnumIter,
252    EnumString,
253    Serialize,
254    Deserialize,
255)]
256#[serde(rename_all = "lowercase")]
257#[strum(ascii_case_insensitive, serialize_all = "lowercase")]
258#[cfg_attr(
259    feature = "python",
260    pyo3::pyclass(
261        eq,
262        eq_int,
263        module = "nautilus_trader.adapters.deribit",
264        from_py_object,
265        rename_all = "SCREAMING_SNAKE_CASE",
266    )
267)]
268#[cfg_attr(
269    feature = "python",
270    pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.adapters.deribit")
271)]
272pub enum DeribitEnvironment {
273    /// Live trading environment.
274    #[default]
275    Mainnet,
276    /// Testnet environment.
277    Testnet,
278}
279
280/// Resolves an optional Nautilus trigger type to a Deribit trigger string.
281pub fn resolve_trigger_type(trigger_type: Option<TriggerType>) -> Option<String> {
282    trigger_type.and_then(|tt| match tt {
283        TriggerType::LastPrice | TriggerType::Default => Some("last_price".to_string()),
284        TriggerType::MarkPrice => Some("mark_price".to_string()),
285        TriggerType::IndexPrice => Some("index_price".to_string()),
286        _ => None,
287    })
288}
289
290#[cfg(test)]
291mod tests {
292    use rstest::rstest;
293
294    use super::*;
295
296    #[rstest]
297    #[case(DeribitInstrumentState::Created, "created")]
298    #[case(DeribitInstrumentState::Started, "started")]
299    #[case(DeribitInstrumentState::Settled, "settled")]
300    #[case(DeribitInstrumentState::Closed, "closed")]
301    #[case(DeribitInstrumentState::Terminated, "terminated")]
302    fn test_deribit_instrument_state_display(
303        #[case] state: DeribitInstrumentState,
304        #[case] expected: &str,
305    ) {
306        assert_eq!(state.to_string(), expected);
307    }
308
309    #[rstest]
310    #[case(DeribitInstrumentState::Created, MarketStatusAction::PreOpen)]
311    #[case(DeribitInstrumentState::Started, MarketStatusAction::Trading)]
312    #[case(DeribitInstrumentState::Settled, MarketStatusAction::Close)]
313    #[case(DeribitInstrumentState::Closed, MarketStatusAction::Close)]
314    #[case(
315        DeribitInstrumentState::Terminated,
316        MarketStatusAction::NotAvailableForTrading
317    )]
318    fn test_deribit_instrument_state_to_market_status_action(
319        #[case] state: DeribitInstrumentState,
320        #[case] expected: MarketStatusAction,
321    ) {
322        assert_eq!(MarketStatusAction::from(state), expected);
323    }
324}