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", from_py_object)
91)]
92#[cfg_attr(
93 feature = "python",
94 pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.model")
95)]
96#[derive(Debug, Clone, PartialEq)]
97pub enum DefiData {
98 Block(Block),
100 Pool(Pool),
102 PoolSnapshot(PoolSnapshot),
104 PoolSwap(PoolSwap),
106 PoolLiquidityUpdate(PoolLiquidityUpdate),
108 PoolFeeCollect(PoolFeeCollect),
110 PoolFlash(PoolFlash),
112}
113
114impl DefiData {
115 #[must_use]
121 pub fn instrument_id(&self) -> InstrumentId {
122 match self {
123 Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), Self::PoolSnapshot(snapshot) => snapshot.instrument_id,
125 Self::PoolSwap(swap) => swap.instrument_id,
126 Self::PoolLiquidityUpdate(update) => update.instrument_id,
127 Self::PoolFeeCollect(collect) => collect.instrument_id,
128 Self::Pool(pool) => pool.instrument_id,
129 Self::PoolFlash(flash) => flash.instrument_id,
130 }
131 }
132}
133
134impl Display for DefiData {
135 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136 match self {
137 Self::Block(b) => write!(f, "{b}"),
138 Self::Pool(p) => write!(f, "{p}"),
139 Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
140 Self::PoolSwap(s) => write!(f, "{s}"),
141 Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
142 Self::PoolFeeCollect(c) => write!(f, "{c}"),
143 Self::PoolFlash(p) => write!(f, "{p}"),
144 }
145 }
146}
147
148impl From<Pool> for DefiData {
149 fn from(value: Pool) -> Self {
150 Self::Pool(value)
151 }
152}
153
154impl From<PoolSwap> for DefiData {
155 fn from(value: PoolSwap) -> Self {
156 Self::PoolSwap(value)
157 }
158}
159
160impl From<PoolLiquidityUpdate> for DefiData {
161 fn from(value: PoolLiquidityUpdate) -> Self {
162 Self::PoolLiquidityUpdate(value)
163 }
164}
165
166impl From<PoolFeeCollect> for DefiData {
167 fn from(value: PoolFeeCollect) -> Self {
168 Self::PoolFeeCollect(value)
169 }
170}
171
172impl From<PoolSnapshot> for DefiData {
173 fn from(value: PoolSnapshot) -> Self {
174 Self::PoolSnapshot(value)
175 }
176}