nautilus_model/defi/data/
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) 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 nautilus_core::UnixNanos;
24use serde::{Deserialize, Serialize};
25
26use crate::{data::HasTsInit, defi::Pool, identifiers::InstrumentId};
27
28pub mod block;
29pub mod liquidity;
30pub mod swap;
31pub mod transaction;
32
33// Re-exports
34pub use block::Block;
35pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
36pub use swap::PoolSwap;
37pub use transaction::Transaction;
38
39/// Represents DeFi-specific data events in a decentralized exchange ecosystem.
40#[allow(clippy::large_enum_variant)]
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub enum DefiData {
43    /// A block completion in a blockchain network.
44    Block(Block),
45    /// A DEX liquidity pool definition or update.
46    Pool(Pool),
47    /// A token swap transaction on a decentralized exchange.
48    PoolSwap(PoolSwap),
49    /// A liquidity update event (mint/burn) in a DEX pool.
50    PoolLiquidityUpdate(PoolLiquidityUpdate),
51}
52
53impl DefiData {
54    /// Returns the instrument ID associated with this DeFi data.
55    #[must_use]
56    pub fn instrument_id(&self) -> InstrumentId {
57        match self {
58            Self::Block(_) => todo!("Not implemented yet"),
59            Self::PoolSwap(swap) => swap.instrument_id(),
60            Self::PoolLiquidityUpdate(update) => update.instrument_id(),
61            Self::Pool(pool) => pool.instrument_id(),
62        }
63    }
64}
65
66impl Display for DefiData {
67    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68        match self {
69            Self::Block(b) => write!(f, "{b}"),
70            Self::PoolSwap(s) => write!(f, "{s}"),
71            Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
72            Self::Pool(p) => write!(f, "{p}"),
73        }
74    }
75}
76
77impl HasTsInit for DefiData {
78    fn ts_init(&self) -> UnixNanos {
79        match self {
80            Self::Block(block) => block.timestamp, // TODO: TBD
81            Self::PoolSwap(swap) => swap.ts_init,
82            Self::PoolLiquidityUpdate(update) => update.ts_init,
83            Self::Pool(pool) => pool.ts_init,
84        }
85    }
86}
87
88impl From<PoolSwap> for DefiData {
89    fn from(value: PoolSwap) -> Self {
90        Self::PoolSwap(value)
91    }
92}
93
94impl From<PoolLiquidityUpdate> for DefiData {
95    fn from(value: PoolLiquidityUpdate) -> Self {
96        Self::PoolLiquidityUpdate(value)
97    }
98}
99
100impl From<Pool> for DefiData {
101    fn from(value: Pool) -> Self {
102        Self::Pool(value)
103    }
104}