use std::fmt::Display;
use crate::{
defi::{Pool, pool_analysis::snapshot::PoolSnapshot},
identifiers::InstrumentId,
};
pub mod block;
pub mod collect;
pub mod flash;
pub mod liquidity;
pub mod swap;
pub mod swap_trade_info;
pub mod transaction;
pub use block::Block;
pub use collect::PoolFeeCollect;
pub use flash::PoolFlash;
pub use liquidity::{PoolLiquidityUpdate, PoolLiquidityUpdateType};
pub use swap::PoolSwap;
pub use transaction::Transaction;
#[derive(Debug, Clone, PartialEq)]
pub enum DexPoolData {
Swap(PoolSwap),
LiquidityUpdate(PoolLiquidityUpdate),
FeeCollect(PoolFeeCollect),
Flash(PoolFlash),
}
impl DexPoolData {
#[must_use]
pub fn block_number(&self) -> u64 {
match self {
Self::Swap(s) => s.block,
Self::LiquidityUpdate(u) => u.block,
Self::FeeCollect(c) => c.block,
Self::Flash(f) => f.block,
}
}
#[must_use]
pub fn transaction_index(&self) -> u32 {
match self {
Self::Swap(s) => s.transaction_index,
Self::LiquidityUpdate(u) => u.transaction_index,
Self::FeeCollect(c) => c.transaction_index,
Self::Flash(f) => f.transaction_index,
}
}
#[must_use]
pub fn log_index(&self) -> u32 {
match self {
Self::Swap(s) => s.log_index,
Self::LiquidityUpdate(u) => u.log_index,
Self::FeeCollect(c) => c.log_index,
Self::Flash(f) => f.log_index,
}
}
}
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass_enum(module = "nautilus_trader.model")
)]
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, PartialEq)]
pub enum DefiData {
Block(Block),
Pool(Pool),
PoolSnapshot(PoolSnapshot),
PoolSwap(PoolSwap),
PoolLiquidityUpdate(PoolLiquidityUpdate),
PoolFeeCollect(PoolFeeCollect),
PoolFlash(PoolFlash),
}
impl DefiData {
#[must_use]
pub fn instrument_id(&self) -> InstrumentId {
match self {
Self::Block(_) => panic!("`InstrumentId` not applicable to `Block`"), Self::PoolSnapshot(snapshot) => snapshot.instrument_id,
Self::PoolSwap(swap) => swap.instrument_id,
Self::PoolLiquidityUpdate(update) => update.instrument_id,
Self::PoolFeeCollect(collect) => collect.instrument_id,
Self::Pool(pool) => pool.instrument_id,
Self::PoolFlash(flash) => flash.instrument_id,
}
}
}
impl Display for DefiData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Block(b) => write!(f, "{b}"),
Self::Pool(p) => write!(f, "{p}"),
Self::PoolSnapshot(s) => write!(f, "PoolSnapshot(block={})", s.block_position.number),
Self::PoolSwap(s) => write!(f, "{s}"),
Self::PoolLiquidityUpdate(u) => write!(f, "{u}"),
Self::PoolFeeCollect(c) => write!(f, "{c}"),
Self::PoolFlash(p) => write!(f, "{p}"),
}
}
}
impl From<Pool> for DefiData {
fn from(value: Pool) -> Self {
Self::Pool(value)
}
}
impl From<PoolSwap> for DefiData {
fn from(value: PoolSwap) -> Self {
Self::PoolSwap(value)
}
}
impl From<PoolLiquidityUpdate> for DefiData {
fn from(value: PoolLiquidityUpdate) -> Self {
Self::PoolLiquidityUpdate(value)
}
}
impl From<PoolFeeCollect> for DefiData {
fn from(value: PoolFeeCollect) -> Self {
Self::PoolFeeCollect(value)
}
}
impl From<PoolSnapshot> for DefiData {
fn from(value: PoolSnapshot) -> Self {
Self::PoolSnapshot(value)
}
}