nautilus_model/defi/data/
mod.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//! DeFi (Decentralized Finance) data models and types.
17//!
18//! This module provides core data structures for working with decentralized finance protocols,
19//! including blockchain networks, tokens, liquidity pools, swaps, and other DeFi primitives.
20
21use std::fmt::Display;
22
23use serde::{Deserialize, Serialize};
24
25use crate::{
26    defi::{Pool, pool_analysis::snapshot::PoolSnapshot},
27    identifiers::InstrumentId,
28};
29
30pub mod block;
31pub mod collect;
32pub mod flash;
33pub mod liquidity;
34pub mod swap;
35pub mod transaction;
36
37// Re-exports
38pub use block::Block;
39pub use collect::PoolFeeCollect;
40pub use flash::PoolFlash;
41pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
42pub use swap::PoolSwap;
43pub use transaction::Transaction;
44
45#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
46pub enum DexPoolData {
47    Swap(PoolSwap),
48    LiquidityUpdate(PoolLiquidityUpdate),
49    FeeCollect(PoolFeeCollect),
50    Flash(PoolFlash),
51}
52
53impl DexPoolData {
54    /// Returns the block number associated with this pool event.
55    #[must_use]
56    pub fn block_number(&self) -> u64 {
57        match self {
58            Self::Swap(s) => s.block,
59            Self::LiquidityUpdate(u) => u.block,
60            Self::FeeCollect(c) => c.block,
61            Self::Flash(f) => f.block,
62        }
63    }
64
65    /// Returns the transaction index associated with this pool event.
66    #[must_use]
67    pub fn transaction_index(&self) -> u32 {
68        match self {
69            Self::Swap(s) => s.transaction_index,
70            Self::LiquidityUpdate(u) => u.transaction_index,
71            Self::FeeCollect(c) => c.transaction_index,
72            Self::Flash(f) => f.transaction_index,
73        }
74    }
75
76    /// Returns the log index associated with this pool event.
77    #[must_use]
78    pub fn log_index(&self) -> u32 {
79        match self {
80            Self::Swap(s) => s.log_index,
81            Self::LiquidityUpdate(u) => u.log_index,
82            Self::FeeCollect(c) => c.log_index,
83            Self::Flash(f) => f.log_index,
84        }
85    }
86}
87
88/// Represents DeFi-specific data events in a decentralized exchange ecosystem.
89#[cfg_attr(
90    feature = "python",
91    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
92)]
93#[allow(clippy::large_enum_variant)]
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub enum DefiData {
96    /// A block completion in a blockchain network.
97    Block(Block),
98    /// A DEX liquidity pool definition or update.
99    Pool(Pool),
100    /// A complete snapshot of a pool's state at a specific point in time.
101    PoolSnapshot(PoolSnapshot),
102    /// A token swap transaction on a decentralized exchange.
103    PoolSwap(PoolSwap),
104    /// A liquidity update event (mint/burn) in a DEX pool.
105    PoolLiquidityUpdate(PoolLiquidityUpdate),
106    /// A fee collection event from a DEX pool position.
107    PoolFeeCollect(PoolFeeCollect),
108    /// A flash event
109    PoolFlash(PoolFlash),
110}
111
112impl DefiData {
113    /// Returns the instrument ID associated with this DeFi data.
114    ///
115    /// # Panics
116    ///
117    /// Panics if the variant is a `Block` or `PoolSnapshot` where instrument IDs are not applicable.
118    #[must_use]
119    pub fn instrument_id(&self) -> InstrumentId {
120        match self {
121            Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), // TBD?
122            Self::PoolSnapshot(snapshot) => snapshot.instrument_id,
123            Self::PoolSwap(swap) => swap.instrument_id,
124            Self::PoolLiquidityUpdate(update) => update.instrument_id,
125            Self::PoolFeeCollect(collect) => collect.instrument_id,
126            Self::Pool(pool) => pool.instrument_id,
127            Self::PoolFlash(flash) => flash.instrument_id,
128        }
129    }
130}
131
132impl Display for DefiData {
133    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134        match self {
135            Self::Block(b) => write!(f, "{b}"),
136            Self::Pool(p) => write!(f, "{p}"),
137            Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
138            Self::PoolSwap(s) => write!(f, "{s}"),
139            Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
140            Self::PoolFeeCollect(c) => write!(f, "{c}"),
141            Self::PoolFlash(p) => write!(f, "{p}"),
142        }
143    }
144}
145
146impl From<Pool> for DefiData {
147    fn from(value: Pool) -> Self {
148        Self::Pool(value)
149    }
150}
151
152impl From<PoolSwap> for DefiData {
153    fn from(value: PoolSwap) -> Self {
154        Self::PoolSwap(value)
155    }
156}
157
158impl From<PoolLiquidityUpdate> for DefiData {
159    fn from(value: PoolLiquidityUpdate) -> Self {
160        Self::PoolLiquidityUpdate(value)
161    }
162}
163
164impl From<PoolFeeCollect> for DefiData {
165    fn from(value: PoolFeeCollect) -> Self {
166        Self::PoolFeeCollect(value)
167    }
168}
169
170impl From<PoolSnapshot> for DefiData {
171    fn from(value: PoolSnapshot) -> Self {
172        Self::PoolSnapshot(value)
173    }
174}