nautilus_model/defi/data/
mod.rs1use 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
33pub use block::Block;
35pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
36pub use swap::PoolSwap;
37pub use transaction::Transaction;
38
39#[allow(clippy::large_enum_variant)]
41#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
42pub enum DefiData {
43 Block(Block),
45 Pool(Pool),
47 PoolSwap(PoolSwap),
49 PoolLiquidityUpdate(PoolLiquidityUpdate),
51}
52
53impl DefiData {
54 #[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, 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}