nautilus_model/defi/data/
mod.rs1use std::fmt::Display;
22
23use crate::{
24 defi::{Pool, pool_analysis::snapshot::PoolSnapshot},
25 identifiers::InstrumentId,
26};
27
28pub mod block;
29pub mod collect;
30pub mod flash;
31pub mod liquidity;
32pub mod swap;
33pub mod swap_trade_info;
34pub mod transaction;
35
36pub use block::Block;
38pub use collect::PoolFeeCollect;
39pub use flash::PoolFlash;
40pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
41pub use swap::PoolSwap;
42pub use transaction::Transaction;
43
44#[derive(Debug, Clone, PartialEq)]
45pub enum DexPoolData {
46 Swap(PoolSwap),
47 LiquidityUpdate(PoolLiquidityUpdate),
48 FeeCollect(PoolFeeCollect),
49 Flash(PoolFlash),
50}
51
52impl DexPoolData {
53 #[must_use]
55 pub fn block_number(&self) -> u64 {
56 match self {
57 Self::Swap(s) => s.block,
58 Self::LiquidityUpdate(u) => u.block,
59 Self::FeeCollect(c) => c.block,
60 Self::Flash(f) => f.block,
61 }
62 }
63
64 #[must_use]
66 pub fn transaction_index(&self) -> u32 {
67 match self {
68 Self::Swap(s) => s.transaction_index,
69 Self::LiquidityUpdate(u) => u.transaction_index,
70 Self::FeeCollect(c) => c.transaction_index,
71 Self::Flash(f) => f.transaction_index,
72 }
73 }
74
75 #[must_use]
77 pub fn log_index(&self) -> u32 {
78 match self {
79 Self::Swap(s) => s.log_index,
80 Self::LiquidityUpdate(u) => u.log_index,
81 Self::FeeCollect(c) => c.log_index,
82 Self::Flash(f) => f.log_index,
83 }
84 }
85}
86
87#[cfg_attr(
89 feature = "python",
90 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
91)]
92#[allow(clippy::large_enum_variant)]
93#[derive(Debug, Clone, PartialEq)]
94pub enum DefiData {
95 Block(Block),
97 Pool(Pool),
99 PoolSnapshot(PoolSnapshot),
101 PoolSwap(PoolSwap),
103 PoolLiquidityUpdate(PoolLiquidityUpdate),
105 PoolFeeCollect(PoolFeeCollect),
107 PoolFlash(PoolFlash),
109}
110
111impl DefiData {
112 #[must_use]
118 pub fn instrument_id(&self) -> InstrumentId {
119 match self {
120 Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), Self::PoolSnapshot(snapshot) => snapshot.instrument_id,
122 Self::PoolSwap(swap) => swap.instrument_id,
123 Self::PoolLiquidityUpdate(update) => update.instrument_id,
124 Self::PoolFeeCollect(collect) => collect.instrument_id,
125 Self::Pool(pool) => pool.instrument_id,
126 Self::PoolFlash(flash) => flash.instrument_id,
127 }
128 }
129}
130
131impl Display for DefiData {
132 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133 match self {
134 Self::Block(b) => write!(f, "{b}"),
135 Self::Pool(p) => write!(f, "{p}"),
136 Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
137 Self::PoolSwap(s) => write!(f, "{s}"),
138 Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
139 Self::PoolFeeCollect(c) => write!(f, "{c}"),
140 Self::PoolFlash(p) => write!(f, "{p}"),
141 }
142 }
143}
144
145impl From<Pool> for DefiData {
146 fn from(value: Pool) -> Self {
147 Self::Pool(value)
148 }
149}
150
151impl From<PoolSwap> for DefiData {
152 fn from(value: PoolSwap) -> Self {
153 Self::PoolSwap(value)
154 }
155}
156
157impl From<PoolLiquidityUpdate> for DefiData {
158 fn from(value: PoolLiquidityUpdate) -> Self {
159 Self::PoolLiquidityUpdate(value)
160 }
161}
162
163impl From<PoolFeeCollect> for DefiData {
164 fn from(value: PoolFeeCollect) -> Self {
165 Self::PoolFeeCollect(value)
166 }
167}
168
169impl From<PoolSnapshot> for DefiData {
170 fn from(value: PoolSnapshot) -> Self {
171 Self::PoolSnapshot(value)
172 }
173}