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
53/// Represents DeFi-specific data events in a decentralized exchange ecosystem.
54#[cfg_attr(
55    feature = "python",
56    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
57)]
58#[allow(clippy::large_enum_variant)]
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60pub enum DefiData {
61    /// A block completion in a blockchain network.
62    Block(Block),
63    /// A DEX liquidity pool definition or update.
64    Pool(Pool),
65    /// A complete snapshot of a pool's state at a specific point in time.
66    PoolSnapshot(PoolSnapshot),
67    /// A token swap transaction on a decentralized exchange.
68    PoolSwap(PoolSwap),
69    /// A liquidity update event (mint/burn) in a DEX pool.
70    PoolLiquidityUpdate(PoolLiquidityUpdate),
71    /// A fee collection event from a DEX pool position.
72    PoolFeeCollect(PoolFeeCollect),
73    /// A flash event
74    PoolFlash(PoolFlash),
75}
76
77impl DefiData {
78    /// Returns the instrument ID associated with this DeFi data.
79    ///
80    /// # Panics
81    ///
82    /// Panics if the variant is a `Block` or `PoolSnapshot` where instrument IDs are not applicable.
83    #[must_use]
84    pub fn instrument_id(&self) -> InstrumentId {
85        match self {
86            Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), // TBD?
87            Self::PoolSnapshot(_) => panic!("`InstrumentId` not applicable to `PoolSnapshot`"), // TBD?
88            Self::PoolSwap(swap) => swap.instrument_id(),
89            Self::PoolLiquidityUpdate(update) => update.instrument_id(),
90            Self::PoolFeeCollect(collect) => collect.instrument_id(),
91            Self::Pool(pool) => pool.instrument_id,
92            Self::PoolFlash(flash) => flash.instrument_id(),
93        }
94    }
95}
96
97impl Display for DefiData {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        match self {
100            Self::Block(b) => write!(f, "{b}"),
101            Self::Pool(p) => write!(f, "{p}"),
102            Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
103            Self::PoolSwap(s) => write!(f, "{s}"),
104            Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
105            Self::PoolFeeCollect(c) => write!(f, "{c}"),
106            Self::PoolFlash(p) => write!(f, "{p}"),
107        }
108    }
109}
110
111impl From<Pool> for DefiData {
112    fn from(value: Pool) -> Self {
113        Self::Pool(value)
114    }
115}
116
117impl From<PoolSwap> for DefiData {
118    fn from(value: PoolSwap) -> Self {
119        Self::PoolSwap(value)
120    }
121}
122
123impl From<PoolLiquidityUpdate> for DefiData {
124    fn from(value: PoolLiquidityUpdate) -> Self {
125        Self::PoolLiquidityUpdate(value)
126    }
127}
128
129impl From<PoolFeeCollect> for DefiData {
130    fn from(value: PoolFeeCollect) -> Self {
131        Self::PoolFeeCollect(value)
132    }
133}
134
135impl From<PoolSnapshot> for DefiData {
136    fn from(value: PoolSnapshot) -> Self {
137        Self::PoolSnapshot(value)
138    }
139}