nautilus_model/defi/data/
mod.rs1use 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
37pub 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#[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 Block(Block),
63 Pool(Pool),
65 PoolSnapshot(PoolSnapshot),
67 PoolSwap(PoolSwap),
69 PoolLiquidityUpdate(PoolLiquidityUpdate),
71 PoolFeeCollect(PoolFeeCollect),
73 PoolFlash(PoolFlash),
75}
76
77impl DefiData {
78 #[must_use]
84 pub fn instrument_id(&self) -> InstrumentId {
85 match self {
86 Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), Self::PoolSnapshot(_) => panic!("`InstrumentId` not applicable to `PoolSnapshot`"), 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}