nautilus_common/messages/defi/
mod.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 Posei Systems Pty Ltd. All rights reserved.
3//  https://poseitrader.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//! DeFi (Decentralized Finance) specific messages.
17
18use std::any::Any;
19
20use nautilus_core::{UUID4, UnixNanos};
21use nautilus_model::identifiers::{ClientId, Venue};
22
23pub mod subscribe;
24pub mod unsubscribe;
25
26// Re-exports
27pub use subscribe::{
28    SubscribeBlocks, SubscribePool, SubscribePoolLiquidityUpdates, SubscribePoolSwaps,
29};
30pub use unsubscribe::{
31    UnsubscribeBlocks, UnsubscribePool, UnsubscribePoolLiquidityUpdates, UnsubscribePoolSwaps,
32};
33
34#[derive(Clone, Debug)]
35pub enum DefiDataCommand {
36    Subscribe(DefiSubscribeCommand),
37    Unsubscribe(DefiUnsubscribeCommand),
38}
39
40impl PartialEq for DefiDataCommand {
41    fn eq(&self, other: &Self) -> bool {
42        self.command_id() == other.command_id()
43    }
44}
45
46impl DefiDataCommand {
47    /// Converts the command to a dyn Any trait object for messaging.
48    pub fn as_any(&self) -> &dyn Any {
49        self
50    }
51
52    pub fn command_id(&self) -> UUID4 {
53        match self {
54            Self::Subscribe(cmd) => cmd.command_id(),
55            Self::Unsubscribe(cmd) => cmd.command_id(),
56        }
57    }
58
59    pub fn client_id(&self) -> Option<&ClientId> {
60        match self {
61            Self::Subscribe(cmd) => cmd.client_id(),
62            Self::Unsubscribe(cmd) => cmd.client_id(),
63        }
64    }
65
66    pub fn venue(&self) -> Option<&Venue> {
67        match self {
68            Self::Subscribe(cmd) => cmd.venue(),
69            Self::Unsubscribe(cmd) => cmd.venue(),
70        }
71    }
72
73    pub fn ts_init(&self) -> UnixNanos {
74        match self {
75            Self::Subscribe(cmd) => cmd.ts_init(),
76            Self::Unsubscribe(cmd) => cmd.ts_init(),
77        }
78    }
79}
80
81#[derive(Clone, Debug)]
82pub enum DefiSubscribeCommand {
83    Blocks(SubscribeBlocks),
84    Pool(SubscribePool),
85    PoolSwaps(SubscribePoolSwaps),
86    PoolLiquidityUpdates(SubscribePoolLiquidityUpdates),
87}
88
89impl PartialEq for DefiSubscribeCommand {
90    fn eq(&self, other: &Self) -> bool {
91        self.command_id() == other.command_id()
92    }
93}
94
95impl DefiSubscribeCommand {
96    /// Converts the command to a dyn Any trait object for messaging.
97    pub fn as_any(&self) -> &dyn Any {
98        self
99    }
100
101    pub fn command_id(&self) -> UUID4 {
102        match self {
103            Self::Blocks(cmd) => cmd.command_id,
104            Self::Pool(cmd) => cmd.command_id,
105            Self::PoolSwaps(cmd) => cmd.command_id,
106            Self::PoolLiquidityUpdates(cmd) => cmd.command_id,
107        }
108    }
109
110    pub fn client_id(&self) -> Option<&ClientId> {
111        match self {
112            Self::Blocks(cmd) => cmd.client_id.as_ref(),
113            Self::Pool(cmd) => cmd.client_id.as_ref(),
114            Self::PoolSwaps(cmd) => cmd.client_id.as_ref(),
115            Self::PoolLiquidityUpdates(cmd) => cmd.client_id.as_ref(),
116        }
117    }
118
119    // TODO: TBD
120    pub fn venue(&self) -> Option<&Venue> {
121        match self {
122            Self::Blocks(_) => None,
123            Self::Pool(_) => None,
124            Self::PoolSwaps(_) => None,
125            Self::PoolLiquidityUpdates(_) => None,
126        }
127    }
128
129    pub fn ts_init(&self) -> UnixNanos {
130        match self {
131            Self::Blocks(cmd) => cmd.ts_init,
132            Self::PoolSwaps(cmd) => cmd.ts_init,
133            Self::PoolLiquidityUpdates(cmd) => cmd.ts_init,
134            Self::Pool(cmd) => cmd.ts_init,
135        }
136    }
137}
138
139#[derive(Clone, Debug)]
140pub enum DefiUnsubscribeCommand {
141    Blocks(UnsubscribeBlocks),
142    Pool(UnsubscribePool),
143    PoolSwaps(UnsubscribePoolSwaps),
144    PoolLiquidityUpdates(UnsubscribePoolLiquidityUpdates),
145}
146
147impl PartialEq for DefiUnsubscribeCommand {
148    fn eq(&self, other: &Self) -> bool {
149        self.command_id() == other.command_id()
150    }
151}
152
153impl DefiUnsubscribeCommand {
154    /// Converts the command to a dyn Any trait object for messaging.
155    pub fn as_any(&self) -> &dyn Any {
156        self
157    }
158
159    pub fn command_id(&self) -> UUID4 {
160        match self {
161            Self::Blocks(cmd) => cmd.command_id,
162            Self::Pool(cmd) => cmd.command_id,
163            Self::PoolSwaps(cmd) => cmd.command_id,
164            Self::PoolLiquidityUpdates(cmd) => cmd.command_id,
165        }
166    }
167
168    pub fn client_id(&self) -> Option<&ClientId> {
169        match self {
170            Self::Blocks(cmd) => cmd.client_id.as_ref(),
171            Self::Pool(cmd) => cmd.client_id.as_ref(),
172            Self::PoolSwaps(cmd) => cmd.client_id.as_ref(),
173            Self::PoolLiquidityUpdates(cmd) => cmd.client_id.as_ref(),
174        }
175    }
176
177    // TODO: TBD
178    pub fn venue(&self) -> Option<&Venue> {
179        match self {
180            Self::Blocks(_) => None,
181            Self::Pool(_) => None,
182            Self::PoolSwaps(_) => None,
183            Self::PoolLiquidityUpdates(_) => None,
184        }
185    }
186
187    pub fn ts_init(&self) -> UnixNanos {
188        match self {
189            Self::Blocks(cmd) => cmd.ts_init,
190            Self::Pool(cmd) => cmd.ts_init,
191            Self::PoolSwaps(cmd) => cmd.ts_init,
192            Self::PoolLiquidityUpdates(cmd) => cmd.ts_init,
193        }
194    }
195}