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::{defi::Pool, identifiers::InstrumentId};
26
27pub mod block;
28pub mod collect;
29pub mod liquidity;
30pub mod swap;
31pub mod transaction;
32
33// Re-exports
34pub use block::Block;
35pub use collect::PoolFeeCollect;
36pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
37pub use swap::PoolSwap;
38pub use transaction::Transaction;
39
40/// Represents DeFi-specific data events in a decentralized exchange ecosystem.
41#[cfg_attr(
42    feature = "python",
43    pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
44)]
45#[allow(clippy::large_enum_variant)]
46#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
47pub enum DefiData {
48    /// A block completion in a blockchain network.
49    Block(Block),
50    /// A DEX liquidity pool definition or update.
51    Pool(Pool),
52    /// A token swap transaction on a decentralized exchange.
53    PoolSwap(PoolSwap),
54    /// A liquidity update event (mint/burn) in a DEX pool.
55    PoolLiquidityUpdate(PoolLiquidityUpdate),
56    /// A fee collection event from a DEX pool position.
57    PoolFeeCollect(PoolFeeCollect),
58}
59
60impl DefiData {
61    /// Returns the instrument ID associated with this DeFi data.
62    ///
63    /// # Panics
64    ///
65    /// Panics if the variant is a `Block` where instrument IDs are not applicable.
66    #[must_use]
67    pub fn instrument_id(&self) -> InstrumentId {
68        match self {
69            Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), // TBD?
70            Self::PoolSwap(swap) => swap.instrument_id,
71            Self::PoolLiquidityUpdate(update) => update.instrument_id,
72            Self::PoolFeeCollect(collect) => collect.instrument_id,
73            Self::Pool(pool) => pool.instrument_id,
74        }
75    }
76}
77
78impl Display for DefiData {
79    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
80        match self {
81            Self::Block(b) => write!(f, "{b}"),
82            Self::PoolSwap(s) => write!(f, "{s}"),
83            Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
84            Self::PoolFeeCollect(c) => write!(f, "{c}"),
85            Self::Pool(p) => write!(f, "{p}"),
86        }
87    }
88}
89
90impl From<PoolSwap> for DefiData {
91    fn from(value: PoolSwap) -> Self {
92        Self::PoolSwap(value)
93    }
94}
95
96impl From<PoolLiquidityUpdate> for DefiData {
97    fn from(value: PoolLiquidityUpdate) -> Self {
98        Self::PoolLiquidityUpdate(value)
99    }
100}
101
102impl From<Pool> for DefiData {
103    fn from(value: Pool) -> Self {
104        Self::Pool(value)
105    }
106}
107
108impl From<PoolFeeCollect> for DefiData {
109    fn from(value: PoolFeeCollect) -> Self {
110        Self::PoolFeeCollect(value)
111    }
112}