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
53impl DexPoolData {
54 #[must_use]
56 pub fn block_number(&self) -> u64 {
57 match self {
58 Self::Swap(s) => s.block,
59 Self::LiquidityUpdate(u) => u.block,
60 Self::FeeCollect(c) => c.block,
61 Self::Flash(f) => f.block,
62 }
63 }
64
65 #[must_use]
67 pub fn transaction_index(&self) -> u32 {
68 match self {
69 Self::Swap(s) => s.transaction_index,
70 Self::LiquidityUpdate(u) => u.transaction_index,
71 Self::FeeCollect(c) => c.transaction_index,
72 Self::Flash(f) => f.transaction_index,
73 }
74 }
75
76 #[must_use]
78 pub fn log_index(&self) -> u32 {
79 match self {
80 Self::Swap(s) => s.log_index,
81 Self::LiquidityUpdate(u) => u.log_index,
82 Self::FeeCollect(c) => c.log_index,
83 Self::Flash(f) => f.log_index,
84 }
85 }
86}
87
88#[cfg_attr(
90 feature = "python",
91 pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model")
92)]
93#[allow(clippy::large_enum_variant)]
94#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
95pub enum DefiData {
96 Block(Block),
98 Pool(Pool),
100 PoolSnapshot(PoolSnapshot),
102 PoolSwap(PoolSwap),
104 PoolLiquidityUpdate(PoolLiquidityUpdate),
106 PoolFeeCollect(PoolFeeCollect),
108 PoolFlash(PoolFlash),
110}
111
112impl DefiData {
113 #[must_use]
119 pub fn instrument_id(&self) -> InstrumentId {
120 match self {
121 Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), Self::PoolSnapshot(snapshot) => snapshot.instrument_id,
123 Self::PoolSwap(swap) => swap.instrument_id,
124 Self::PoolLiquidityUpdate(update) => update.instrument_id,
125 Self::PoolFeeCollect(collect) => collect.instrument_id,
126 Self::Pool(pool) => pool.instrument_id,
127 Self::PoolFlash(flash) => flash.instrument_id,
128 }
129 }
130}
131
132impl Display for DefiData {
133 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
134 match self {
135 Self::Block(b) => write!(f, "{b}"),
136 Self::Pool(p) => write!(f, "{p}"),
137 Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
138 Self::PoolSwap(s) => write!(f, "{s}"),
139 Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
140 Self::PoolFeeCollect(c) => write!(f, "{c}"),
141 Self::PoolFlash(p) => write!(f, "{p}"),
142 }
143 }
144}
145
146impl From<Pool> for DefiData {
147 fn from(value: Pool) -> Self {
148 Self::Pool(value)
149 }
150}
151
152impl From<PoolSwap> for DefiData {
153 fn from(value: PoolSwap) -> Self {
154 Self::PoolSwap(value)
155 }
156}
157
158impl From<PoolLiquidityUpdate> for DefiData {
159 fn from(value: PoolLiquidityUpdate) -> Self {
160 Self::PoolLiquidityUpdate(value)
161 }
162}
163
164impl From<PoolFeeCollect> for DefiData {
165 fn from(value: PoolFeeCollect) -> Self {
166 Self::PoolFeeCollect(value)
167 }
168}
169
170impl From<PoolSnapshot> for DefiData {
171 fn from(value: PoolSnapshot) -> Self {
172 Self::PoolSnapshot(value)
173 }
174}